0

コンテキスト: 読者のユーザー ガイドを表す junit テストを作成しようとしました。そのために、init メソッドを作成します。これは、私の仮定をテストするテストです。つまり、リソースが初期化され、リーダーが作成され、最後にファイルの内容が読み取られます。

(junit テストの 2 番目の目標は、仮定と順序テストの経験を積むことです)

私の問題: 作成と読み取りの間に、リーダー (テスト クラスの属性) が null になります (プライベート BeanioReader リーダーを参照)。

テクノス => 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)

4

1 に答える 1