英文:
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<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);
}
}
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<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);
}
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;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论