Search in sources :

Example 1 with StateMaster

use of org.mockito.StateMaster in project mockito by mockito.

the class InvalidStateDetectionTest method detectsAndCleansUp.

private void detectsAndCleansUp(DetectsInvalidState detector, Class<?> expected) {
    try {
        detector.detect(mock);
        fail("Should throw an exception");
    } catch (Exception e) {
        assertEquals(expected, e.getClass());
    }
    //Make sure state is cleaned up
    new StateMaster().validate();
}
Also used : StateMaster(org.mockito.StateMaster) UnfinishedStubbingException(org.mockito.exceptions.misusing.UnfinishedStubbingException) UnfinishedVerificationException(org.mockito.exceptions.misusing.UnfinishedVerificationException) InvalidUseOfMatchersException(org.mockito.exceptions.misusing.InvalidUseOfMatchersException)

Example 2 with StateMaster

use of org.mockito.StateMaster in project mockito by mockito.

the class InvalidUseOfMatchersTest method should_mention_matcher_when_misuse_detected.

@Test
public void should_mention_matcher_when_misuse_detected() {
    // Given
    // When
    Result run = new JUnitCore().run(ObjectMatcherMisuseOnPrimitiveSite.class);
    // Then
    assertThat(run.getFailures()).hasSize(2);
    assertThat(run.getFailures().get(0).getException()).isInstanceOf(NullPointerException.class).hasMessage(null);
    assertThat(run.getFailures().get(1).getException()).isInstanceOf(InvalidUseOfMatchersException.class).hasMessageContaining("primitive alternatives");
    new StateMaster().reset();
}
Also used : InvalidUseOfMatchersException(org.mockito.exceptions.misusing.InvalidUseOfMatchersException) JUnitCore(org.junit.runner.JUnitCore) StateMaster(org.mockito.StateMaster) Result(org.junit.runner.Result) Test(org.junit.Test)

Example 3 with StateMaster

use of org.mockito.StateMaster in project mockito by mockito.

the class TestBase method cleanUpConfigInAnyCase.

@After
public void cleanUpConfigInAnyCase() {
    ConfigurationAccess.getConfig().overrideCleansStackTrace(false);
    ConfigurationAccess.getConfig().overrideDefaultAnswer(null);
    StateMaster state = new StateMaster();
    //catch any invalid state left over after test case run
    //this way we can catch early if some Mockito operations leave weird state afterwards
    state.validate();
    //reset the state, especially, reset any ongoing stubbing for correct error messages of tests that assert unhappy paths
    state.reset();
}
Also used : StateMaster(org.mockito.StateMaster) After(org.junit.After)

Aggregations

StateMaster (org.mockito.StateMaster)3 InvalidUseOfMatchersException (org.mockito.exceptions.misusing.InvalidUseOfMatchersException)2 After (org.junit.After)1 Test (org.junit.Test)1 JUnitCore (org.junit.runner.JUnitCore)1 Result (org.junit.runner.Result)1 UnfinishedStubbingException (org.mockito.exceptions.misusing.UnfinishedStubbingException)1 UnfinishedVerificationException (org.mockito.exceptions.misusing.UnfinishedVerificationException)1