我正在为以下方法编写一个 Junit 测试类“ ServiceImplTest.java ”,但它在尝试 Marshall xmlRequest 时为空。任何人都可以帮我解决这个问题。提前致谢。
ServiceImplTest.java
@RunWith(PowerMockRunner.class)
@PrepareForTest({RequestXmlBuilder.class})
public class ServiceImplTest {
@Before
public void setUp() throws Exception {
PowerMockito.mockStatic(RequestXmlBuilder.class);
}
@Test
public void testExecute() throws Exception {
PowerMockito.when(RequestXmlBuilder.serviceMarshall(Request, jaxb2Marshaller)).thenReturn("XmlTest");
}
}
ServiceImpl.java
public class ServiceImpl {
public Response execute() {
String xmlRqst = RequestXmlBuilder.serviceMarshall(request, jaxb2Marshaller);
}
}
RequestXmlBuilder.java
public class RequestXmlBuilder {
public static String serviceMarshall(Request request, Jaxb2Marshaller jaxb2Marshaller)
throws JAXBException {
StringWriter requestXml = new StringWriter();
jaxb2Marshaller.marshal(request, new StreamResult(requestXml));
return requestXml.toString();
}
}
注意:在下面的语句中获取空值
jaxb2Marshaller.marshal(request, new StreamResult(requestXml));