How to run JUnit tests with Gradle?

前端 未结 5 1195
野的像风
野的像风 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: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.

提交回复
热议问题