Mockito对于在Mock对象上调用的方法引发的空指针异常

huangapple go评论84阅读模式
英文:

Mockito NullPointer Exception for the method called on Mock Object

问题

代码部分不要翻译,以下是翻译后的内容:

当第一次调用when(creditApplicationCache.getObject(""))时,我收到了一个空指针异常(NPE)。creditApplicationCache 为 null。

  1. 我正在使用 mockito 2.17.jar
  2. 我已经添加了 @RunWith(MockitoJUnitRunner.class)
  3. 我还尝试过 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.

  1. I am using mockito 2.17.jar.
  2. I have added the @RunWith(MockitoJUnitRunner.class).
  3. 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

huangapple
  • 本文由 发表于 2020年10月3日 05:31:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/64178469.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定