source

spring-boot-starter-test with JUnit 5

factcode 2023. 3. 15. 19:57
반응형

spring-boot-starter-test with JUnit 5

「」를 사용합니다.spring-boot-starter-test2.0종속성이 되었습니다.2.0.6의 경우 JUnit 4의 종속성이 도입됩니다.★★★를 어떻게 사용할 수 있습니까?spring-boot-starter-test(계조) JUnit 5 신n JUnit 4 ( ( 。

Gradle로부터의 의존관계 출력의 일부를 다음에 나타냅니다.

+--- org.springframework.boot:spring-boot-starter-test -> 2.0.5.RELEASE
|    +--- org.springframework.boot:spring-boot-starter:2.0.5.RELEASE (*)
|    +--- org.springframework.boot:spring-boot-test:2.0.5.RELEASE
|    |    \--- org.springframework.boot:spring-boot:2.0.5.RELEASE (*)
|    +--- org.springframework.boot:spring-boot-test-autoconfigure:2.0.5.RELEASE
|    |    +--- org.springframework.boot:spring-boot-test:2.0.5.RELEASE (*)
|    |    \--- org.springframework.boot:spring-boot-autoconfigure:2.0.5.RELEASE (*)
|    +--- com.jayway.jsonpath:json-path:2.4.0
|    |    +--- net.minidev:json-smart:2.3
|    |    |    \--- net.minidev:accessors-smart:1.2
|    |    |         \--- org.ow2.asm:asm:5.0.4
|    |    \--- org.slf4j:slf4j-api:1.7.25
|    +--- junit:junit:4.12
|    |    \--- org.hamcrest:hamcrest-core:1.3


다음은 build.gradle 파일입니다.

buildscript {
    ext {
        springBootVersion = '2.0.6.RELEASE'
    rootGradleDir = "${rootProject.rootDir}/gradle"
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

apply from: "${rootGradleDir}/staticCodeAnalysis.gradle"

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

test {
  useJUnitPlatform()
}

dependencies {
    implementation('org.springframework.boot:spring-boot-starter-data-jpa')
    implementation('org.springframework.boot:spring-boot-starter-jdbc')
    implementation('org.springframework.boot:spring-boot-starter-security')
    implementation('org.springframework.boot:spring-boot-starter-thymeleaf')
    implementation('org.springframework.boot:spring-boot-starter-validation')
    implementation('org.springframework.boot:spring-boot-starter-web')
    implementation('org.liquibase:liquibase-core')
    runtimeOnly('org.springframework.boot:spring-boot-devtools')
  runtimeOnly('org.postgresql:postgresql')
  testImplementation('org.springframework.boot:spring-boot-starter-test')
  testImplementation('org.springframework.security:spring-security-test')

  implementation('org.glassfish.jaxb:jaxb-runtime:2.3.1')
  implementation('org.glassfish.jaxb:jaxb-runtime2.3.1')
  implementation('org.springframework.boot:spring-boot-starter-data-redis')

  testCompile('org.junit.jupiter:junit-jupiter-api:5.3.1')
  testCompile('org.junit.jupiter:junit-jupiter-params:5.3.1')
  testRuntime('org.junit.jupiter:junit-jupiter-engine:5.3.1')
}

갱신하다

JUnit 5 의존관계를 추가하고 코멘트에 기재되어 있는 제외를 실시하면 효과가 있었습니다.테스트 의존성은 다음과 같습니다.

testImplementation('org.springframework.boot:spring-boot-starter-test') {
    exclude group: 'junit', module: 'junit' //by both name and group
}

다른 기여자가 언급한 몇 가지 추가 참고 사항:

스프링 부트 사용> 2.4.0

[ Boot ]> [ Spring Boot ]를 2.4.0는 JUnit 5 Jupiter를 사용할 필요가 없습니다.JUnit 5 Jupiter는 JUnit 5 Jupiter를 할 .spring-boot-starter-test라이브러리에는 빈티지 엔진 의존성(JUnit 4 포함)이 없어졌습니다.프로젝트에 대한 스타터 의존성만 포함하면 됩니다. 하다를 사용하세요.useJUnitPlatform()그래들 설정으로 합니다.

2.4.0 > Spring Boot > 2.2.0 사용

버전을 한다면 더 높은 하는 것이 2.2.0.RELEASE5을 JUnit 5에 spring-boot-starter-test폴트입입니니다

이러한 버전에서는 라이브러리에는 Vintage Engine 의존관계도 포함되어 있어 JUnit 5 Jupiter 플랫폼을 사용하여 JUnit 4 테스트를 실행할 수 있습니다. 4를 실행할 없는 은 JUnit 4 테스트를 제외할 것을 합니다.org.junit.vintage:junit-vintage-engine뿐만 junit" " " " " " ) :

testCompile('org.springframework.boot:spring-boot-starter-test') {
    exclude group: 'org.junit.vintage'
}

서도 '어울리다', '어울리다', '어울리다'를 .useJUnitPlatform()물론 지시입니다.

Gradle 4.6(아마도)부터는 JUnit 5 네이티브 지원이 있습니다.다음과 같이 JUnit5를 포함할 수 있습니다.

dependencies {
  testCompile "org.junit.jupiter:junit-jupiter-api:5.2.0"
  testCompile "org.junit.jupiter:junit-jupiter-params:5.2.0"
  testRuntime "org.junit.jupiter:junit-jupiter-engine:5.2.0"
}

또, 다음과 같은 것도 필요합니다.

test {
  useJUnitPlatform()
}

JUnit 4와 5는 서로 다른 패키지 이름을 사용하기 때문에 같은 프로젝트에서 공존할 수 있습니다.주석은 같다).@Test ) 、 [ ] 、 [ ] 、 [ ] 、 [ ] 、 [ ]의 URL 에서 그것들을 시켜 주세요.org.junit.jupiter.api★★★★★★★★★★★★★★★★★★.

여기에서는 컴파일 대신 구현사용하고 있습니다.

test {
  useJUnitPlatform()
}

dependencies {
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'junit', module: 'junit'
    }
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.4.2'
    testImplementation 'org.junit.jupiter:junit-jupiter-params:5.4.2'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.4.2'
}

업데이트 2020-10-29

Spring Boot 2.3 프로젝트에서는 이 스니펫이 필요 없게 되었습니다.디폴트로는 이미 JUnit 5를 사용하고 있었습니다.

의 것으로 .spring-boot-starter-test 2.되어 있음)는 Import하고 있습니다(2.2.6/2.2.7). Import하다.org.junit.vintage:junit-vintage-engine에 대한 있습니다.junit:junit ,만.junit 가지 오류를요: gave가 gave gave gave gave gave gave gave gave gave gave gave gave gave gave gave gave gave gave gave gave gave gave 。

May 13, 2020 9:20:05 AM org.junit.platform.launcher.core.DefaultLauncher handleThrowable
WARNING: TestEngine with ID 'junit-vintage' failed to discover tests
java.lang.NoClassDefFoundError: junit/runner/Version
...
Caused by: java.lang.ClassNotFoundException: junit.runner.Version
...

설정:

testCompile('org.springframework.boot:spring-boot-starter-test') {
    exclude group: 'org.junit.vintage'
}

모든 테스트가 계속 잘 진행되었습니까?

10개월 후, 그리고 사용법은Gradle 5.4.1,Spring Boot 2.1.7.RELEASE,그리고.JUnit 5.5.1다른 답변과는 다른 의존관계를 사용하고 있음을 알게 되었습니다.또한 개별 아티팩트가 아닌 Aggregate 아티팩트를 포함하는 것이 좋습니다.

testImplementation('org.junit.jupiter:junit-jupiter:5.5.1')

저의 관찰에 대한 자세한 내용은 다음 SO Q&A에서 확인할 수 있습니다.Spring Boot와 JUnit 5 간의 상호작용 - 개인이 아닌 전체적인 아티팩트를 사용해야 합니다.

추가만 하면 됩니다.

testImplementation('org.springframework.boot:spring-boot-starter-test') {
    exclude group: 'junit', module: 'junit'
}
testImplementation 'org.junit.jupiter:junit-jupiter:5.4.2'

그리고:

test {
    useJUnitPlatform()
}

언급URL : https://stackoverflow.com/questions/52862349/spring-boot-starter-test-with-junit-5

반응형