Jaxb2Marshaller Mock Bean 作为空 JUNIT5 出现
Jaxb2Marshaller Mock Bean coming as null JUNIT5
我正在尝试为一个方法编写 JUNIT。我能够模拟除 Jaxb2Marshaller 之外的所有其他 bean。在实际方法中,我得到了 Jaxb2Marshaller 的空指针异常。其他 bean 可以从相同的配置中获得 class。为什么只有 Jaxb2Marshaller bean 不可用。
测试CLASS
@SpringBootTest
@ActiveProfiles("test")
class EPSGCFundAndActivateAPIClientTest {
@Autowired
EPSGCFundAndActivateAPIClient epsgcFundAndActivateAPIClient;
@MockBean
@Qualifier("paymentServiceRestTemplate")
RestTemplate paymentServiceRestTemplate;
@MockBean
Jaxb2Marshaller jaxb2Marshaller;
@Test
void consume() throws JAXBException {
String input = "";
//I cannot mock the jaxb2Marshaller marshal call . Because its void.
epsgcFundAndActivateAPIClient.consume(input);
}
}
配置 CLASS
@Configuration
@DependsOn({"otsProperties"})
public class BeanConfiguration {
@Bean
public Jaxb2Marshaller jaxb2Marshaller() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
Map<String, Object> properties = new HashMap<>();
properties.put(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setMarshallerProperties(properties);
return marshaller;
}
@Bean
public RestTemplate paymentServiceRestTemplate() {
final RestTemplateBuilder builder =
new RestTemplateBuilder().basicAuthentication("ABC", "123456");
builder.messageConverters(new Jaxb2RootElementHttpMessageConverter());
return builder.build();
}
}
实际值CLASS
@Component("epsGCFundAndActivateAPIClient")
public class EPSGCFundAndActivateAPIClient {
@Autowired
@Qualifier("paymentServiceRestTemplate")
RestTemplate paymentServiceRestTemplate;
@Autowired
Jaxb2Marshaller jaxb2Marshaller;
@Override
public PaymentServiceResponse consume(Input input) {
StringWriter sw = new StringWriter();
jaxb2Marshaller.createMarshaller().marshal(input.getPayload(),sw); // Getting null pointer Exception for jaxb2Marshaller
paymentServiceRestTemplate.methodCall(sw); // If i comment out the above line this method is getting called successfully. Means paymentServiceRestTemplate is available.
}
}
我认为你得到 NullPointerException
不是因为 jaxb2Marshaller
为空,而是因为表达式 jaxb2Marshaller.createMarshaller()
return 为空,并且进一步调用marshal
是实际抛出异常的那个。
如果确实如此,那是因为您没有模拟对 createMarshaller
的调用。你必须模拟 jaxb2Marshaller.createMarshaller()
到 return 编组器:
@Test
void consume() throws JAXBException {
when(jaxb2Marshaller.createMarshaller()).thenReturn(whateverYouNeed);
String input = "";
//I cannot mock the jaxb2Marshaller marshal call . Because its void.
epsgcFundAndActivateAPIClient.consume(input);
}
我正在尝试为一个方法编写 JUNIT。我能够模拟除 Jaxb2Marshaller 之外的所有其他 bean。在实际方法中,我得到了 Jaxb2Marshaller 的空指针异常。其他 bean 可以从相同的配置中获得 class。为什么只有 Jaxb2Marshaller bean 不可用。
测试CLASS
@SpringBootTest
@ActiveProfiles("test")
class EPSGCFundAndActivateAPIClientTest {
@Autowired
EPSGCFundAndActivateAPIClient epsgcFundAndActivateAPIClient;
@MockBean
@Qualifier("paymentServiceRestTemplate")
RestTemplate paymentServiceRestTemplate;
@MockBean
Jaxb2Marshaller jaxb2Marshaller;
@Test
void consume() throws JAXBException {
String input = "";
//I cannot mock the jaxb2Marshaller marshal call . Because its void.
epsgcFundAndActivateAPIClient.consume(input);
}
}
配置 CLASS
@Configuration
@DependsOn({"otsProperties"})
public class BeanConfiguration {
@Bean
public Jaxb2Marshaller jaxb2Marshaller() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
Map<String, Object> properties = new HashMap<>();
properties.put(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setMarshallerProperties(properties);
return marshaller;
}
@Bean
public RestTemplate paymentServiceRestTemplate() {
final RestTemplateBuilder builder =
new RestTemplateBuilder().basicAuthentication("ABC", "123456");
builder.messageConverters(new Jaxb2RootElementHttpMessageConverter());
return builder.build();
}
}
实际值CLASS
@Component("epsGCFundAndActivateAPIClient")
public class EPSGCFundAndActivateAPIClient {
@Autowired
@Qualifier("paymentServiceRestTemplate")
RestTemplate paymentServiceRestTemplate;
@Autowired
Jaxb2Marshaller jaxb2Marshaller;
@Override
public PaymentServiceResponse consume(Input input) {
StringWriter sw = new StringWriter();
jaxb2Marshaller.createMarshaller().marshal(input.getPayload(),sw); // Getting null pointer Exception for jaxb2Marshaller
paymentServiceRestTemplate.methodCall(sw); // If i comment out the above line this method is getting called successfully. Means paymentServiceRestTemplate is available.
}
}
我认为你得到 NullPointerException
不是因为 jaxb2Marshaller
为空,而是因为表达式 jaxb2Marshaller.createMarshaller()
return 为空,并且进一步调用marshal
是实际抛出异常的那个。
如果确实如此,那是因为您没有模拟对 createMarshaller
的调用。你必须模拟 jaxb2Marshaller.createMarshaller()
到 return 编组器:
@Test
void consume() throws JAXBException {
when(jaxb2Marshaller.createMarshaller()).thenReturn(whateverYouNeed);
String input = "";
//I cannot mock the jaxb2Marshaller marshal call . Because its void.
epsgcFundAndActivateAPIClient.consume(input);
}