0

In my programming class, we are trying to figure out an assignment that was given to us to JUnit test code that we put together. And we are running into an issue because we can't tell if the void method is outputting the string inside correctly. Is there a way to test that? Or is that not legal in the Java language?

Side Note: The void method is an interface (we were learning about interfaces and polymorphism).

Some code to get the idea-

Creation of the interface:

public interface Flyable {

/**
 * Method: Launch - void 
 */
void launch();

/**
 * Method: Land - void 
 */
void land();

The class we're wanting to test(In the class Plane):

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

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

The test we're trying to figure out:

 /**
 * Method: Test Method for test Launch 
 */
@Test
void testLaunch() {
    assertEquals("Rolling until take-off", myPlane.launch());
}

/**
 * Method: Test Method for test Land 
 */
@Test
void testLand() {
    assertEquals("Rolling to a stop", myPlane.land());
}

Is there any way we can get something like this to work? Hope this made sense!

1 Answers1

0

Unit testing is white box testing, meaning the code you are testing should be there. It doesn't make sense if you don't know what the code does. You don't really know what is inside System.out.println now do you? So it doesn't really makes sense testing it.

But if the case is there are logic before the System.out.print that you need to verify, which you need to capture the value before it was printed. Then there is a way

albertjtan
  • 171
  • 2
  • 14