How to enable debug on my JUnit through Gradle test task

前端 未结 4 1457
日久生厌
日久生厌 2020-12-23 20:10

I get into trouble while I try to run my JUnit test through gradle test task. While I run test in eclipse directly with Run As -> JUnit test, everything is ok, test succeeds

相关标签:
4条回答
  • 2020-12-23 20:24

    To debug tests the following argument should be used: --debug-jvm

    For example: gradle test --debug-jvm
    Gradle will suspend execution right before running tests and wait for debugger connection on port 5005.

    For executing only specific tests see https://docs.gradle.org/current/userguide/java_testing.html#simple_name_pattern

    0 讨论(0)
  • 2020-12-23 20:27

    I'm on 4.6 (gradle) and I'm able to debug my tests when I have this in my build.gradle file:

    test {
        debug true
    }
    

    Link - https://docs.gradle.org/4.6/userguide/userguide_single.html#sec:java_test

    0 讨论(0)
  • 2020-12-23 20:28

    As explained under 23.12. Test in the Gradle User Guide, executing gradle test -Dtest.single=MyTestClass -Dtest.debug will suspend the test JVM upon start, and allows to connect an external debugger (such as the Eclipse debugger) on port 5005.

    0 讨论(0)
  • 2020-12-23 20:45

    Putting this here as --debug-jvm did not work for me, I was able to do this by setting:

     org.gradle.daemon=true
     org.gradle.jvmargs=... -agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=10999
    

    in

     ~/.gradle/gradle.properties
    

    But when I connect with eclipse debugger for the project none of the breakpoints I've set compile/trigger... I am connected via the debugger, I can see action in the Debug view, whenever I run gradle test from command line, like new threads starting/stopping, but can't get breakpoints to trigger, trying to resolve this now...

    Fyi to stop deamon run gradle --stop

    Other solution

    Leaving above as reference, this worked for triggering break points in tests, I turned off deamon as I could not get it to work properly:

    Using directions from this article: http://blogs.steeplesoft.com/posts/2013/gradle-tip-attaching-a-debugger.html

    test {        
        if (System.getProperty('DEBUG', 'false') == 'true') {
            jvmArgs '-Xdebug',
                '-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=10999'
        }
    }
    

    Executed via gradle test -DDEBUG=true

    Solution when using the JUnit Platform Gradle plugin

    The solution above won't work when using org.junit.platform.gradle.plugin.

    Instead it should be replaced by:

    junitPlatformTest {        
        if (System.getProperty('DEBUG', 'false') == 'true') {
            jvmArgs '-Xdebug',
                '-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=10999'
        }
    }
    
    0 讨论(0)
提交回复
热议问题