用Mockito模拟带有NullPointerException的StoredProcedureQuery

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

Mock StoredProcedureQuery with Mockito NullPointerException

问题

我正在以以下方式测试Spring Boot应用程序中的服务层

```java
import static org.mockito.Mockito.mock;

@RunWith(MockitoJUnitRunner.class)
public class ADPTest {
    @InjectMocks
	private ADPsService ADPsService;
	@Test
	public void getADPsDetails() {
		ADPsStoredDetails e = new ADPsStoredDetails();
		e.setAlert(STRING_VALUE);
		e.setTsid(STRING_VALUE);
		
		List<ADPsStoredDetails> ADPsStoredDetails = new ArrayList<ADPsStoredDetails>();
		ADPsStoredDetails.add(e);

		EntityManager targetEm = mock(EntityManager.class);
		StoredProcedureQuery query = mock(StoredProcedureQuery.class);
		Mockito.when(targetEm.createStoredProcedureQuery("dbo.getADPsinfo","ADPsDetails")).thenReturn(query);
		Mockito.when(query.getResultList()).thenReturn(ADPsStoredDetails);
		
		List<ADPDTO> actionsDTO =  ADPsService.getADPsDetails();
		assertTrue("List has values", actionsDTO.size() > 0);

	}
}

我正在测试的方法具有以下签名:

public List<ADPDTO> getADPsDetails() {
		// TODO Auto-generated method stub
		StoredProcedureQuery ADPsDetailsStoredProcedure = manager.createStoredProcedureQuery(
				"dbo.getADPsinfo","ADPsDetails");
		
		List<ADPsStoredDetails> results = ADPsDetailsStoredProcedure.getResultList();
		
		Map<String, List<ADPsStoredDetails>> result = results.stream()
                .collect(Collectors.groupingBy(ADPsStoredDetails::getTsid));

        List<ADPDTO> ADPDTO = new ArrayList<ADPDTO>();
        for (Map.Entry<String, List<ADPsStoredDetails>> entry : result.entrySet())  {
            ADPDTO e = new ADPDTO();
            e.setTsName(entry.getKey());
            e.setAlerts(entry.getValue());
            ADPDTO.add(e);
        } 

在服务方法中,我得到了 java.lang.NullPointerException,在以下代码处:

StoredProcedureQuery ADPsDetailsStoredProcedure = manager.createStoredProcedureQuery(
				"dbo.getADPsinfo","ADPsDetails");

请提供相关帮助。


<details>
<summary>英文:</summary>

I am testing service layer in Spring Boot Application in following manner :

import static org.mockito.Mockito.mock;

@RunWith(MockitoJUnitRunner.class)
public class ADPTest {
@InjectMocks
private ADPsService ADPsService;
@Test
public void getADPsDetails() {
ADPsStoredDetails e = new ADPsStoredDetails();
e.setAlert(STRING_VALUE);
e.setTsid(STRING_VALUE);

	List&lt;ADPsStoredDetails&gt; ADPsStoredDetails = new ArrayList&lt;ADPsStoredDetails&gt;();
	ADPsStoredDetails.add(e);

	EntityManager targetEm = mock(EntityManager.class);
	StoredProcedureQuery query = mock(StoredProcedureQuery.class);
	Mockito.when(targetEm.createStoredProcedureQuery(&quot;dbo.getADPsinfo&quot;,&quot;ADPsDetails&quot;)).thenReturn(query);
	Mockito.when(query.getResultList()).thenReturn(ADPsStoredDetails);
	
	List&lt;ADPDTO&gt; actionsDTO =  ADPsService.getADPsDetails();
	assertTrue(&quot;List has values&quot;, actionsDTO.size() &gt; 0);

}
}

The method i am testing has following signature 

public List<ADPDTO> getADPsDetails() {
// TODO Auto-generated method stub
StoredProcedureQuery ADPsDetailsStoredProcedure = manager.createStoredProcedureQuery(
"dbo.getADPsinfo","ADPsDetails");

	List&lt;ADPsStoredDetails&gt; results = ADPsDetailsStoredProcedure.getResultList();
	
	Map&lt;String, List&lt;ADPsStoredDetails&gt;&gt; result = results.stream()
            .collect(Collectors.groupingBy(ADPsStoredDetails::getTsid));

    List&lt;ADPDTO&gt; ADPDTO = new ArrayList&lt;ADPDTO&gt;();
    for (Map.Entry&lt;String, List&lt;ADPsStoredDetails&gt;&gt; entry : result.entrySet())  {
        ADPDTO e = new ADPDTO();
        e.setTsName(entry.getKey());
        e.setAlerts(entry.getValue());
        ADPDTO.add(e);
    } 

I am getting ```java.lang.NullPointerException``` in service method at 

StoredProcedureQuery ADPsDetailsStoredProcedure = manager.createStoredProcedureQuery(
"dbo.getADPsinfo","ADPsDetails");


Kindly help regarding it.



</details>


# 答案1
**得分**: 3

你没有为`InjectMocks`创建`Mock`实例以进行注入。将`targetEm`的创建移到实例变量中。

```java
@InjectMocks
private ADPsService ADPsService;

@Mock
private EntityManager targetEm;
英文:

You didn't create Mock instances for the InjectMocks to inject. Move creation of targetEm as an instance variable.

@InjectMocks
private ADPsService ADPsService;

@Mock
private EntityManager targetEm;

huangapple
  • 本文由 发表于 2020年7月23日 13:17:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63047335.html
匿名

发表评论

匿名网友

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

确定