java存根并没有涵盖我在单元测试中的方法
我试图通过使用存根方法实现单元测试。 但是,当我存根该方法时,测试类没有行覆盖
服务等级
@Service
@Slf4j
public class Service {
@Autowired
private Client client;
private String doclinkUrl = "www.website.com"
public byte[] downloadContent(String objectId) {
String url = doclinkUrl + "documents/" +objectId + "/binary";
return client.target(url).request().get(byte[].class);
}
}
存根服务类
public class ServiceStub extends Service {
@Override
public byte[] downloadContent(String objectId) {
return "test".getBytes();
}
}
测试服务类
@RunWith(MockitoJUnitRunner.class)
public class ServiceTest {
@InjectMocks
private Service testee;
@Test
public void testDownloadContent(){
testee = new ServiceStub();
Assert.assertNotNull(testee.downloadContent("objectId"));
}
}
# 1 楼答案
如果您使用的是spring boot,那么可以对大部分部分进行集成测试,并且只使用
@MockBean
模拟外部API调用# 2 楼答案
单元测试中的子绑定指的是在对组件进行单元测试时不希望它干扰的依赖项
实际上,您希望对组件行为进行单元测试,并模拟或存根可能对其产生副作用的依赖关系
在这里,您将测试该类。这毫无意义
在使用
ServiceStub
实例的情况下执行测试,在单元测试方面当然不包括Service
代码在
Service
类中,要隔离的依赖项是:这样你就可以嘲笑它或者把它存根