org.junit.runner.notification.StoppedByUserException Java Examples

The following examples show how to use org.junit.runner.notification.StoppedByUserException. 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: CancellableRequestFactory.java    From bazel with Apache License 2.0 6 votes vote down vote up
@Override
public void run(RunNotifier notifier) {
  currentNotifier = new ThreadSafeRunNotifier(notifier);
  if (cancellationRequest.get() != NOT_REQUESTED) {
    currentNotifier.pleaseStop();
  }
  if (cancellationRequest.get() == ORDERLY_STOP) {
    return;
  }

  try {
    delegate.run(currentNotifier);
  } catch (StoppedByUserException e) {
    if (cancellationRequest.get() == HARD_STOP) {
      throw new RuntimeException("Test run interrupted", e);
    } else if (cancellationRequest.get() == ORDERLY_STOP) {
      e.printStackTrace();
      return;
    }
    throw e;
  }
}
 
Example #2
Source File: JUnit4RunnerTest.java    From bazel with Apache License 2.0 6 votes vote down vote up
@Test
public void testInterruptedTest() throws Exception {
  config = createConfig();
  mockRunListener = mock(RunListener.class);
  JUnit4BazelMock component = createComponent(SampleSuite.class);
  JUnit4Runner runner = component.runner();
  final CancellableRequestFactory requestFactory = component.cancellableRequestFactory();

  Description testDescription = Description.createTestDescription(SamplePassingTest.class,
      "testThatAlwaysPasses");

  doAnswer(cancelTestRun(requestFactory))
      .when(mockRunListener).testStarted(testDescription);

  RuntimeException e = assertThrows(RuntimeException.class, () -> runner.run());
  assertThat(e).hasMessageThat().isEqualTo("Test run interrupted");
    assertWithMessage("Expected cause to be a StoppedByUserException")
        .that(e.getCause() instanceof StoppedByUserException)
        .isTrue();

    InOrder inOrder = inOrder(mockRunListener);
    inOrder.verify(mockRunListener).testRunStarted(any(Description.class));
    inOrder.verify(mockRunListener).testStarted(testDescription);
  inOrder.verify(mockRunListener).testFinished(testDescription);
}
 
Example #3
Source File: CancellableRequestFactoryTest.java    From bazel with Apache License 2.0 6 votes vote down vote up
@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();

  RuntimeException e = assertThrows(RuntimeException.class, () -> core.run(request));
  assertThat(e).hasMessageThat().isEqualTo("Test run interrupted");
  assertThat(e).hasCauseThat().isInstanceOf(StoppedByUserException.class);

  assertThat(testRan.get()).isFalse();
}
 
Example #4
Source File: JUnit4Runner.java    From seed with Mozilla Public License 2.0 6 votes vote down vote up
@Override
public void run(RunNotifier notifier) {
    try {
        if (launchMode == LaunchMode.PER_TEST_CLASS) {
            doStart();
        }

        super.run(notifier);

        if (launchMode == LaunchMode.PER_TEST_CLASS) {
            doStop();
        }
    } catch (StoppedByUserException e) {
        throw e;
    } catch (Throwable t) {
        notifyFailure(t, notifier);
    }
}
 
Example #5
Source File: TestRunner.java    From marathonv5 with Apache License 2.0 5 votes vote down vote up
private void doRunTest(Test testSuite) {
    mode = Mode.RUNNING;
    resetToolBar();
    startTime = System.currentTimeMillis();
    runnerThread = new Thread("TestRunner-Thread") {
        @Override
        public void run() {
            try {
                runner = new MarathonTestRunner();
                runner.addListener(runListener);
                runner.addListener(new AllureMarathonRunListener());
                @SuppressWarnings("unused")
                Result result = runner.run(testSuite);
                runFinished(testSuite);
            } catch (StoppedByUserException e) {
                abort();
            } finally {
                // always return control to UI
                if (interrupted()) {
                    abort();
                }
                MarathonTestCase.reset();
                runnerThread = null;
                System.gc();
            }
        }

        private void abort() {
            endTime = System.currentTimeMillis();
            Platform.runLater(() -> status.setText("Aborted after " + (endTime - startTime) / 1000 + " seconds"));
            mode = Mode.RESULTS;
            resetToolBar();
        }

    };
    createTestReportDir();
    runnerThread.start();
}
 
Example #6
Source File: RetryRunner.java    From java-sdk with Apache License 2.0 5 votes vote down vote up
@Override
public void run(final RunNotifier notifier) {
  EachTestNotifier testNotifier = new EachTestNotifier(notifier, getDescription());
  Statement statement = classBlock(notifier);
  try {
    statement.evaluate();
  } catch (AssumptionViolatedException ave) {
    testNotifier.fireTestIgnored();
  } catch (StoppedByUserException sbue) {
    throw sbue;
  } catch (Throwable t) {
    LOG.warning("Retry class: " + getDescription().getDisplayName());
    retry(testNotifier, statement, t, getDescription());
  }
}
 
Example #7
Source File: KurentoRunNotifier.java    From kurento-java with Apache License 2.0 5 votes vote down vote up
@Override
public void fireTestStarted(final Description description) throws StoppedByUserException {
  if (pleaseStop) {
    throw new StoppedByUserException();
  }
  new SafeNotifier() {
    @Override
    protected void notifyListener(RunListener each) throws Exception {
      each.testStarted(description);
    }
  }.run();
}
 
Example #8
Source File: AdaptingRunListener.java    From pitest with Apache License 2.0 5 votes vote down vote up
@Override
public void testStarted(final Description description) throws Exception {
  if (this.failed) {
    // If the JUnit test has been annotated with @BeforeClass or @AfterClass
    // need to force the exit after the first failure as tests will be run as
    // a block
    // rather than individually.
    // This is apparently the junit way.
    throw new StoppedByUserException();
  }
  this.rc.notifyStart(this.description);
}
 
Example #9
Source File: CancellableRequestFactory.java    From bazel with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}<p>
 *
 * The implementation is almost an exact copy of the version in
 * {@code RunNotifier} but is thread-safe.
 */
@Override
public void fireTestStarted(Description description) throws StoppedByUserException {
  if (stopRequested) {
    throw new StoppedByUserException();
  }
  getDelegate().fireTestStarted(description);
}
 
Example #10
Source File: AdaptingRunListenerTest.java    From pitest with Apache License 2.0 4 votes vote down vote up
@Test(expected = StoppedByUserException.class)
public void shouldRunStoppedByUserExceptionIfMoreTestsRunAfterAFailure()
    throws Exception {
  this.testee.testFailure(new Failure(this.junitDesc, this.throwable));
  this.testee.testStarted(this.junitDesc);
}
 
Example #11
Source File: RunNotifierWrapper.java    From bazel with Apache License 2.0 4 votes vote down vote up
@Override
public void fireTestStarted(Description description) throws StoppedByUserException {
  delegate.fireTestStarted(description);
}
 
Example #12
Source File: DelegateRunNotifier.java    From buck with Apache License 2.0 4 votes vote down vote up
@Override
public void fireTestStarted(Description description) throws StoppedByUserException {
  delegate.fireTestStarted(description);

  // Do not do apply the default timeout if the test has its own @Test(timeout).
  if (hasJunitTimeout(description)) {
    return;
  }

  // Schedule a timer that verifies that the test completed within the specified timeout.
  TimerTask task =
      new TimerTask() {
        @Override
        public void run() {
          synchronized (finishedTests) {
            // If the test already finished, then do nothing.
            if (finishedTests.contains(description)) {
              return;
            }

            hasTestThatExceededTimeout.set(true);

            // Should report the failure. The Exception is modeled after the one created by
            // org.junit.internal.runners.statements.FailOnTimeout#createTimeoutException(Thread).
            Exception exception =
                new Exception(
                    String.format(
                        "test timed out after %d milliseconds", defaultTestTimeoutMillis));
            Failure failure = new Failure(description, exception);
            fireTestFailure(failure);
            fireTestFinished(description);

            if (!finishedTests.contains(description)) {
              throw new IllegalStateException("fireTestFinished() should update finishedTests.");
            }

            onTestRunFinished();
          }
        }
      };
  timer.schedule(task, defaultTestTimeoutMillis);
}