No matching tests found in any candidate test task. 에러 해결

예슬·2024년 1월 9일
0
post-thumbnail

스프링 부트와 AWS로 혼자 구현하는 웹 서비스


p. 61~ HelloControllerTest 클래스 작성 시 나는 에러

No matching tests found in any candidate test task.

에러나는 이유

현재 Junit5를 사용하고 있는데, JUnit4의 문법을 가져와 사용 중이다.

해결 법

  1. 클래스 애노테이션 @RunWith(SpringRunner.class) 구문을 @ExtendWIth(SpringExtension.class)로 교체한다.

    // Junit4
    @RunWith(SpringRunner.class)
    
    // Junit5
    @ExtendWIth(SpringExtension.class)
  2. import 구문의 Test를 수정한다.

    // Junit4
    import org.junit.Test;
    
    // Junit5
    import org.junit.jupiter.api.Test;
  3. 테스트 클래스의 접근 제한자를 public으로 수정한다.

    // Junit4
    class HelloControllerTest {
    	...
    }
    /// Junit5
    public class HelloControllerTest {
    	...
    }
  4. 테스트 실행 도구를 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로 설정해줘야 실행시간이 빠르다고 알고 있어서 이렇게 설정 했던 걸로 기억하는데... 좀 알아봐야할듯


빌드&테스트 성공!

수정된 코드 전문 (HelloControllerTest 클래스)

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));
    }
}

참고 사이트

profile
블로그 이사 했습니다! 🏠 ⤵

1개의 댓글

comment-user-thumbnail
2024년 1월 20일

감사합니다!

답글 달기