Closed wmx3ng closed 2 years ago
仅有依赖接口类(无实现,ICheckService) , mock后, 在实际业务类中, 没有自动注入.
public class LogCheck { private ICheckService checkService; public void setCheckService(ICheckService checkService) { this.checkService = checkService; } public String getCheckValue() { //TODO do something. return checkService.check1(); } }
public interface ICheckService { String check1(); }
public class ICheckServiceMock { @MockMethod(targetClass = ICheckService.class) public String check1() { return "mock#check1"; } }
@MockWith(ICheckServiceMock.class) public class LogCheckTest { private LogCheck logCheck = new LogCheck(); @Test public void check1() { String value = logCheck.getCheckValue(); System.out.println("value = " + value); } }
在测试执行中, 调到业务类: return checkService.check1(); 报了空指针异常, 请教下, 如何将mock类自动注入进来
return checkService.check1();
请问是如何解决的
问题描述
仅有依赖接口类(无实现,ICheckService) , mock后, 在实际业务类中, 没有自动注入.
场景描述
业务类 LogCheck:
依赖类ICheckService, 仅有接口
依赖类ICheckService的Mock类-ICheckServiceMock
测试类LogCheckTest:
在测试执行中, 调到业务类:
return checkService.check1();
报了空指针异常, 请教下, 如何将mock类自动注入进来