Junit 5 testing toString()

aitsimhand

I recently started messing around with Junit, now i want to check if my method returns the expected String. Just dont know how to do it

Here's my test method;`

@Test
void toStringTest(){

    Voetbalclub schoonhoven = new Voetbalclub("Schoonhoven");
    String expected = "Schoonhoven 2 1 1 0 0";

    schoonhoven.verwerkResultaat('g');
    schoonhoven.verwerkResultaat('w');
    assertEquals(schoonhoven, expected.equals("Schoonhoven 2 1 1 0 0"));

    System.out.println(schoonhoven);


}`

Ps: I know that my code is checking if its in the same memory spot, which it obviously isnt. Also tried .equals(),still got the same result though.

Any help would be highly appreciated.

Stultuske
@Test
void toStringTest(){
    Voetbalclub schoonhoven = new Voetbalclub("Schoonhoven");
    String expected = "Schoonhoven 2 1 1 0 0";

    schoonhoven.verwerkResultaat('g');
    schoonhoven.verwerkResultaat('w');
    assertEquals(schoonhoven, expected.equals("Schoonhoven 2 1 1 0 0"));

    System.out.println(schoonhoven);
}

Honestly, this code doesn't really make much sense. What your code is actually checking, is whether your schoonhoven variable is equal to a boolean, which it obviously isn't.

@Test
void toStringTest(){
    Voetbalclub schoonhoven = new Voetbalclub("Schoonhoven");
    String expected = "Schoonhoven 2 1 1 0 0";

    schoonhoven.verwerkResultaat('g');
    schoonhoven.verwerkResultaat('w');
    assertEquals(expected, schoonhoven.toString());
}

This will verify whether the result of schoonhoven.toStrign() is equal to the expected String.

Here you'll find more information. The parameters you pass are:

  1. the expected object
  2. the actual object

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related