0

THE CONTEXT: I tried to create a junit test which represents a user guide of my reader. So to do that, I create an init method, a test which test my assumption: resources are initialized, create a reader and finally read file content.

(The second goal of my junit test is to get more experiences with assumptions and order tests)

MY ISSUE: between the creation and the reading, my reader (which is an attribute of my test class) become null (cf private BeanioReader reader).

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

This is my reader and the corresponding junit test.

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

    }
}

I hope to understand why...

Test results, BeanIoReaderTest:

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

console:


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)

Manucci
  • 1
  • 4

1 Answers1

0

Because reader is an instance variable (an not a class variable with static keyword) it will be null in your READING test case.

The reason for this is that Junit instantiate the class for each test.

What you set here is only applicable for this method:

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

After this CREATION the READER will be launched with a new instance which havent yet set the value for the reader.

One solution is make the reader static which is bad practice for tests, because each test should be independent from others.
Other solution - which is the recommended way - to make sure reader will instansiated for every test.

extract this line:

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

to a @Before method, so before every testcase your reader will get its value.

beatrice
  • 3,684
  • 5
  • 22
  • 49
  • I agree. However, @Manuci, I would remove the `assume_resources_okay` as a test case - after all, it does not test the sut but only verifies the test setup; I would do that in `init` after what's already there. Also, you could get rid of `createReader_successful` altogether because it only verifies it's not null, and you might as well do that at the beginning of the `read` test case. – daniu Apr 13 '19 at 11:49
  • I removed assume_resources_okay (the aim was to try assumption but that was not a good case to use it). I removed the createReader_successful and the reader creation had been set into a @BeforeEach setup method as Beatrice advised – Manucci Apr 14 '19 at 15:16