由JsonItemReaderBuilder构建的单元测试阅读器

时间:2018-08-27 08:50:24

标签: java spring unit-testing spring-boot spring-batch

我有一个包含批处理作业中使用的Bean的类:

@Configuration
public class JobBeans { 
    @Bean
    JsonItemReader<Rabbit> jsonReader() {
        return new JsonItemReaderBuilder<Rabbit>()
            .jsonObjectReader(new JacksonJsonObjectReader<>(Rabbit.class))
            .resource(new ClassPathResource("rabbits.json"))
            .name("rabbitReader")
            .build();
    }
}

我该如何测试Bean?

我当前的测试套件如下所示:

@ContextConfiguration(classes = JobBeans.class)
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, StepScopeTestExecutionListener.class })
@RunWith(SpringRunner.class)
public class RabbitReaderTest {

    @Autowired
    private JsonItemReader<Rabbit> sut;

    @Test
    public void read() throws Exception {
        assertNotNull(sut.read());
    }
}

它会在测试执行期间抛出NPE:

  

10:40:07.684 [Thread-0]警告   org.springframework.beans.factory.support.DisposableBeanAdapter-   在名称为的bean上调用destroy方法'close'失败   'rabbitReader'   org.springframework.batch.item.ItemStreamException:时出错   在以下位置关闭项目阅读器   org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.close(AbstractItemCountingItemStreamItemReader.java:138)     在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:498)在   org.springframework.beans.factory.support.DisposableBeanAdapter.invokeCustomDestroyMethod(DisposableBeanAdapter.java:364)     在   org.springframework.beans.factory.support.DisposableBeanAdapter.destroy(DisposableBeanAdapter.java:287)     在   org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroyBean(DefaultSingletonBeanRegistry.java:578)     在   org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingleton(DefaultSingletonBeanRegistry.java:554)     在   org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingleton(DefaultListableBeanFactory.java:961)     在   org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingletons(DefaultSingletonBeanRegistry.java:523)     在   org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingletons(DefaultListableBeanFactory.java:968)     在   org.springframework.context.support.AbstractApplicationContext.destroyBeans(AbstractApplicationContext.java:1033)     在   org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:1009)     在   org.springframework.context.support.AbstractApplicationContext $ 2.run(AbstractApplicationContext.java:928)   引起原因:java.lang.NullPointerException:null为   org.springframework.batch.item.json.JacksonJsonObjectReader.close(JacksonJsonObjectReader.java:89)     在   org.springframework.batch.item.json.JsonItemReader.doClose(JsonItemReader.java:123)     在   org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.close(AbstractItemCountingItemStreamItemReader.java:135)     ...省略了14个共同的框架

即使在测试资源目录中提供了该文件,InputStream也似乎为空。

1 个答案:

答案 0 :(得分:1)

似乎调用了读者的close方法,但没有调用open方法。您需要先打开阅读器,然后才能使用它读取数据。例如:

@Test
public void read() throws Exception {
    sut.open(new ExecutionContext());
    assertNotNull(sut.read());
    sut.close();
}