public static void main(String[] args) {
Integer b = null;
Integer a = false ? 0 : b;
System.out.println("a = " + a);
}
第二行报空指针异常
单元测试
使用mockito写单元测试,使用spring boot test, mockito mvc写集成测试
1. mockito理解
Mockito 3.6.0 中文文档 - ChangingFond
@MockBean 用于加到ApplicationContext中,会替换里面同类型的bean,没有就new一个。而@Mock就是一般使用。
另外@InjectMocks 是一个动词,给它注入依赖的mock。不过在spring中,spring会处理依赖,所以应该用不上。
@Inject就是一般的注入正常的对象。spring中应该也不太用,因为都被注好了。
2. spring boot test使用SpringBoot——学会使用Test,检测自己写的代码_我是一棵卷心菜的博客-CSDN博客_springboot testcase
3. 契约测试
Spring Cloud Contract实践_编码是个技术活的博客-CSDN博客Getting Started
Getting Startedhttps://docs.spring.io/spring-cloud-contract/docs/current/reference/html/getting-started.html#getting-started-introducing-spring-cloud-contractstubServer默认运行在localhost下