【发布时间】:2021-04-26 18:42:12
【问题描述】:
我对 Junit 5 和 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());
}
}
【问题讨论】:
-
我非常有信心您应该更早获得 IllegalArgumentExeption:
new JAXBElement <> (any(), any(), any())。请向我们展示堆栈跟踪。见github.com/javaee/jaxb-spec/blob/…
标签: java spring-boot junit mockito