'Intellij-idea Debugger disconnected when debugging maven test

I have followed this link to debug maven test via Intellij Idea : http://www.grygoriy.com/2012/01/how-to-debug-tests-maven-test-via.html

When reaching the third step and starting debugging, It's connected but quickly disconnected and isn't stopped in breakpoints. I had in Intellij :

Connected to the target VM, address: 'localhost:5005', transport: 'socket'

Disconnected from the target VM, address: 'localhost:5005', transport: 'socket'

Any idea ?



Solution 1:[1]

The only thing that prevents Idea from debugging Maven's goals is forking.

Plugins such surefire and spring-boot are using forking by default.

So, when you start debuging maven goal from IDEA it connects to maven, but process that you are really want to debug distincts from maven process, so it doesn't connected.

To prevent such behavior in surefire plugin you should read this article: http://maven.apache.org/surefire/maven-surefire-plugin/examples/fork-options-and-parallel-execution.html

In short:

If you use old surefire:

<configuration>
    <forkMode>never</forkMode>
</configuration>

In new surefire:

<configuration>
    <forkCount>0</forkCount>
</configuration>

But it's not much clear:

  1. in case of CI (i wish you are using CI tools) you are not have prevent forking while it's much slow
  2. if you ship your project to others - they will not be happy if some of modules behave in not default way

So if you want to please CI, IDEA, co-developers and yourself you should provide more smart way to allow debugging you build.

My suggestion:

  1. default behavior is forked because build process is very often thing, while debugging it - is exception

  2. debugger behavior is isolated with simple to use "switch on"

My variant:

<properties>
    <test.forkCount>1</test.forkCount>
</properties>
<profiles>
      <profile>
            <id>debug</id>
             <properties>
                     <test.forkCount>0</test.forkCount>    
             </properties>
        </profile>
 </profiles>
 <build>
      <plugins>
           <plugin>
                <!-- surefire -->
                <configuration>
                    <forkCount>${test.forkCount}</forkCount>
                </configuration>
            </plugin>
        </plugins>
</build>

So, in IDEA you just require to create named Run configuration with goal test and include debug to profile list.

But in other contexts - maven behaves still by default.

Where is addition profit - you can incapsulate whole debug behavior in single profile.

For example in my real project debug profile:

  1. swith off forking on spring-boot:run
  2. switch off JaCoCo coverage (it requires forking on surefire)
  3. keep building Docker images locally but prevents pushing to registry
  4. keep full packaging process but prevents publication to nexus
  5. redirects SOAP UI functional tests to specital URL for local debugging
  6. redirects DBCONFIG to docker-based Postgres that is "always empty"
  7. downgrades loglevel for log4j to DEBUG

So if I use mvn <any> -P debug i'm sure that my environment and process is really debug

But if i ran mvn deploy on CI - i will get full stack of my building process.

Solution 2:[2]

This can also happen if e.g. the annotation BeforeAll is incorrectly used. IntelliJ swallows problem during the test initialization.

In my case the BeforeAll method was not static:

Incorrect:

    @BeforeAll
    private void beforeAll() {
        }

It can also happen if the BeforeAll method is failing and the test is not started. Using mvn verify should print the error message in these cases.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 comdiv
Solution 2 k_o_