lundi 9 novembre 2020

JUnit4: Testing a read file method with type ArrayList

I have written a test to check if my java method to read a .txt file will successfully execute

@Test
    public void testRead() {
        String filePath = "testpackread.txt";

        ArrayList<Card> testReaderMethod = FileManager.readFile(filePath);
        assertEquals(4,testReaderMethod);
    }

The file I open contains the number 4, all I want to do is have the test pass if it reads the first line as 4

The test runs and the method is successful, so the problem isn't how I wrote the method. The output in the commandline shows:

java.lang.AssertionError: expected:<4> but was:<[project.Card@3043fe0e]>

If it helps this is method that is being tested:

    public static ArrayList<Card> readFile(String packPath){  
        ArrayList<Card> cardDeck = new ArrayList<>();
        int cardNumber;
       
            BufferedReader in = new BufferedReader(new FileReader (packPath));
            String line = in.readLine();
            while(line!= null){
                cardNumber = Integer.parseInt(line);
                if (cardNumber > 0) {
                    cardDeck.add(new Card(cardNumber));
                    line = in.readLine();
                }else {//If found invalid card stop immediately and clear cardDeck
                    System.out.println("[Message] : Integer must be positive");
                    cardDeck.clear();
                    line = null;
                }
           
            in.close();//Close file reader
        return cardDeck;
    }

any help is greatly appreciated, I hope i've been clear

Aucun commentaire:

Enregistrer un commentaire