Java Code Examples for org.junit.runner.Request#method()

The following examples show how to use org.junit.runner.Request#method() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SingleJUnitTestRunner.java    From io with Apache License 2.0 6 votes vote down vote up
/**
 * .
 * @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);
}
 
Example 2
Source File: DynamicClassCompilerTest.java    From nopol with GNU General Public License v2.0 5 votes vote down vote up
@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());
}
 
Example 3
Source File: TestCaseRunner.java    From s3mper with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 4
Source File: TestRunner.java    From at.info-knowledge-base with MIT License 5 votes vote down vote up
@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());
    }
}
 
Example 5
Source File: MethodTestRunner.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
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();
	}
}
 
Example 6
Source File: JUnitSingleTestResultRunner.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Result call() throws Exception {
	JUnitCore runner = new JUnitCore();
	runner.addListener(listener);
	Request request = Request.method(testClassFromCustomClassLoader(), testCaseName());
	return runner.run(request);
}
 
Example 7
Source File: JUnitSingleTestRunner.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@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);
    }
}
 
Example 8
Source File: TestRunner.java    From spoon-examples with GNU General Public License v2.0 5 votes vote down vote up
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();
}
 
Example 9
Source File: DynamicClassCompilerTest.java    From nopol with GNU General Public License v2.0 5 votes vote down vote up
@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());
}
 
Example 10
Source File: DynamicClassCompilerTest.java    From nopol with GNU General Public License v2.0 5 votes vote down vote up
@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());
}
 
Example 11
Source File: JUnitSingleTestResultRunner.java    From nopol with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Result call() throws Exception {
	JUnitCore runner = new JUnitCore();
	runner.addListener(listener);
	Request request = Request.method(testClassFromCustomClassLoader(), testCaseName());
	return runner.run(request);
}
 
Example 12
Source File: JUnitSingleTestRunner.java    From nopol with GNU General Public License v2.0 5 votes vote down vote up
@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);
    }
}
 
Example 13
Source File: MethodTestRunner.java    From nopol with GNU General Public License v2.0 5 votes vote down vote up
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();
    }
}
 
Example 14
Source File: JUnitRunner.java    From SimFix with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 15
Source File: GuidedFuzzing.java    From JQF with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * 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();
    }



}
 
Example 16
Source File: JUnit_Test.java    From The-Kraken-Pathfinding with MIT License 5 votes vote down vote up
/**
 * 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);
	}
}
 
Example 17
Source File: JUnit4TestRunner.java    From quickperf with Apache License 2.0 3 votes vote down vote up
@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);

}