非Spring Boot应用程序的@SpringBootTest

18

我正在使用spring-rest、spring-data-jpa等创建一个非spring-boot应用程序,我想使用spring boot(1.4.1.RELEASE)进行集成测试。请注意,我没有任何SpringApplication类或@SpringApplication注解。

在我的测试类中,我有

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = MyConfiguration.class)
public class MyIT { }

@RestController
public class MyController { }

我正在启动一个嵌入式的Tomcat,可以看到我的控制器正在被初始化,但是当使用TestRestTemplate调用我的服务时,我收到了404错误。似乎DispatcherServlet不知道我的控制器。

此外,我不得不定义一个名为servletContainer的bean,如下所示:

@Bean
public EmbeddedServletContainerFactory servletContainer() {
    TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
    factory.setPort(9000);
    factory.setSessionTimeout(10, TimeUnit.MINUTES);
    return factory;
}

我是否缺少Spring的某些配置,以使我的控制器对嵌入式Tomcat可见? 我尝试在测试类上使用@EnableAutoConfiguration和@ComponentScan,但它们没有任何效果。 我已经浪费了两天时间在这个问题上,非常感谢任何提示!

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

}

控制器类

@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的内容吗? - Issam El-atif
添加了完整的MyIT类。 - ashburnite
您正在使用restTemplate请求 **"/"**,请问您是否在rest控制器 MyController 中处理了 @RequestMapping(value = "/", method = RequestMethod.GET) - Issam El-atif
是的,我做。使用@Getmapping("/")。 - ashburnite
错误意味着Spring没有扫描和加载MyController。MyController在主包中,而MyIT在测试包中,对吗?也许你应该在MyIT中添加@ComponentScan("mycontroller.package")来扫描MyController。 - Issam El-atif
显示剩余3条评论
2个回答

20

可以通过创建一个名为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注解。这是Spring用于查找您的应用程序bean的方式。

尽管这个解决方案能够工作,但它的实现方式有些粗糙,并且难以维护。 - kylie.zoltan

0

有一个测试配置,定义你想要使用的bean,并用@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;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接