JUnit Test for an @Override method

Brandon Bischoff :

I have a program containing the following class in it:

  public class Plane implements Flyable
{
    private final int numberOfEngines;
    private final String model;

    public Plane(int engines, String m)
    {
        numberOfEngines = engines;
        model = m;
    }

    @Override
    public String toString()
    {
        return String.format("%s with %d engines", model, numberOfEngines);
    }

    @Override
    public void launch() {
        System.out.println("Rolling until take-off");

    }

    @Override
    public void land() {
        System.out.println("Rolling to a stop");

    }
}

And I am currently attempting to write JUnit tests for the toString, launch, and land methods. This is what I have so far:

class PlaneTest {

    @Test
    void testToString() {
        assertEquals("Boing with 4 engines", Plane.this.toString());
    }

    @Test
    void testLaunch() {
        fail("Not yet implemented");
    }

    @Test
    void testLand() {
        fail("Not yet implemented");
    }

My problem is I cannot for the life of me figure out how to properly call the toString method within the Plane class. The only way Eclipse is showing me the toString method is with "this" in front of it, however I cannot figure out how to create an instance of Plane for it to use. I've spent way too much time trying to wrap my head around this so if somebody could help me out here it would be greatly appreciated. Thank you in advance!

Guy :

You need to call it with a Plan instance because toString() is not static and because you need to initialize the class members.

@Test
void testToString() {
    assertEquals("Boeing with 4 engines", new Plane(4, "Boeing").toString());
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=420695&siteId=1