【问题标题】:Jaxb2Marshaller Mock Bean coming as null JUNIT5Jaxb2Marshaller Mock Bean 以空 JUNIT5 形式出现
【发布时间】:2020-05-03 03:27:42
【问题描述】:

我正在尝试为方法编写 JUNIT。我能够模拟除 Jaxb2Marshaller 之外的所有其他 bean。在实际方法中,我得到 Jaxb2Marshaller 的空指针异常。其他 bean 可从同一配置类中获得。为什么只有 Jaxb2Marshaller bean 不可用。

测试班

@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);
    }

}

配置类

@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();
    }

}

实际课程

@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.

    } 
}

【问题讨论】:

  • 如果它可以帮助任何人,我不需要模拟 Jaxb2Marshaller,我只需要将定义我的 bean 的类添加到我的测试类的上下文配置中:例如@ContextConfiguration(classes = {..., Jaxb2Configuration.class})

标签: java spring mockito junit5


【解决方案1】:

我不认为你得到 NullPointerException 是因为 jaxb2Marshaller 为 null,而是因为表达式 jaxb2Marshaller.createMarshaller() 返回 null,并且进一步调用 marshal 是实际抛出异常的那个。

如果确实如此,那是因为您没有模拟对createMarshaller 的调用。您必须模拟 jaxb2Marshaller.createMarshaller() 才能返回编组器:


@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);

}

【讨论】:

  • 谢谢,这就是问题所在。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-03
  • 2014-05-27
  • 1970-01-01
  • 1970-01-01
  • 2011-04-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多