Skip to content

Instantly share code, notes, and snippets.

@sauceaaron
Created September 23, 2019 18:45
Show Gist options
  • Save sauceaaron/bbc29f4ee16bc1ee459988fb2098e9ac to your computer and use it in GitHub Desktop.
Save sauceaaron/bbc29f4ee16bc1ee459988fb2098e9ac to your computer and use it in GitHub Desktop.
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
import static org.junit.Assert.assertEquals;
public class TestDataFromName
{
@Rule
public TestName testName = new TestName();
@Test
public void testHello_Aaron()
{
// use TestName Rule to get the current method name
String methodName = testName.getMethodName();
// get everything after the underscore
String parameter = methodName.substring(methodName.indexOf("_")+1);
// execute code to test
String result = greeting(parameter);
// check result
assertEquals(result, "Hello, Aaron");
}
public String greeting(String name) {
if (name == null)
{
name = "World";
}
return "Hello, " + name;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment