0
@Override
public Collection<Flight> getAll() {
    try (ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream(file)))) {
        Object read = ois.readObject();
        List<Flight> objects   = (ArrayList<Flight>) read;
        return objects;
    } catch (IOException | ClassNotFoundException ex) {
        ex.printStackTrace();
        return new ArrayList<>();
    }
}

@Test
public void testGetAll() {
    try (ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream("flights.txt")))) {
        Object read = ois.readObject();
        expected = (ArrayList<Flight>) read;
    } catch (IOException | ClassNotFoundException ex) {
        ex.printStackTrace();
    }
    Collection<Flight> actual = flightService.getAll();
    assertEquals(expected, actual);
}

Hi I have serious problem with testing. Is the above code a correct way to test? Please help me

5
  • No, that's not a good way to test it; it duplicates the code rather than setting up a proper environment where the result is predictable and then checking that. Where does the file come from in the getAll() method? Commented Mar 13, 2020 at 7:31
  • Yah i felt it, it is wrong(.... getAll() got file from same place which Collection<Flight> get.. Both take file form "flights.txt" Commented Mar 13, 2020 at 7:32
  • What I meant is, how is file initialized in the tested class. Is the file path passed to it, or is it some constant value. Commented Mar 13, 2020 at 7:51
  • i dont initialize any file in tested class.there is one file which located in project.and I read these file in both case (expected and actual). Should I initilaze new file in test class? Commented Mar 13, 2020 at 8:00
  • I don't mean the file itself, I mean the file variable (containing the file path). Is it passed to the class in a constructor or set via some method? Commented Mar 13, 2020 at 8:19

1 Answer 1

1

So say your class is given the file to read in the constructor, like this:

class FlightReader {
    File file;
    public FlightReader(File f) {
        file = f;
    }
    // your getAll here
}

then a test would first create a file of its own with known data, then read it, then verify the results are as expected, like this:

@Test
public void testGetAll() {
    Flight f1 = new Flight("ACREG1", "B737");
    Flight f2 = new Flight("ACREG2", "A320");
    Flight f3 = new Flight("ACREG3", "B777");
    List<Flight> written = new ArrayList<>(Arrays.asList(f1, f2, f3));
    File tempFile = File.createTempFile("flights", "test");
    // write sample data
    try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(tempFile))) {
        oos.writeObject(written);
    }

    // data is written to a file, read it back using the tested code
    FlightReader reader = new FlightReader(tempFile);
    List<Flight> readFlights = reader.getAll();

    // verify the written and read data are the same
    assertThat(readFlights).contains(f1, f2, f3);
}

Some notes:

  • you should use specific classes - like in this case ArrayList - as little as possible. Why cast to an ArrayList if you just return a Collection in the end?
  • you shouldn't use Java Serialization at all; it's error-prone, a security risk, and considered a mistake by the Java architects.
Sign up to request clarification or add additional context in comments.

5 Comments

So it can create another file
and we will read this file ,and check if getall contains correct object yes?
What is FlightReader here?
@SamirAllahverdiyev FlightReader is the name I gave your class to be tested since you didn't mention it. The Flight class needs a properly defined equals () method for this to work.
Yeap I get it Thanksss

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.