我对JUnit5和mockito框架完全陌生。现在我尝试为函数实现junit测试用例。有人能帮忙模拟一下吗,因为下面的测试实现在指定的行抛出一个nullpointerexception。
Class XYZ{
@Autowired
private SoapCall soapCall;
public void validate(){
//code....
JAXBElement <SubDTO> jax = (JAXBElement <Sub_DTO>) soapCall.callSoapService(val1, val2, val3, val4, val5);
SubDTO response = jax.getValue(); // Getting null pointer exception while Unit testing at this line
}
}
单元测试
Class TestValid{
@Mock
SoapCall soapCall;
@InjectMocks
XYZ xyzzy;
@BeforeEach
void setUp(){
MockitoAnnotations.initMocks(this);
}
@Test
public void test1(){
SubDTO dto= new SubDTO("a","b","c");
JAXBElement<SubDTO> jax = new JAXBElement <> (any(), any(), any());
jax.setValue(dto)
Mockito.when(soapCall.callSoapService(any(),any(),any(),anyInt(),anyInt())).thenReturn( jax );
Assertions.assertDoesNotThrow(()->xyz.validate());
}
}
1条答案
按热度按时间chhkpiq41#
我觉得这句话很奇怪:
JAXBElement jax = new JAXBElement <> (null, null, null);