下面列出了org.junit.runner.Request#method() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* .
* @param args .
* @throws ClassNotFoundException .
*/
public static void main(String... args) throws ClassNotFoundException {
int retCode = 0;
String resultMessage = "SUCCESS";
String[] classAndMethod = args[0].split("#");
Request request = Request.method(Class.forName(classAndMethod[0]),
classAndMethod[1]);
Result result = new JUnitCore().run(request);
if (!result.wasSuccessful()) {
retCode = 1;
resultMessage = "FAILURE";
}
System.out.println(resultMessage);
System.exit(retCode);
}
/**
* Lanceur d'une seule méthode de test
*
* @param args
* @throws ClassNotFoundException
*/
public static void main(String[] args) throws ClassNotFoundException
{
if(args.length != 2)
{
System.out.println("Usage : JUnit_Test class method");
}
else
{
Request request = Request.method(Class.forName(args[0]), args[1]);
Result result = new JUnitCore().run(request);
System.exit(result.wasSuccessful() ? 0 : 1);
}
}
/**
* Runs the guided fuzzing loop for a resolved class.
*
* <p>The test class must be annotated with <tt>@RunWith(JQF.class)</tt>
* and the test method must be annotated with <tt>@Fuzz</tt>.</p>
*
* <p>Once this method is invoked, the guided fuzzing loop runs continuously
* until the guidance instance decides to stop by returning <tt>false</tt>
* for {@link Guidance#hasInput()}. Until the fuzzing stops, this method
* cannot be invoked again (i.e. at most one guided fuzzing can be running
* at any time in a single JVM instance).</p>
*
* @param testClass the test class containing the test method
* @param testMethod the test method to execute in the fuzzing loop
* @param guidance the fuzzing guidance
* @param out an output stream to log Junit messages
* @throws IllegalStateException if a guided fuzzing run is currently executing
* @return the Junit-style test result
*/
public synchronized static Result run(Class<?> testClass, String testMethod,
Guidance guidance, PrintStream out) throws IllegalStateException {
// Ensure that the class uses the right test runner
RunWith annotation = testClass.getAnnotation(RunWith.class);
if (annotation == null || !annotation.value().equals(JQF.class)) {
throw new IllegalArgumentException(testClass.getName() + " is not annotated with @RunWith(JQF.class)");
}
// Set the static guided instance
setGuidance(guidance);
// Register callback
SingleSnoop.setCallbackGenerator(guidance::generateCallBack);
// Create a JUnit Request
Request testRequest = Request.method(testClass, testMethod);
// Instantiate a runner (may return an error)
Runner testRunner = testRequest.getRunner();
// Start tracing for the test method
SingleSnoop.startSnooping(testClass.getName() + "#" + testMethod);
// Run the test and make sure to de-register the guidance before returning
try {
JUnitCore junit = new JUnitCore();
if (out != null) {
junit.addListener(new TextListener(out));
}
return junit.run(testRunner);
} finally {
unsetGuidance();
}
}
/**
* args[0] test class
* args[1] test method (optional)
*
* @param args
*/
public static void main(String[] args) {
if (args.length < 1 || args.length > 2) {
System.err.println("Usage: java -cp .:JUnitRunner-0.0.1-SNAPSHOT.jar:<project cp> uk.ac.shef.JUnitRunner <full test class name> [test method name]");
System.exit(-1);
}
Class<?> clazz = null;
try {
clazz = Class.forName(args[0], false, JUnitRunner.class.getClassLoader());
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.exit(-1);
}
Request request = null;
if (args.length == 1) {
request = Request.aClass(clazz);
} else if (args.length == 2) {
request = Request.method(clazz, args[1]);
}
JUnitListener listener = new JUnitListener();
JUnitCore runner = new JUnitCore();
runner.addListener(listener);
runner.run(request); // run test method
System.exit(0);
}
private static void runTest(String test) {
try {
String[] classAndMethod = test.split("#");
System.out.println(test);
Request request = Request.method(Class.forName(classAndMethod[0]), classAndMethod[1]);
JUnitCore junit = new JUnitCore();
junit.addListener(new TextListener(System.out));
junit.run(request);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
@Override
public Result call() throws Exception {
JUnitCore runner = new JUnitCore();
runner.addListener(listener);
Request request = Request.method(testClassFromCustomClassLoader(), testCaseName());
try {
return runner.run(request);
} catch (Throwable e) {
throw new RuntimeException(e);
}
}
@Override
public Result call() throws Exception {
JUnitCore runner = new JUnitCore();
runner.addListener(listener);
Request request = Request.method(testClassFromCustomClassLoader(), testCaseName());
return runner.run(request);
}
@Test
public void accessPublicMethodFromDifferentClassloader() throws ClassNotFoundException {
String qualifiedName = "test.dynamic.compiler.HelloWorld";
String qualifiedTestName = "test.dynamic.compiler.HelloWorldTest";
String code =
"package test.dynamic.compiler;" +
"public class HelloWorld {" +
" @Override" +
" public String toString() {" +
" return \"Hello World!\";" +
" }" +
"}";
String testCode =
"package test.dynamic.compiler;" +
"import org.junit.Test;" +
"import static org.junit.Assert.assertEquals;" +
"public class HelloWorldTest {" +
" @Test" +
" public void toStringTest() {" +
" assertEquals(\"Hello World!\", new HelloWorld().toString());" +
" }" +
"}";
ClassLoader parentLoader = BytecodeClassLoaderBuilder.loaderFor(qualifiedName, code);
Map<String, String> sources = adHocMap(asList(qualifiedName, qualifiedTestName), asList(code, testCode));
ClassLoader loader = BytecodeClassLoaderBuilder.loaderFor(sources, parentLoader);
Class<?> testClass = loader.loadClass(qualifiedTestName);
Class<?> theClass = loader.loadClass(qualifiedName);
assertFalse(parentLoader == loader);
assertTrue(loader == theClass.getClassLoader());
assertTrue(loader == testClass.getClassLoader());
JUnitCore junit = new JUnitCore();
Request request = Request.method(testClass, "toStringTest");
Result result = junit.run(request);
assertTrue(result.wasSuccessful());
}
@Test
public void accessProtectedMethodFromSameClassloaderAndPackage() throws ClassNotFoundException {
String qualifiedName = "test.dynamic.compiler.HelloWorld";
String qualifiedTestName = "test.dynamic.compiler.HelloWorldTest";
String code =
"package test.dynamic.compiler;" +
"public class HelloWorld {" +
" protected String message() {" +
" return \"Hello World!\";" +
" }" +
"}";
String testCode =
"package test.dynamic.compiler;" +
"import org.junit.Test;" +
"import static org.junit.Assert.assertEquals;" +
"public class HelloWorldTest {" +
" @Test" +
" public void protectedMethodTest() {" +
" assertEquals(\"Hello World!\", new HelloWorld().message());" +
" }" +
"}";
Map<String, String> sources = adHocMap(asList(qualifiedName, qualifiedTestName), asList(code, testCode));
ClassLoader loader = BytecodeClassLoaderBuilder.loaderFor(sources);
Class<?> testClass = loader.loadClass(qualifiedTestName);
Class<?> theClass = loader.loadClass(qualifiedName);
assertTrue(loader == theClass.getClassLoader());
assertTrue(loader == testClass.getClassLoader());
JUnitCore junit = new JUnitCore();
Request request = Request.method(testClass, "protectedMethodTest");
Result result = junit.run(request);
assertTrue(result.wasSuccessful());
}
@Test
public void accessProtectedMethodFromDifferentClassloaderButSamePackageName() throws ClassNotFoundException {
String qualifiedName = "test.dynamic.compiler.HelloWorld";
String qualifiedTestName = "test.dynamic.compiler.HelloWorldTest";
String code =
"package test.dynamic.compiler;" +
"public class HelloWorld {" +
" protected String message() {" +
" return \"Hello World!\";" +
" }" +
"}";
String testCode =
"package test.dynamic.compiler;" +
"import org.junit.Test;" +
"import static org.junit.Assert.assertEquals;" +
"public class HelloWorldTest {" +
" @Test" +
" public void protectedMethodTest() {" +
" assertEquals(\"Hello World!\", new HelloWorld().message());" +
" }" +
"}";
Map<String, String> sources = adHocMap(asList(qualifiedName, qualifiedTestName), asList(code, testCode));
ClassLoader parentLoader = BytecodeClassLoaderBuilder.loaderFor(qualifiedName, code);
ClassLoader loader = BytecodeClassLoaderBuilder.loaderFor(sources, parentLoader);
Class<?> testClass = loader.loadClass(qualifiedTestName);
Class<?> theClass = loader.loadClass(qualifiedName);
assertFalse(parentLoader == loader);
assertTrue(loader == theClass.getClassLoader());
assertTrue(loader == testClass.getClassLoader());
JUnitCore junit = new JUnitCore();
Request request = Request.method(testClass, "protectedMethodTest");
Result result = junit.run(request);
assertTrue(result.wasSuccessful());
}
public static List<Failure> runTest(String fullQualifiedName, String testCaseName, String[] classpath) throws MalformedURLException, ClassNotFoundException {
ClassLoader classLoader = new URLClassLoader(
arrayStringToArrayUrl.apply(classpath),
ClassLoader.getSystemClassLoader()
);
Request request = Request.method(classLoader.loadClass(fullQualifiedName), testCaseName);
Runner runner = request.getRunner();
RunNotifier fNotifier = new RunNotifier();
final TestListener listener = new TestListener();
fNotifier.addFirstListener(listener);
fNotifier.fireTestRunStarted(runner.getDescription());
runner.run(fNotifier);
return listener.getTestFails();
}
@Override
public Result call() throws Exception {
JUnitCore runner = new JUnitCore();
runner.addListener(listener);
Request request = Request.method(testClassFromCustomClassLoader(), testCaseName());
try {
return runner.run(request);
} catch (Throwable e) {
throw new RuntimeException(e);
}
}
@Override
public Result call() throws Exception {
JUnitCore runner = new JUnitCore();
runner.addListener(listener);
Request request = Request.method(testClassFromCustomClassLoader(), testCaseName());
return runner.run(request);
}
private static void runTest(String test) {
try {
String[] classAndMethod = test.split("#");
System.out.println(test);
Request request = Request.method(Class.forName(classAndMethod[0]), classAndMethod[1]);
JUnitCore junit = new JUnitCore();
junit.addListener(new TextListener(System.out));
junit.run(request);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
@Test
public void test() {
Request request = Request.method(testMethod.getDeclaringClass(), testMethod.getName());
Result result = new JUnitCore().run(request);
if (result.getIgnoreCount() > 0)
throw new AssumptionViolatedException("Test " + testMethod.getDeclaringClass()
+ "." + testMethod.getName() + " were ignored");
if (result.getFailureCount() > 0) {
Assert.fail(result.getFailures().toString());
}
}
public static void main(String[] args) throws ClassNotFoundException {
Request testCase = Request.method(Class.forName(args[0]), args[1]);
JUnitCore core = new JUnitCore();
Result result = core.run(testCase);
for(Failure f: result.getFailures()) {
System.out.println(f.getMessage());
f.getException().printStackTrace();
}
System.exit(result.wasSuccessful() ? 0 : 1);
}
@Override
public TestIssue executeTestMethod(Class<?> testClass, String methodName) {
Request junitRequestOfMethod = Request.method(testClass, methodName);
Result testResult = new JUnitCore().run(junitRequestOfMethod);
List<Failure> jUnit4Failures = testResult.getFailures();
List<Throwable> jUnit4failuresAsThrowables = convertJUnit4FailuresToThrowables(jUnit4Failures);
return TestIssue.buildInNewJvmFrom(jUnit4failuresAsThrowables);
}