Details

Recommendation type: Additional threads

Configurable: Yes

Filter category: Unstable test environment

Description

Avoid writing test cases with side effects. Additional threads may change the state of your system and cause random test failures.

Example

@Test
public void testChangeState() throws Exception {
    new Thread(new Runnable() {

        public void run() {
            System.out.println("foo");
        }

    }).start();

    State initialState = new State("state", 2);
    // Given
    TrackableMethodExample underTest = new TrackableMethodExample(initialState);

    // When
    State newState = new State("state changed", 10);
    ResultStatus result = underTest.changeState(newState);

    Assert.assertNotNull(result);
}
  • No labels