A slightly different way to do Boolean assertions in JUnit

Photo by Debby Hudson on Unsplash
    @Test
public void testFridgeTempProperRange() {
Refrigerator fridge = new Refrigerator();
double temperature = fridge.getTemperature();
assertTrue("Temp too cold", temperature > 10.0);
assertTrue("Temp too high", temperature < 40.0);

}
Testcase: testFridgeTempProperRange(basicexercises.RefrigeratorTest): FAILED
Temp too cold
junit.framework.AssertionFailedError: Temp too cold
at basicexercises.RefrigeratorTest.testGetTemperature(RefrigeratorTest.java:48)
assert condition;
    @Test
public void testFridgeTempProperRange() {
Refrigerator fridge = new Refrigerator();
double temperature = fridge.getTemperature();
assert temperature > 10.0;
assert temperature < 40.0;

}
Testcase: testFridgeTempProperRange(basicexercises.RefrigeratorTest): FAILED
null
junit.framework.AssertionFailedError

at basicexercises.RefrigeratorTest.testFridgeTempProperRange(RefrigeratorTest.java:47)
    @Test
public void testFridgeTempProperRange() {
Refrigerator fridge = new Refrigerator();
double temperature = fridge.getTemperature();
assert temperature > 10.0 : "Temp too cold";
assert temperature < 40.0 : "Temp too hot";

}
Testcase: testFridgeTempProperRange(basicexercises.RefrigeratorTest): FAILED
Temp too cold
junit.framework.AssertionFailedError: Temp too cold
at basicexercises.RefrigeratorTest.testFridgeTempProperRange(RefrigeratorTest.java:47)
    public void recalibrateThermometer() {
// PLACEHOLDER FOR RECALIBRATION TASKS
double temperature = this.getTemperature();
assert temperature > 10.0 : "Recalibration problem";
assert temperature < 40.0 : "Recalibration problem";
}

public static void main(String[] args) {
Refrigerator fridge = new Refrigerator();
System.out.println("Fridge temperature is "
+ fridge.getTemperature()
+ " degrees.");
System.out.println("Recalibrating, don't open door...");
fridge.recalibrateThermometer();
System.out.println("Fridge temperature is "
+ fridge.getTemperature()
+ " degrees");
System.out.println("Recalibration successful.");
}
run:
Fridge temperature is 42.7 degrees.
Recalibrating thermometer, don't open door...
Fridge temperature is 42.7 degrees.
Recalibration successful.
BUILD SUCCESSFUL (total time: 0 seconds)
    @Test
public void testRecalibrateThermometer() {
System.out.println("recalibrateThermometer");
Refrigerator fridge = new Refrigerator();
fridge.recalibrateThermometer();
}
Testcase: testRecalibrateThermometer(basicexercises.RefrigeratorTest): FAILED
Recalibration problem
junit.framework.AssertionFailedError: Recalibration problem
at basicexercises.Refrigerator.recalibrateThermometer(Refrigerator.java:22)
at basicexercises.RefrigeratorTest.testRecalibrateThermometer(RefrigeratorTest.java:56)
Testcase: testFridgeTempProperRange(basicexercises.RefrigeratorTest): FAILED
Temp too hot
junit.framework.AssertionFailedError: Temp too hot
at basicexercises.RefrigeratorTest.testFridgeTempProperRange(RefrigeratorTest.java:48)

is a composer and photographer from Detroit, Michigan. He has been working on a Java program to display certain mathematical diagrams.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store