programing

스프링 부트 테스트에서 TestRestTemplate 및 MockMvc를 삽입할 수 없습니다.

linuxpc 2023. 3. 7. 21:09
반응형

스프링 부트 테스트에서 TestRestTemplate 및 MockMvc를 삽입할 수 없습니다.

스프링 부츠를 사용하고 있습니다.1.4.0.RELEASE컨트롤러 클래스 시험을 쓰고 있습니다.다음과 같은 예외가 있습니다.

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.concur.cognos.authentication.service.ServiceControllerITTest': Unsatisfied dependency expressed through field 'restTemplate': No qualifying bean of type [org.springframework.boot.test.web.client.TestRestTemplate] found for dependency [org.springframework.boot.test.web.client.TestRestTemplate]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.boot.test.web.client.TestRestTemplate] found for dependency [org.springframework.boot.test.web.client.TestRestTemplate]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

여기는 나의 시험 수업이다.

public class ServiceControllerITTest extends ApplicationTests {

    @Autowired
    private TestRestTemplate restTemplate;

    @Autowired
    private MockMvc mvc;

    @Test
    public void exampleTest() throws Exception {
         // test
    }
}

ApplicationTests.java

@RunWith(SpringRunner.class)
@SpringBootTest
@WebAppConfiguration
//@DirtiesContext
public class ApplicationTests {

    @Autowired
    Environment env;

    @Test
    public void contextLoads() {

    }

}

TestRestTemplate이 자동 설정되는 것은,@SpringBootTest가 설정되어 있다.webEnvironment즉, 웹 컨테이너를 시작하고 HTTP 요청을 수신합니다.예를 들어 다음과 같습니다.

@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)

Spring Boot의 Java 문서를 읽는 경우테스트 주석: 주석에는 다음과 같은 기능이 있습니다(여기에는 모든 기능이 나열되지 않고 질문과 관련된 기능만 나열됨).

  • 정의된 포트 또는 랜덤 포트에서 완전히 실행 중인 웹 서버를 시작할 수 있는 기능 등 다양한 webEnvironment 모드를 지원합니다.
  • TestRestTemplate 및/또는 WebTestClient bean을 등록하여 정의된 포트 또는 랜덤포트에서 완전히 가동되는 웹 서버를 사용하는 웹 테스트에 사용합니다.

@SpringBootTest(WebEnvironment=WebEnvironment).RANDOM_PORT)는 TestRestTemplate를 자동 접속하는 기능을 제공합니다(@AndyWilkinson의 답변에서도 언급됨).

그러나 같은 TestClass에서도 MockMvc를 자동 배선하려면 TestClass에서 @AutoConfigureMockMvc 주석을 사용합니다.

테스트 클래스는 다음과 같습니다.

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
public class SBTest {
    @Autowired
    private TestRestTemplate restTemplate;

    @Autowired
    private MockMvc mvc;

   // tests
}

이 기능을 사용하려면 사용되지 않는 TestRestTemplate를 사용하지 마십시오.

폐지:

import org.springframework.boot.test.TestRestTemplate;

정답:

import org.springframework.boot.test.web.client.TestRestTemplate;

그 후 를 사용할 수 있습니다.@Autowired클래스 내 주석:

@Autowired
private TestRestTemplate restTemplate;

사용하지 마십시오.

@Autowired
private MockMvc mvc;

둘 다 안 돼요.

스프링 부트 매뉴얼에 따르면:

자동 구성도 가능합니다.MockMvc비(非)로@WebMvcTest(예:SpringBootTest)에 주석을 붙임으로써@AutoConfigureMockMvc.

언급URL : https://stackoverflow.com/questions/39213531/spring-boot-test-unable-to-inject-testresttemplate-and-mockmvc

반응형