Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9073897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:31:18+00:00 2026-06-16T18:31:18+00:00

I’m beginner, and keep yourself in hands. I need to test this method –

  • 0

I’m beginner, and keep yourself in hands.
I need to test this method – write/read to/from file. Can You get advice? How better I can do this, use only junit test or better EasyMock? What can you recomend to do?

public class AbsFigure {

public void write(String fileName, List<FigureGeneral> figuresList) {
    try {
        PrintWriter out = new PrintWriter(
                new File(fileName).getAbsoluteFile());
        try {
            for (int i = 0; i < figuresList.size(); i++) {
                out.println(figuresList.get(i).toString());
            }
        } finally {
            out.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
        System.out.println("Cannot write to file!");
    }
}

// read from file
private ArrayList<FigureGeneral> figureReader(String fileName) {
    String line = null;
    ArrayList<FigureGeneral> myListFigure = new ArrayList<FigureGeneral>();

    try {
        File myFile = new File(fileName);
        FileReader fileReader = new FileReader(myFile);
        BufferedReader bufferedReader = new BufferedReader(fileReader);

        while ((line = bufferedReader.readLine()) != null) {
            myListFigure.add(fromStringToFigureGeneral(line));
        }
        System.out.println(myListFigure);
        bufferedReader.close();

    } catch (FileNotFoundException e) {
        System.out.println("File which You loking for not found!");
    } catch (NumberFormatException e) {
        e.printStackTrace();
    } catch (IOException e) {
        System.out.println("Wrong input!");
    }

    return myListFigure;
}

// change from String to FigureGeneral(Triangle or Rectangle)
private FigureGeneral fromStringToFigureGeneral(String line) {
    String[] arrayLines = line.split(" ");
    FigureGeneral figures = null;

    if (arrayLines[0].equals("triangle")) {
        figures = new Triangle(Double.parseDouble(arrayLines[1]),
                Double.parseDouble(arrayLines[2]), "triangle");
    } else {
        figures = new Rectangle(Double.parseDouble(arrayLines[1]),
                Double.parseDouble(arrayLines[2]), "rectangle");
    }

    return figures;
}

public void launch(AbsFigure absFigure) {
    List<FigureGeneral> figuresList = absFigure.generateRandomFigures(5);

    AreaCompare areaCompare = new AreaCompare();
    Collections.sort(figuresList, areaCompare);

    absFigure.write("test.txt", figuresList);
    figureReader("test.txt");
}

public static void main(String[] args) {
    AbsFigure absFigure = new AbsFigure();
    absFigure.launch(absFigure);
}
}

I wrote little more code, and You can understend situations better. I hope You can recomend how to test this(maybe you can show two variants).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T18:31:19+00:00Added an answer on June 16, 2026 at 6:31 pm

    You can completely circumvent the usage of disk I/O, by using memory I/O. You don’t need EasyMock for that, simply different input and output streams. You can assert that your methods are working correctly with JUnit by checking the resulting output stream (write) and resulting list of FigureGeneral objects (read).

    Step by step:

    1) Inject the outputstream that the write methods will use.

    You could add another “write” method that is called by your already existing “write” method that takes an output stream with a signature like this:

    public void write(OutputStream outputStream, List<FigureGeneral> figuresList);
    

    Your original write method would wrap the above method by calling that method like this:

    write(new FileOutputStream(new File(fileName).getAbsoluteFile()), figuresList);
    

    2) Repeat the above trick for the read method using InputStream.

    That would give you a read method with a signature like this:

    private ArrayList<FigureGeneral> figureReader(InputStream inputStream);
    

    3) Now write JUnit tests that use ByteArrayInputStream and ByteArrayOutputStream and call the above methods.

    You can check the results of your write method by calling toByteArray() on your ByteArrayOutputStream and asserting the results. You can check the results of your read method by asserting the values in the List after passing it a certain ByteArrayInputStream.

    Here’s a (pseudo) method you could add to your test class to assert that a lists of FigureGeneral objects of differing natures can be written to an output stream, read back from an input stream and be equal.

    public void assertCanBeWrittenAndReaback(List<FigureGeneral> inputFigure) {
      ByteArrayOutputStream outStream = new ByteArrayOutputStream();
      absFigure.write(outStream, inputFigures);
      List<FigureGeneral> outputFigure = absFigure.figureReader(new ByteArrayInputStream(outStream.toByteArray()));
      assertEqualsFigureGeneralLists(inputFigure, outputFigure);
    }
    

    4) This will give you a pretty good test coverage. Still, if you would like to achieve 100% test coverage you would still have to test the orignal write and readFigure methods that have now become wrappers.

    The original methods do nothing more than redirect to their namesake which uses the input and output stream. You could check their redirect using EasyMock by overriding (or implementing if you put these methods behind an interface) the two redirect methods and asserting that they receive proper FileInputStream and FileOutputStream objects. You’ll need some temporary files for that though since the FileOutputStream constructor needs an existing file. I don’t see any major problems using temporary files in a testcase however.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know how can I replace this 2 symbol below from the string
For some reason, after submitting a string like this Jack’s Spindle from a text
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.