课时六十三、spring boot 1.3单元测试
SpringJUnit支持,由此引入Spring-Test框架支持!
@RunWith(SpringJUnit4ClassRunner.class)指定我们SpringBoot工程的Application启动类
@SpringApplicationConfiguration(classes = App.class)///由于是Web项目,Junit需要模拟ServletContext,因此我们需要给我们的测试类加上@WebAppConfiguration。
@WebAppConfiguration
参考:spring-boot-handlerInterceptor
课时六十四、Spring Boot 1.4+单元测试
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)//随机端口号:避免端口碰撞.
public class AppTest {
@Autowired
private HelloController helloController;
@Test //单元测试方法.
public void testHello(){
// Assert.assertEquals("hello", helloController.hello());
helloController.hello();
System.out.println("xzxxx"+helloController.hello());}
}
参考:spring-boot-handlerInterceptor