应用程序test的java,java – 用于非spring-boot应用程序的@SpringBootTest

我正在使用

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,

Apache Tomcat/8.5.5 - Error report

HTTP Status 404 - /

type Status report

message /

description The requested resource is not available.


Apache Tomcat/8.5.5

,{Content-Type=[text/html;charset=utf-8], Content-Language=[en], Content-Length=[992], Date=[Wed, 05 Oct 2016 14:26:46 GMT]}>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值