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

我有一个包含批处理作业中使用的 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();

    }

}

我怎样才能像这样测试 Beans?


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


@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] WARN org.springframework.beans.factory.support.DisposableBeanAdapter - 在名为 'rabbitReader' org.springframework.batch.item.ItemStreamException 的 bean 上调用销毁方法 'close' 失败:错误而在 org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.close(AbstractItemCountingItemStreamItemReader.java:138) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java) 关闭项目阅读器) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在 java.lang.reflect.Method.invoke(Method.java:498) 在 org.springframework.beans.factory.support.DisposableBeanAdapter.invokeCustomDestroyMethod(DisposableBeanAdapter.爪哇: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) 在

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


拉丁的传说
浏览 163回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java