John
John

Reputation: 464

Weird context behavior in tests

I'm trying to use SpringJUnit4ClassRunner in my test cases. In tests with only one @Test method, it works perfectly. But in tests with several @Test methods sometimes it works, sometimes I get NPE or IllegalStateException. I tried it without @DirtiesContext annotation and with different classMode modes.

And it only happens when I build maven project. When I run this test from IDE - everything is OK.

P.S. my context is stateless.

@RunWith(SpringJUnit4ClassRunner.class)
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
@ContextConfiguration(classes = {TestConfiguration.class})
@ActiveProfiles("dummy")
public class TestFieldMapping {

    @Autowired
    private ApplicationContext context;

    @Test
    public void test1() {
        context.getBean...
    }

    @Test
    public void test2() {
        context.getBean...
    }

    @Test
    public void test3() {
        context.getBean...
    }

}

Upvotes: 1

Views: 52

Answers (1)

John
John

Reputation: 464

Here is what I found:

SpringJUnit4ClassRunner is not compatible with multithread mode

https://jira.spring.io/browse/SPR-12421

I solved it by setting parallel=classes for sunfire

           <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <parallel>classes</parallel>
                    <reuseForks>true</reuseForks>
                    <threadCount>1</threadCount>
                </configuration>
            </plugin>

Upvotes: 1

Related Questions