unit-testingjunitmockitoinputstreamdatainputstream

EOF Exception for ObjectInputStream test


I am trying to test readObject method for reading object from ObjectInputStream

@Test
public void testReadObject() throws Exception {
    ObjectInputStream in = new ObjectInputStream(System.in);
...
} 

I am getting java.io.EOFException for ObjectInputStream in ObjectInputStream in = new ObjectInputStream(System.in);.

Do I mock ObjectInputStream or is it a bad practice? How do I approach in writing a test case to cover readObject scenario as a whole?

My Employee class:

public class Employee {

private Object Teacher;
...
private void readObject(java.io.ObjectInputStream in) throws Exception {
    Teacher = in.readObject();
 }
...
}

Note that I am using JUnit and Mockito.


Solution

  • My method require a little bit more test code, but it gives you an opportunity to write a test without mocking ObjectInputStream:

    @Test
    void testReadObject() throws Exception {
        String teacherObject = "TeacherObject";
        final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        new ObjectOutputStream(outputStream).writeObject(teacherObject);
        final Employee employee = new Employee();
    
        employee.readObject(
            new ObjectInputStream(
                new ByteArrayInputStream(
                    outputStream.toByteArray()
                )
            )
        );
    
        assertEquals(teacherObject, employee.getTeacher());
    }
    
    

    Note: This test is written under two assumption:

    1. Employee class has a method getTeacher(). Cause you need assert somehow that Teacher is correctly read from ObjectInputStream. If you have the other way to do that - modify my example according your need.
    2. Employee.readObject() method visibility is public. This is done solely to simplify my example. In your quesion readObject() has a private visibility. You can either modify my example according your need or give me more context that I can modify my example accordingly.

    Full source code:

    1. Employee.java
    package dev.iakunin.stackoverflow.question;
    
    public class Employee {
    
        private Object teacher;
    
        public void readObject(java.io.ObjectInputStream in) throws Exception {
            teacher = in.readObject();
        }
    
        public Object getTeacher() {
            return teacher;
        }
    }
    
    1. EmployeeTest.java
    package dev.iakunin.stackoverflow.question;
    
    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.nio.charset.Charset;
    import java.nio.charset.StandardCharsets;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import static org.junit.jupiter.api.Assertions.*;
    import org.junit.jupiter.api.Test;
    
    class EmployeeTest {
    
        @Test
        void testReadObject() throws Exception {
            String teacherObject = "TeacherObject";
            final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            new ObjectOutputStream(outputStream).writeObject(teacherObject);
            final Employee employee = new Employee();
    
            employee.readObject(
                new ObjectInputStream(
                    new ByteArrayInputStream(
                        outputStream.toByteArray()
                    )
                )
            );
    
            assertEquals(teacherObject, employee.getTeacher());
        }
    }