为什么测试类的属性在 2 个测试之间变为空



上下文:我试图创建一个代表我的读者的用户指南的junit测试。为此,我创建了一个 init 方法,一个测试我的假设的测试:初始化资源,创建一个读取器,最后读取文件内容。

(我的 junit 测试的第二个目标是获得更多关于假设和顺序测试的经验(

我的问题:在创建和阅读之间,我的阅读器(这是我的测试类的一个属性(变为空(参见私有BeanioReader阅读器(。

TECHNOS => org.junit.jupiter:5.4.1 + beanio:2.1.0 + commons-lang3:3.8.1 + jdk1.8.0_102

这是我的读者和相应的 junit 测试。

public class BeanioReader implements AutoCloseable{
    private BeanReader deleguate;
    public BeanioReader(final String mappingFilePath, final String streamName, final File csvFile) {
        //IllegalArgumentChecking...
        //Init...
        StreamFactory sf = StreamFactory.newInstance();
        sf.load(mappingFilePath);
        deleguate = sf.createReader(streamName, csvFile);
    }
    public Employee readLine() {
        return (Employee) deleguate.read();
    }
    @Override
    public void close() throws Exception {
        this.deleguate.close();
    }
}
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class BeanioReaderTest {
    private BeanioReader reader;
    private static String mappingFilePath;
    private static String streamName;
    private static File csvFile;
    private static final String BEANIO_CONFIG_FILENAME = "mapping.xml";
    private static final String BEANIO_CONFIG_NAME = "employeeFile";
    private static final String CSV_FILE_NAME = "data.csv";
    private static final Integer TOTAL_REC_IN_FILE=3;
    @BeforeAll
    public static void init() {
        mappingFilePath = BeanioReaderTest.class.getClassLoader().getResource(BEANIO_CONFIG_FILENAME).getFile();
        streamName = BEANIO_CONFIG_NAME;
        csvFile = new File(BeanioReaderTest.class.getClassLoader().getResource(CSV_FILE_NAME).getFile());
    }
    @Test
    @Order(1)
    @DisplayName("ASSUMES RESOURCES ARE OKAY")
    public void assume_resources_are_okay(){
        assumeTrue(mappingFilePath!=null && !mappingFilePath.isEmpty());
        assumeTrue(streamName!=null && !streamName.isEmpty());
        assumeTrue(csvFile!=null && csvFile.exists());
    }
    @Test
    @Order(2)
    @DisplayName("CREATION")
    public void createReader_successfull() {
        this.reader = new BeanioReader(mappingFilePath, streamName, csvFile);
        assertNotNull(this.reader);
    }
    @Test
    @Order(3)
    @DisplayName("READING")
    public void read() throws Exception {
        ArrayList<Employee> employees = new ArrayList<>();
        Employee employee;
        while ((employee = this.reader.readLine()) != null) {
           assertNotNull(employee);
           employees.add(employee);
        }
        reader.close();
        assertTrue(TOTAL_REC_IN_FILE==employees.size());
    }
}

我希望明白为什么...

测试结果,BeanIoReaderTest:

v ASSUME REQUIRED RESOURCES ARE OKAY
v CREATE BEAN IO READER
! READ CONTENT FILE

安慰:


java.lang.NullPointerException
    at com.lc.beaniopoc.BeanioReaderTest.read(BeanioReaderTest.java:58)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:628)
    at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:117)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$7(TestMethodTestDescriptor.java:184)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:180)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:127)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:68)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:135)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
    at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
    at java.util.ArrayList.forEach(ArrayList.java:1249)
    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
    at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
    at java.util.ArrayList.forEach(ArrayList.java:1249)
    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
    at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:229)
    at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$6(DefaultLauncher.java:197)
    at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:211)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:191)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:128)
    at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:74)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

因为 reader 是一个实例变量(不是带有静态关键字的类变量(,所以在你的 READING 测试用例中它将为 null。

这样做的原因是 Junit 为每个测试实例化了类。

您在此处设置的内容仅适用于此方法:

@Test
@Order(2)
@DisplayName("CREATION")
public void createReader_successfull() {
    this.reader = new BeanioReader(mappingFilePath, streamName, csvFile);
    assertNotNull(this.reader);
}

在此创建之后,READER 将使用尚未为读取器设置值的新实例启动。

一种解决方案是使读取器静态,这对于测试来说是不好的做法,因为每个测试都应该独立于其他测试。
其他解决方案 - 这是推荐的方法 - 以确保读者将为每个测试实例化。

提取此行:

 this.reader = new BeanioReader(mappingFilePath, streamName, csvFile);

到@Before方法,因此在每个测试用例之前,您的读者都会获得其值。

相关内容

  • 没有找到相关文章

最新更新