英文:
Mockito NullPointer Exception for the method called on Mock Object
问题
代码部分不要翻译,以下是翻译后的内容:
当第一次调用when(creditApplicationCache.getObject(""))
时,我收到了一个空指针异常(NPE)。creditApplicationCache
为 null。
- 我正在使用
mockito 2.17.jar
。 - 我已经添加了
@RunWith(MockitoJUnitRunner.class)
。 - 我还尝试过
MockitoAnnotations.initMocks(this)
。
但是没有什么帮助。CreditApplicationCache
没有被初始化。
@RunWith(MockitoJUnitRunner.class)
public class WebServiceClientTest {
@InjectMocks
WebServiceClientImpl webServiceClientImpl;
@Mock
private ApplicationCacheImpl creditApplicationCache;
/*@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}*/
@Test
public void testWebServiceClient() throws BusinessException, SystemException {
WebServiceClientImpl webServiceClientImpl = new WebServiceClientImpl();
CreditCardDetailsResponse creditCardDetailsResponse = new CreditCardDetailsResponse();
creditCardDetailsResponse.setAccountNumber("1234567");
when(creditApplicationCache.getObject("XXXXXXX")).thenReturn(creditCardDetailsResponse);
when(webServiceClientImpl.getCreditCardDetails("1234")).thenReturn(creditCardDetailsResponse);
CreditCardDetailsResponse mockResponse = webServiceClientImpl.getCreditCardDetails("1234");
assertEquals(creditCardDetailsResponse.toString(), mockResponse.toString());
}
}
WebServiceClientImpl.java
public class WebServiceClientImpl implements WebServiceClient {
@Autowired
private ApplicationCache creditApplicationCache;
public CreditCardDetailsResponse getCreditCardDetails(String id)
throws BusinessException, SystemException {
// 一些代码
CreditCardDetailsResponse response = null;
response = (CreditCardDetailsResponse) creditApplicationCache.getObject(cacheKey);
if (response == null) {
try {
// 更多代码
}
}
return response;
}
}
英文:
I am getting a NPE when the first when(creditApplicationCache.getObject("")).thenRetun(response) is called. creditApplicationCache is null.
- I am using mockito 2.17.jar.
- I have added the @RunWith(MockitoJUnitRunner.class).
- I have also tried with MockitoAnnotations.initMocks(this).
But nothing has helped. CreditApplicationCache is not getting initialized.
@RunWith(MockitoJUnitRunner.class)
public class WebServiceClientTest {
@InjectMocks
WebServiceClientImpl webServiceClientImpl;
@Mock
private ApplicationCacheImpl creditApplicationCache;
/*@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}*/
@Test
public void testWebServiceClient() throws BusinessException, SystemException {
WebServiceClientImpl webServiceClientImpl = new WebServiceClientImpl();
CreditCardDetailsResponse creditCardDetailsResponse = new CreditCardDetailsResponse();
creditCardDetailsResponse.setAccountNumber("1234567");
when(creditApplicationCache.getObject("XXXXXXX")).thenReturn(creditCardDetailsResponse);
when(webServiceClientImpl.getCreditCardDetails("1234")).thenReturn(creditCardDetailsResponse);
CreditCardDetailsResponse mockResponse = webServiceClientImpl.getCreditCardDetails("1234");
assertEquals(creditCardDetailsResponse.toString(),mockResponse.toString());
}
}
---------------------------------------------------------------------------------------------------------
WebServiceClientImpl.java
public class WebServiceClientImpl implements WebServiceClient {
@Autowired
private ApplicationCache creditApplicationCache;
public CreditCardDetailsResponse getCreditCardDetails(String id)
throws BusinessException, SystemException {
// Some Code
CreditCardDetailsResponse response = null;
response = (CreditCardDetailsResponse) creditApplicationCache.getObject(cacheKey);
if (response == null) {
try {
// More Code }
return response ;
}
}
答案1
得分: 0
这个问题可能会出现在您从org.junit.jupiter.api.Test
导入@Test
而不是org.junit.Test
时。您能确保您正在使用正确的@Test
注解吗?
英文:
This issue might occur, when you import @Test
from org.junit.jupiter.api.Test
instead of org.junit.Test
. Can you ensure, that you are using the correct @Test
annotation?
答案2
得分: 0
你的问题在于你在测试的第一行又重新创建了客户端:WebServiceClientImpl webServiceClientImpl = new WebServiceClientImpl();
@InjectMocks
已经构造了被注解的类,并且注入了所有相关的模拟对象,但是你的操作是在没有它的情况下重新创建了它,因此出现了空指针异常。
只需移除那一行代码,应该就能正常运行。
英文:
Your problem is that you are creating the client again on the first line of the test: WebServiceClientImpl webServiceClientImpl = new WebServiceClientImpl();
@InjectMocks
already constructs the annotated class, and injects all the relevant mocks, but what you do is re-create it without it, hence the NPE.
Simply remove that line and it should work
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论