How to run JUnit tests with Gradle?

前端 未结 5 1178
野的像风
野的像风 2020-12-12 20:19

Currently I have the following build.gradle file:

apply plugin: \'java\'

sourceSets {
    main {
        java {
            srcDir \'src/model\'
           


        
相关标签:
5条回答
  • 2020-12-12 20:27

    If you created your project with Spring Initializr, everything should be configured correctly and all you need to do is run...

    ./gradlew clean test --info
    
    • Drop --info if you don't want to see test output.
    • Drop clean if you don't want to rerun tests that have already passed since the last change.

    Dependencies required in build.gradle for testing in Spring Boot...

    dependencies {
        compile('org.springframework.boot:spring-boot-starter')
        testCompile('org.springframework.boot:spring-boot-starter-test')
    }
    

    For some reason the test runner doesn't tell you this, but it produces an HTML report in build/reports/tests/test/index.html.

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

    If you set up your project with the default gradle package structure, i.e.:

    src/main/java
    src/main/resources
    src/test/java
    src/test/resources
    

    then you won't need to modify sourceSets to run your tests. Gradle will figure out that your test classes and resources are in src/test. You can then run as Oliver says above. One thing to note: Be careful when setting property files and running your test classes with both gradle and you IDE. I use Eclipse, and when running JUnit from it, Eclipse chooses one classpath (the bin directory) whereas gradle chooses another (the build directory). This can lead to confusion if you edit a resource file, and don't see your change reflected at test runtime.

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

    If you want to add a sourceSet for testing in addition to all the existing ones, within a module regardless of the active flavor:

    sourceSets {
        test {
            java.srcDirs += [
                    'src/customDir/test/kotlin'
            ]
            print(java.srcDirs)   // Clean
        }
    }
    

    Pay attention to the operator += and if you want to run integration tests change test to androidTest.

    GL

    0 讨论(0)
  • 2020-12-12 20:38

    testCompile is deprecated. Gradle 7 compatible:

    dependencies {
    ...
       testImplementation 'junit:junit:4.13'
    }
    

    and if you use the default folder structure (src/test/java/...) the test section is simply:

    test {
        useJUnit()
    }
    

    Finally:

    gradlew clean test
    

    Alos see: https://docs.gradle.org/current/userguide/java_testing.html

    0 讨论(0)
  • 2020-12-12 20:39

    How do I add a junit 4 dependency correctly?

    Assuming you're resolving against a standard Maven (or equivalent) repo:

    dependencies {
        ...
        testCompile "junit:junit:4.11"  // Or whatever version
    }
    

    Run those tests in the folders of tests/model?

    You define your test source set the same way:

    sourceSets {
        ...
    
        test {
            java {
                srcDirs = ["test/model"]  // Note @Peter's comment below
            }
        }
    }
    

    Then invoke the tests as:

    ./gradlew test
    

    EDIT: If you are using JUnit 5 instead, there are more steps to complete, you should follow this tutorial.

    0 讨论(0)
提交回复
热议问题