I want to do a JUnit test on the ForecastDisplay class below that has no return value. The way this class works is that the currentPressure=29.92f gets replaced by a new pressure assigned in the tester class. The display method compares the new and old pressures and prints the appropriate message to the console. As this is a void method, I don't know how to test it.
For example: if I assign a new currentPressure of 35 in the JUnit test, then the first message will print because 35>29.92. If anyone could suggest how to test this I would appreciate it because so far I can't do this without changing the display method to return a value, which is cheating as I shouldn't have to adapt the code to pass a JUnit test. Thanks
public class ForecastDisplay implements Observer, DisplayElement {
private float currentPressure = 29.92f;
private float lastPressure;
private WeatherData weatherData;
public ForecastDisplay(WeatherData weatherData) {
this.weatherData = weatherData;
weatherData.registerObserver(this);
}
public void update(float temp, float humidity, float pressure) {
lastPressure = currentPressure;
currentPressure = pressure;
display();
}
public void display() {
System.out.print("Forecast Display: ");
if (currentPressure > lastPressure) {
System.out.println("Improving weather on the way!");
} else if (currentPressure == lastPressure) {
System.out.println("More of the same");
} else if (currentPressure < lastPressure) {
System.out.println("Watch out for cooler, rainy weather");
}
}
}