我正在为应用程序中的服务进行单元测试。在此服务内部,有对外部Web服务(SOAP
)的调用。对该Web服务的调用将值设置为不同的Holder<String>
和Holder<Integer>
,我已将其作为参数发送给此WS调用。
这是我的服务方法:
CustomerServiceImpl
@Override
public Customer createCustomer(Customer dto) {
UserEkos userEkos = SecurityEkosUtils.getUserEkos();
if (dto.getOrigen() != null && dto.getOrigen().equalsIgnoreCase(DEL)) {
Optional<CustomerModel> result = customerRepository.findById(dto.getId());
if (!result.isPresent()) {
log.error("[createCustomer] The central does exist in SAP " + dto.getId());
throw new BusinessServiceException(Constants.FUNCTIONAL_ERROR, " The central does exist in SAP " + dto.getId());
}
} else if(dto.getOrigen() != null && !dto.getOrigen().equalsIgnoreCase(DEL)) {
log.error("[createCustomer] Do not introduce an ID");
throw new BusinessServiceException(Constants.FUNCTIONAL_ERROR, "Do not introduce an ID");
}
Zwcta293 zwcta293 = altaDeudoresMapper.dtoToZwcta293(dto);
Holder<String> eCodCreated = new Holder<>();
Holder<String> eMessage = new Holder<>();
Holder<Integer> eReturn = new Holder<>();
Holder<String> eSocCreated = new Holder<>();
Holder<String> eResidenceType = new Holder<>();
altaProxy.zwebserAltaDeudores(zwcta293, eCodCreated, eMessage, eReturn, eSocCreated, eResidenceType);
if (eReturn.value == 0) {
CustomerModel customerModel = CustomerMapper.dtoToDomain(dto);
customerModel.setId(eCodCreated.value);
if (dto.getSocieties() != null && !dto.getSocieties().isEmpty()) {
CustomerSocietyModel customerSociety = new CustomerSocietyModel();
customerSociety.setCustomer(customerModel.getId());
customerSociety.setSociety(userEkos.getSociety());
customerModel.addCustomerSociety(customerSociety);
}
customerRepository.save(customerModel);
} else {
log.error("[createCustomer] Error SAP " + eMessage.value);
throw new BusinessServiceException(Constants.FUNCTIONAL_ERROR, "Error SAP " + eMessage.value);
}
Customer customer = new Customer();
customer.setId(eCodCreated.value);
return customer;
}
这是我的测试方法:
CustomerServiceTest
@Test
void createCustomerOKTest() throws Exception{
Holder<String> eCodCreated = new Holder<>();
Holder<String> eMessage = new Holder<>();
Holder<Integer> eReturn = new Holder<>();
Holder<String> eSocCreated = new Holder<>();
Holder<String> eResidenceType = new Holder<>();
Customer customer = (Customer) UtilsTest.getFileJsonToFile(CUSTOMER_JSON, Customer.class);
customer.setId("1239000");
Zwcta293 sapCustomer = (Zwcta293) UtilsTest.getFileJsonToFile(CUSTOMER_SAP_JSON, Zwcta293.class);
CustomerModel customerModel = (CustomerModel) UtilsTest.getFileJsonToFile(CUSTOMER_MODEL, CustomerModel.class);
when(altaDeudoresMapper.dtoToZwcta293(any(Customer.class))).thenReturn(sapCustomer);
doAnswer(invoication -> {
Holder<String> eCodCreated1 = (Holder<String>) invoication.getArgument(1);
Holder<String> eMessage1 = (Holder<String>) invoication.getArgument(2);
Holder<Integer> eReturn1 = (Holder<Integer>) invoication.getArgument(3);
Holder<String> eSocCreated1 = (Holder<String>) invoication.getArgument(4);
Holder<String> eResidenceType1 = (Holder<String>) invoication.getArgument(5);
eReturn1.value = 1239000;
return null;
}).when(altaProxy).zwebserAltaDeudores(sapCustomer, eCodCreated, eMessage, eReturn, eSocCreated, eResidenceType);
when(customerRepository.save(Mockito.any(CustomerModel.class))).thenReturn(customerModel);
Customer customerFinal = customerService.createCustomer(customer);
assertSame(Integer.toString(1239000), customerFinal.getId());
}
当它到达服务if(eRetorno.value == 0)
中的行时,我得到一个NullPointerException
java.lang.NullPointerException
at com.vocento.ekos.customer.service.CustomerServiceImpl.createCustomer(CustomerServiceImpl.java:83)
at com.vocento.ekos.customer.service.CustomerServiceTest.createCustomerOKTest(CustomerServiceTest.java:131)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:532)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:115)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:171)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:72)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:167)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:114)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:59)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$4(NodeTestTask.java:108)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:72)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:98)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:74)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1540)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$4(NodeTestTask.java:112)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:72)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:98)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:74)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1540)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$4(NodeTestTask.java:112)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:72)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:98)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:74)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:220)
at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$6(DefaultLauncher.java:188)
at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:202)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:181)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:128)
at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:69)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
我知道为什么它返回NullPointerException
,但是我的问题是我真的不知道如何使其工作。我的服务使用该方法内的Holder值是否继续进行,但是我不知道有什么方法可以在测试中获取此Holder值,也无法模拟它们。
我试图为要传递的Holder设置一个值,作为在单元测试中的参数,但是它也不起作用。另外,我尝试模拟Holder来将它们作为参数发送,而不是传递真实对象。
答案 0 :(得分:0)
我最终通过将我的doAnswer().when()
代码更改为此来解决了该问题:
doAnswer((Answer<Void>) invocation -> {
Object[] arguments = invocation.getArguments();
if(arguments != null && arguments.length > 1 && arguments[0] != null && arguments[1] != null){
Holder<String> eCodCreated = (Holder<String>) arguments[1];
Holder<Integer> eReturn = (Holder<Integer>) arguments[3];
Holder<String> eSocCreated = (Holder<String>) arguments[4];
eReturn.value = 0;
eCodCreated.value = "1239000";
eSocCreated.value = "";
}
return null;
}
).when(altaProxy).zwebserAltaDeudores(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any());
设置完所需的“ Holder”值后,我不得不添加另一行代码,以便可以正确设置。