p. 61~ HelloControllerTest 클래스 작성 시 나는 에러
No matching tests found in any candidate test task.
현재 Junit5를 사용하고 있는데, JUnit4의 문법을 가져와 사용 중이다.
클래스 애노테이션 @RunWith(SpringRunner.class)
구문을 @ExtendWIth(SpringExtension.class)
로 교체한다.
// Junit4
@RunWith(SpringRunner.class)
// Junit5
@ExtendWIth(SpringExtension.class)
import
구문의 Test
를 수정한다.
// Junit4
import org.junit.Test;
// Junit5
import org.junit.jupiter.api.Test;
테스트 클래스의 접근 제한자를 public
으로 수정한다.
// Junit4
class HelloControllerTest {
...
}
/// Junit5
public class HelloControllerTest {
...
}
테스트 실행 도구를 IntelliJ IDEA로 교체한다.
영문 버전
Preferences > Build,Execution,Deployment > Build Tools > Gradle > Runner > "Run tests using:"
Changed from "Gradle Test runner" to "Platform Test runner"
한글 버전
Preferences(환경 설정) > 빌드, 실행, 배포 > 빌드 도구 > Gradle > 빌드 및 실행 > 다음을 사용하여 테스트 실행 > "Gradle"에서 "IntelliJ IDEA"로 교체
참고) 해당 설정은 인텔리제이를 껐다 켜면 날아가는 것 같다.
원래 Gradle로 설정해줘야 실행시간이 빠르다고 알고 있어서 이렇게 설정 했던 걸로 기억하는데... 좀 알아봐야할듯
package com.book.springboot.web;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@ExtendWith(SpringExtension.class)
@WebMvcTest(controllers = HelloController.class)
public class HelloControllerTest {
@Autowired
private MockMvc mvc;
@Test
public void hello가_리턴된다() throws Exception{
String hello = "hello";
mvc.perform(get("/hello"))
.andExpect(status().isOk())
.andExpect(content().string(hello));
}
}
감사합니다!