Search in sources :

Example 36 with Request

use of org.junit.runner.Request in project bazel by bazelbuild.

the class JUnit4RunnerTest method getExpectedOutput.

private ByteArrayOutputStream getExpectedOutput(Class<?> testClass) {
    JUnitCore core = new JUnitCore();
    ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
    PrintStream printStream = new PrintStream(byteStream);
    printStream.println("JUnit4 Test Runner");
    RunListener listener = new TextListener(printStream);
    core.addListener(listener);
    Request request = Request.classWithoutSuiteMethod(testClass);
    core.run(request);
    printStream.close();
    return byteStream;
}
Also used : PrintStream(java.io.PrintStream) JUnitCore(org.junit.runner.JUnitCore) Request(org.junit.runner.Request) TextListener(org.junit.internal.TextListener) ByteArrayOutputStream(java.io.ByteArrayOutputStream) RunListener(org.junit.runner.notification.RunListener)

Example 37 with Request

use of org.junit.runner.Request in project bazel by bazelbuild.

the class TestRunner method main.

public static void main(String[] args) throws ClassNotFoundException {
    if (args.length == 0) {
        throw new IllegalArgumentException("Must specify at least one argument (source files of the tests to run)!");
    }
    JUnitCore junitCore = new JUnitCore();
    junitCore.addListener(new TestListener());
    SecurityManager previousSecurityManager = setGoogleTestSecurityManager();
    Request request = createRequest(args);
    Result result = junitCore.run(request);
    restorePreviousSecurityManager(previousSecurityManager);
    System.exit(result.wasSuccessful() ? 0 : 1);
}
Also used : JUnitCore(org.junit.runner.JUnitCore) GoogleTestSecurityManager(com.google.testing.junit.runner.util.GoogleTestSecurityManager) Request(org.junit.runner.Request) Result(org.junit.runner.Result)

Example 38 with Request

use of org.junit.runner.Request in project bazel by bazelbuild.

the class CancellableRequestFactoryTest method testCancelRunBeforeStarting.

@Test
public void testCancelRunBeforeStarting() throws Exception {
    final AtomicBoolean testRan = new AtomicBoolean(false);
    // A runner that should never run its test
    FakeRunner runner = new FakeRunner("shouldNotRun", new Runnable() {

        @Override
        public void run() {
            testRan.set(true);
        }
    });
    Request request = cancellableRequestFactory.createRequest(Request.runner(runner));
    cancellableRequestFactory.cancelRun();
    JUnitCore core = new JUnitCore();
    try {
        core.run(request);
        fail("exception expected");
    } catch (RuntimeException e) {
        assertEquals("Test run interrupted", e.getMessage());
        assertTrue(e.getCause() instanceof StoppedByUserException);
    }
    assertFalse(testRan.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) JUnitCore(org.junit.runner.JUnitCore) Request(org.junit.runner.Request) StoppedByUserException(org.junit.runner.notification.StoppedByUserException) Test(org.junit.Test)

Example 39 with Request

use of org.junit.runner.Request in project bazel by bazelbuild.

the class CancellableRequestFactoryTest method testNormalRun.

@Test
public void testNormalRun() {
    final AtomicBoolean testRan = new AtomicBoolean(false);
    // A runner that should run its test
    FakeRunner runner = new FakeRunner("shouldRun", new Runnable() {

        @Override
        public void run() {
            testRan.set(true);
        }
    });
    Request request = cancellableRequestFactory.createRequest(Request.runner(runner));
    JUnitCore core = new JUnitCore();
    Result result = core.run(request);
    assertTrue(testRan.get());
    assertEquals(1, result.getRunCount());
    assertEquals(0, result.getFailureCount());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) JUnitCore(org.junit.runner.JUnitCore) Request(org.junit.runner.Request) Result(org.junit.runner.Result) Test(org.junit.Test)

Example 40 with Request

use of org.junit.runner.Request in project bazel by bazelbuild.

the class JUnit4TestXmlListenerTest method failuresAreReported.

@Test
public void failuresAreReported() throws Exception {
    TestSuiteModelSupplier mockModelSupplier = mock(TestSuiteModelSupplier.class);
    TestSuiteModel mockModel = mock(TestSuiteModel.class);
    CancellableRequestFactory mockRequestFactory = mock(CancellableRequestFactory.class);
    OutputStream mockXmlStream = mock(OutputStream.class);
    JUnit4TestXmlListener listener = new JUnit4TestXmlListener(mockModelSupplier, mockRequestFactory, fakeSignalHandlers, mockXmlStream, errPrintStream);
    Request request = Request.classWithoutSuiteMethod(FailingTest.class);
    Description suiteDescription = request.getRunner().getDescription();
    Description testDescription = suiteDescription.getChildren().get(0);
    when(mockModelSupplier.get()).thenReturn(mockModel);
    JUnitCore core = new JUnitCore();
    core.addListener(listener);
    core.run(request);
    assertEquals("no output to stderr expected", 0, errStream.size());
    InOrder inOrder = inOrder(mockModel);
    inOrder.verify(mockModel).testFailure(eq(testDescription), any(Throwable.class));
    inOrder.verify(mockModel).writeAsXml(mockXmlStream);
    verify(mockRequestFactory, never()).cancelRun();
}
Also used : Description(org.junit.runner.Description) InOrder(org.mockito.InOrder) JUnitCore(org.junit.runner.JUnitCore) TestSuiteModel(com.google.testing.junit.runner.model.TestSuiteModel) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) Request(org.junit.runner.Request) Test(org.junit.Test)

Aggregations

Request (org.junit.runner.Request)56 Test (org.junit.Test)37 JUnitCore (org.junit.runner.JUnitCore)33 Result (org.junit.runner.Result)23 Description (org.junit.runner.Description)20 Runner (org.junit.runner.Runner)9 TestSuiteModel (com.google.testing.junit.runner.model.TestSuiteModel)8 PrintableResult.testResult (org.junit.experimental.results.PrintableResult.testResult)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 Filter (org.junit.runner.manipulation.Filter)6 Failure (org.junit.runner.notification.Failure)6 RunListener (org.junit.runner.notification.RunListener)6 OutputStream (java.io.OutputStream)5 CategoryFilter (org.junit.experimental.categories.Categories.CategoryFilter)5 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 InOrder (org.mockito.InOrder)4 Computer (org.junit.runner.Computer)3 RunNotifier (org.junit.runner.notification.RunNotifier)3