私はspring-rest、spring-data-jpaなどを使用して非スプリングブートアプリケーションを作成しており、スプリングブートを使用して統合テストを行いたいと考えています(1.4.1。リリース)。私は@ spring-bootアプリケーションのための@SpringBootTest
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = MyConfiguration.class)
public class MyIT { }
@RestController
public class MyController { }
これは組み込みTomcatを開始されていると私は私のコントローラが初期化されていることがわかります私のテストクラス上の任意の場所
SpringApplicationクラスまたは@SpringApplication注釈を持っていないことに注意してください、しかし、 TestRestTemplateを使って私のサービスを呼び出すときに404が得られます。 DispatcherServletのが私のコントローラも
について知っていないようだ、私は
@Bean
public EmbeddedServletContainerFactory servletContainer() {
TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
factory.setPort(9000);
factory.setSessionTimeout(10, TimeUnit.MINUTES);
return factory;
}
を次のように私は組み込みTomcatに見える私のコントローラを持って春のための任意の構成をしないのですservletContainer Beanを定義しなければならなかったことが表示されますか?テストクラスで@EnableAutoConfiguration @ComponentScanを使ってみましたが効果はありません。私はこれで2日を無駄にしており、どんなヒントも大変ありがとう!テストの
完全MyITクラス
@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));
}
}
Controllerクラス
@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]}>
MyITコンテンツを共有できますか? –
完全なMyITクラスを追加しました – ashburnite
restTemplateで**/"**"をリクエストしています。あなたの残りのコントローラで** @ RequestMapping(value = "/"、method = RequestMethod.GET)**を処理しますか?** MyController ** –