-
스프링 부트, "실행 가능한 메서드 없음"오류로 단위 테스트 실행카테고리 없음 2020. 8. 20. 23:19
질문
Spring boot로 단위 테스트를 실행하고 있지만 이상한
no runnable
오류가 발생합니다. 테스트는 통과하지만 모든 테스트가 성공적으로 종료 된 후 갑자기 이상한 오류가 발생합니다.java.lang.Exception: No runnable methods at org.junit.runners.BlockJUnit4ClassRunner.validateInstanceMethods(BlockJUnit4ClassRunner.java:191) at org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(BlockJUnit4ClassRunner.java:128) at org.junit.runners.ParentRunner.validate(ParentRunner.java:416) at org.junit.runners.ParentRunner.<init>(ParentRunner.java:84) at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:65) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.<init>(SpringJUnit4ClassRunner.java:137) at org.springframework.test.context.junit4.SpringRunner.<init>(SpringRunner.java:49) at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
이 문제를 어떻게 해결합니까? Spring Boot가 내 테스트에서 실행 파일을 찾는 이유는 무엇입니까?
다음은 내 코드의 예입니다.
package ca.bell.uc.hello.world import org.junit.Assert import org.junit.jupiter.api.Test import org.junit.runner.RunWith import org.springframework.test.context.junit4.SpringRunner @RunWith(SpringRunner::class) internal class example { @Test fun f() { Assert.assertTrue(true) } }
다음은 오류의 스크린샷입니다.
감사
추신 Kotlin입니다
답변1
가져오기 때문에 JUnit 5
@Test
주석을 사용하려고합니다.import org.junit.jupiter.api.Test
콘솔 로그에서 JUnit 4가 사용되었음을 알 수 있습니다.
JUnit 4로 작업하려면 가져오기를 사용해야합니다.
import org.junit.Test
답변2
실제로 JUnit4 와 JUnit5 의 조합입니다.
@Ignore
또는@Disabled
를 사용한 테스트에서도@Test
를 기준으로 무시하지 않으려는 동일한 작업이있었습니다.Spring 종속성을 업데이트하고 혼합하지 않도록 gradle에서 Junit4 종속성을 제거하여 Spring에서이 문제를 피할 수 있습니다.
testImplementation("org.springframework.boot:spring-boot-starter-test") { exclude(module = "junit") exclude(module="junit-vintage-engine") exclude(module = "mockito-core") } testImplementation("org.junit.jupiter:junit-jupiter:5.4.2") testImplementation("org.junit.jupiter:junit-jupiter-api") testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
@RunWith (SpringRunner :: class)
를 사용할 필요가없는 경우 가져오기의 차이점을 확인할 수 있습니다. 모두 "jupiter" 및 수업은 다음과 같을 수 있습니다.import org.junit.jupiter.api.Assertions import org.junit.jupiter.api.Test internal class ExampleTest { @Test fun test() { Assertions.assertTrue(true) } }