Mockito 很强大, 但是它不支持静态方法.所以, 就用Powermock了. 要测试的对象是Controller中的接口, 对单元测试来说, 这个待测试的街口应该是一个白盒的. 所以, 其中的第三方(service,其他静态类)的调用都应该使用mock对象来stub起来. 下面是## 示例代码(随手写的,说明问题就行):
Controller:
@Controller
public class MainController {
@Autowired
private MainService mainService;
@GetMapping("/hello")
public String hello(){
mainService.hello();
String result = HelloUtil.hi();
if(result.equals("hi")){
return "ok";
}else {
return "fail";
}
}
}
Service:
/**
* Created by icer on 2017/10/13.
*/
@Service
public class MainService {
public String hello(){
System.out.println("hello");
return "hello";
}
}
Util静态类:
```java
public class HelloUtil {
public static String hi(){
return "hi";
}
}