【问题标题】:Maven vs. Gradle (scope) / spring-boot JUnit5Maven vs. Gradle(范围)/spring-boot JUnit5
【发布时间】:2018-03-09 22:00:27
【问题描述】:

我目前正在尝试为我的 spring-boot 休息服务实施 JUnit5 测试。我还找到了一个很有前途的教程,但我遇到了一个错误。

教程:https://howtoprogram.xyz/2017/09/12/junit-5-spring-boot-example/

与教程相比,我使用的是 Gradle。我的 gradle 看起来像这样:

dependencies {
    compile "com.google.code.gson:gson:2.8.0"
    compile("org.springframework.boot:spring-boot-starter-web") {
        exclude module: "spring-boot-starter-tomcat"
        exclude group: "com.fasterxml.jackson.core"
    }
    compile("org.springframework.boot:spring-boot-starter-jetty")

    compile("io.springfox:springfox-swagger2:2.7.0")

    // reroute default logs
    compile "ch.qos.logback:logback-classic:1.2.3"
    compile "ch.qos.logback:logback-core:1.2.3"
    // json logs:
    compile "net.logstash.logback:logstash-logback-encoder:4.9"

    // junit
    testCompile("org.junit.jupiter:junit-jupiter-api:5.0.0")
    testRuntime("org.junit.jupiter:junit-jupiter-engine:5.0.0")
    // junit for spring-boot
    testCompile("org.springframework.boot:spring-boot-starter-parent:1.5.7.RELEASE")
    testCompile("org.springframework.boot:spring-boot-starter-test")//:1.5.7.RELEASE")
    {
        // exclude JUnit 4 support
        exclude group: "junit"
    }
    testCompile("org.junit.platform:junit-platform-engine:1.0.0-M5")
    testCompile("org.junit.platform:junit-platform-launcher:1.0.0-M5")
}

我不知道如何将 Maven 脚本的测试应用于我的 gradle 脚本。

我在构建时也遇到了这些错误

/home/udev/prj/research/java-unittest/src/test/java/one/utest/HelloControllerTest.java:6: error: package org.springframework
.test.context.junit.jupiter does not exist
import org.springframework.test.context.junit.jupiter.SpringExtension;
                                                     ^
/home/udev/prj/research/java-unittest/src/test/java/one/utest/HelloControllerTest.java:9: error: cannot find symbol
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
 ^
  symbol: class SpringBootTest
/home/udev/prj/research/java-unittest/src/test/java/one/utest/HelloControllerTest.java:13: error: cannot find symbol
   private TestRestTemplate restTemplate;
           ^
  symbol:   class TestRestTemplate
  location: class HelloControllerTest
/home/udev/prj/research/java-unittest/src/test/java/one/utest/HelloControllerTest.java:8: error: cannot find symbol
@ExtendWith(SpringExtension.class)
            ^
  symbol: class SpringExtension
/home/udev/prj/research/java-unittest/src/test/java/one/utest/HelloControllerTest.java:12: error: cannot find symbol
   @Autowired
    ^
  symbol:   class Autowired
  location: class HelloControllerTest
5 errors
:compileTestJava FAILED

【问题讨论】:

    标签: spring-boot gradle junit


    【解决方案1】:

    您的教程谈到 spring-boot-starter-parent 版本 2.0.0.BUILD-SNAPSHOT,但您使用版本 1.5.7.RELEASE。在那个版本中,还没有 Junit 5 支持。

    我不确定您所说的 Maven script's test 是什么意思,但如果您指的是范围测试,那么在 Gradle 中您只需使用 testCompiletestRuntime 即可获得相同的效果。

    【讨论】:

    • 啊,我的错...当我看到 2.0 时,我以某种方式阅读了 1.2,虽然这是一个旧版本...无论如何,我无法获取版本 2.0.0.BUILD-SNAPSHOT有没有一种特殊的方法来获取具有 BUILD-SNAPSHOT 后缀的版本?是的,Maven 脚本的测试我的意思是范围 - 好的,现在很清楚“范围”的含义了。谢谢!
    • maven Central 上不允许快照发布,您需要定义这些构建可用的存储库。在projects.spring.io/spring-boot,您可以找到所需的存储库,即使是 Gradle 格式,这取决于您是要使用快照还是里程碑构建。
    猜你喜欢
    • 2020-02-06
    • 2014-11-05
    • 2019-11-25
    • 2021-08-11
    • 2018-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多