我的应用程序中有以下代码。
有人可以帮我为此编写一个测试用例吗?
Service.java
```public class Service{
public void createMessage(){
try{
LOG.info("message sent");
}catch(JAXBException){
LOG.info("exception occured");
}
}```
ServiceTest.java
```public class ServiceTest{
@InjectMocks
Service service;
@Test
public void testCreateMessageException(){
doThrow(JAXBException.class).when(service.createMessage));
}```
我尝试了上面的测试用例,但它给了我以下错误。 org.mockito.exceptions.misusing.MissingMethodInitationException: when() 需要一个参数,该参数必须是“模拟上的方法调用”。 例如: when(mock.getArticles()).thenReturn(articles);
最佳答案
您的理解是正确的,您不能在用@InjectMocks(正在测试的系统)注释的服务上使用when().then()。它必须用于被测系统中 Autowiring 的服务。
关于java - Junit 测试 @InjectMocks 类中的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61399627/