Wojciech Kumoń
Wojciech Kumoń

Reputation: 325

Maven failure on spring boot test (junit 5)

Is it possible to run spring boot tests with junit 5 using maven? I'm using spring boot 2.0.0.M3, junit 5.0.0-M6, maven 3.5.0. Other junit5 tests (without spring context) works.

There is simple controller:

@Controller
public class HomeController {
    @GetMapping("/")
    String home() {
        return "home";
    }
}

and test:

@ExtendWith(SpringExtension.class)
@WebMvcTest(HomeController.class)
@Import(SecurityConfig.class)
class HomeControllerTest {

    @Autowired
    private MockMvc mvc;

    @Test
    void shouldReturnHomeTemplate() throws Exception {
        this.mvc.perform(get("/").accept(MediaType.TEXT_HTML))
            .andExpect(status().isOk())
            .andExpect(content().string(startsWith("<!DOCTYPE html>")));
    }
}

Everything works when I run it using intellij, but maven build ends with failure:

[WARNING] Corrupted stdin stream in forked JVM 1. See the dump file somePath/target/surefire-reports/2017-07-28T13-50-15_071-jvmRun1.dumpstream

--debug flag shows:

java.lang.OutOfMemoryError: Java heap space

Inside 2017-07-28T13-50-15_071-jvmRun1.dumpstream I can find ~100 same exceptions (one per spring log):

Corrupted stdin stream in forked JVM 1. Stream ' 13:50:15.914 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating CacheAwareContextLoaderDelegate from class [org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate]'. java.lang.IllegalArgumentException: Stream stdin corrupted. Expected comma after third character in command ' 13:50:15.914 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating CacheAwareContextLoaderDelegate from class [org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate]'. at org.apache.maven.plugin.surefire.booterclient.output.ForkClient$OperationalData.(ForkClient.java:469) at org.apache.maven.plugin.surefire.booterclient.output.ForkClient.processLine(ForkClient.java:191) at org.apache.maven.plugin.surefire.booterclient.output.ForkClient.consumeLine(ForkClient.java:158) at org.apache.maven.plugin.surefire.booterclient.output.ThreadedStreamConsumer$Pumper.run(ThreadedStreamConsumer.java:87) at java.lang.Thread.run(Thread.java:745)

Maven surefire plugin:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.20</version>
    <dependencies>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-surefire-provider</artifactId>
            <version>1.0.0-M6</version>
        </dependency>
    </dependencies>
</plugin>

Upvotes: 6

Views: 10191

Answers (2)

Rik Schaaf
Rik Schaaf

Reputation: 37

This problem is caused by the interaction between the maven-surefire-plugin an JUnit5's junit-vintage-engine, that writes to System.out before Surefire is able to handle this.

It was fixed in maven-surefire-plugin version 2.22.2: https://blogs.apache.org/maven/entry/apache-maven-surefire-plugin-version2

Upvotes: 1

Adam
Adam

Reputation: 5445

IntelliJ bundles JUnit5 so IntelliJ and Maven are probably using different versions.

See the JUnit5 docs about IntelliJ

Depending on which version of IntelliJ you are running, it is probably executing an earlier JUnit5 milestone release than what maven is using.

If you're on 2017.2, you could try switching your JUnit5 dependency in your pom.xml back to M4 and see if that solves the incompatibility.

Also try specifying the junit-jupiter-engine:5.0.0-M4 in the plugin dependency in your pom.xml as well as junit-platform-surefire-plugin.

I am using 5.0.0-M5 successfully this way.

[UPDATE 2017-08-10]: regarding your comments, I am using spring boot 1.5.4.RELEASE so I can't do a like-for-like comparison.

You don't say what version of IntelliJ you are using - assuming the tests are still working under IntelliJ, that version configuration is your target, presumably. Look in the maven projects view or debug the test output to get the classpath and see what versions are in use.

Spring is probably managing a lot of the versions for you. Are you using the spring-boot parent pom, or depending on the spring-boot-starter dependencies?

To find out what is going on and which spring dependency is pulling in which JUnit5 dependency that is breaking, run

mvn dependency:tree

and go through the list of dependencies and versions to see exactly what you have under maven.

You may find the versions you think you have specified are being overridden, and you have to put exclusions in the pom to stop that.

[UPDATE 2]: just a quick change - try putting the junit-jupiter-engine dependency in your maven-surefire-plugin dependency block next to junit-platform-surefire-provider:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19.1</version>
            <dependencies>
                <dependency>
                    <groupId>org.junit.platform</groupId>
                    <artifactId>junit-platform-surefire-provider
                    </artifactId>
                    <version>1.0.0-M5</version>
                </dependency>
                <dependency>
                    <groupId>org.junit.jupiter</groupId>
                    <artifactId>junit-jupiter-engine</artifactId>
                    <version>5.0.0-M5</version>
                </dependency>
            </dependencies>
        </plugin>

Note I'm also not yet on version 2.20 (and I'm still on 5.0.0-M5) - sorry but I don't have time to try it this week either.

You could also see if setting the maven-surefire-plugin config forkCount=0 to see if that gives you a better exception message.

You could strip down your project to the bare minimum and open Jira issues with Maven and Spring testing. I changed the tags on your question to incluide spring-boot so one of their guys might take notice. Or might not, depending on your luck - I find they are more likely to react if you tweet your stackoverflow url to @springboot.

Upvotes: 2

Related Questions