@스프링 부츠스프링 부트가 아닌 응용 프로그램에 대한 테스트
스프링-레스트, 스프링-데이터-jpa 등을 사용하여 스프링-부트가 아닌 애플리케이션을 만들고 있으며 스프링-부트(1.4.1)를 사용하여 통합 테스트를 하고 싶습니다.해제).SpringApplication 클래스 또는 @SpringApplication 주석이 없습니다.
나는 시험 수업에 있습니다.
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = MyConfiguration.class)
public class MyIT { }
@RestController
public class MyController { }
내장된 Tomcat이 시작되고 컨트롤러가 초기화되는 것을 볼 수 있지만 TestRestTemplate를 사용하여 서비스를 호출하면 404가 표시됩니다.Dispatcher Servlet이 내 컨트롤러에 대해 모르는 것 같습니다.
또한 다음과 같이 서블릿 컨테이너 빈을 정의해야 했습니다.
@Bean
public EmbeddedServletContainerFactory servletContainer() {
TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
factory.setPort(9000);
factory.setSessionTimeout(10, TimeUnit.MINUTES);
return factory;
}
내장된 Tomcat에서 컨트롤러를 볼 수 있도록 Spring에 대한 구성이 누락되었습니까?@Enable을 사용해 보았습니다.자동 구성 @ComponentScan이 테스트 클래스에 적용되지만 아무런 영향을 주지 않습니다.저는 이것에 이틀을 허비했고 어떤 힌트라도 매우 감사합니다!
내 작업 완료IT클래스
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = { TestContextConfiguration.class })
public class MyIT {
@Value("${local.server.port}")
private int serverPort;
@Resource
private TestRestTemplate restTemplate;
@Test
public void test() throws Exception {
System.out.println("Port:" + serverPort);
System.out.println("Hello:" + this.restTemplate.getForEntity("/", String.class));
}
}
컨트롤러 클래스
@RestController
public class MyController {
@GetMapping("/")
public String hello() {
System.out.println("Hello called");
return "Hello";
}
}
테스트 결과
Port:9000
Hello:<404 Not Found,<!DOCTYPE html><html><head><title>Apache Tomcat/8.5.5 - Error report</title><style type="text/css">H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}.line {height: 1px; background-color: #525D76; border: none;}</style> </head><body><h1>HTTP Status 404 - /</h1><div class="line"></div><p><b>type</b> Status report</p><p><b>message</b> <u>/</u></p><p><b>description</b> <u>The requested resource is not available.</u></p><hr class="line"><h3>Apache Tomcat/8.5.5</h3></body></html>,{Content-Type=[text/html;charset=utf-8], Content-Language=[en], Content-Length=[992], Date=[Wed, 05 Oct 2016 14:26:46 GMT]}>
TestConfig 내부 클래스를 만들고 다음과 같이 @SpringBootConfiguration으로 주석을 달면 이를 달성할 수 있습니다.
@SpringBootConfiguration
public static class TestConfig {
}
그런 다음, 테스트 클래스에서:
@RunWith(SpringRunner.class)
@SpringBootTest(...)
@Import(MyIT.TestConfig.class)
public class MyIT {
@SpringBootConfiguration
@ComponentScan("com.example")
public static class TestConfig {
}
}
TestConfig 클래스에는 @ComponentScan 주석도 있습니다.이것은 스프링이 당신의 응용 프로그램 콩을 찾기 위해 사용합니다.
사용할 콩을 정의하고 주석을 달 수 있는 테스트 구성이 있어야 합니다.@TestConfiguration
@TestConfiguration
public class TestConfig {
@Bean
public Faker getFaker() {
return new Faker();
}
}
그러면 당신의 시험 수업은 이것을 좋아할 것입니다.
@SpringBootTest(classes = {MyService.class,TestConfig.class})
@RunWith(SpringRunner.class)
public class MyIT {
@Autowired
MyService myService;
@Autowired
Faker faker;
}
언급URL : https://stackoverflow.com/questions/39858226/springboottest-for-a-non-spring-boot-application
'source' 카테고리의 다른 글
Oracle 트랜잭션에서 커밋되지 않은 작업이 있는지 확인하려면 어떻게 해야 합니까? (0) | 2023.07.21 |
---|---|
Oracle SQL Developer 코드 창에서 변수를 사용하여 SQL 코드 실행 (0) | 2023.07.21 |
런타임에 Python 모듈 버전 확인 (0) | 2023.07.21 |
열 유형을 지정하는 판다에 빈 데이터 프레임 만들기 (0) | 2023.07.21 |
Spring boot and flyway: 통합 테스트 전에 데이터베이스 데이터 삭제 (0) | 2023.07.21 |