Jump to content

Object-Oriented Programming/Unit Testing/Java

From Wikiversity

main.java

[edit | edit source]
/** This program demonstrates testing of the Temperature class. **/

import org.junit.*;
import org.junit.runner.*;
import static org.junit.Assert.*;
import java.util.Random;

public class Main {
    public static void main(String[] args) {
        org.junit.runner.JUnitCore.main("Main");
    }

    @Test
    public void celsiusValueTest() {
        Random random = new Random();
        int value = random.nextInt(100);

        Temperature temperature = new Temperature();
        temperature.setCelsius(value);
        org.junit.Assert.assertEquals(value, temperature.getCelsius(), 0.0);
    }

    @Test
    public void fahrenheitValueTest() {
        Random random = new Random();
        int value = random.nextInt(100);

        Temperature temperature = new Temperature();
        temperature.setFahrenheit(value);
        org.junit.Assert.assertEquals(value, temperature.getFahrenheit(), 0.0);
    }

    @Test
    public void absoluteZeroCelsiusTest() {
        Temperature temperature = new Temperature();
        double value = temperature.getAbsoluteZeroCelsius();
        org.junit.Assert.assertEquals(value, -273.15, 0.0);
    }

    @Test
    public void absoluteZeroFahrenheitTest() {
        Temperature temperature = new Temperature();
        double value = temperature.getAbsoluteZeroFahrenheit();
        org.junit.Assert.assertEquals(value, -459.67, 0.0);
    }

    @Test
    public void celsiusToFahrenheit() {
        Temperature temperature = new Temperature();
        double value = temperature.toFahrenheit(37);
        org.junit.Assert.assertEquals(value, 98.6, 0.0);
    }

    @Test
    public void fahrenheitToCelsius() {
        Temperature temperature = new Temperature();
        double value = temperature.toCelsius(98.6);
        org.junit.Assert.assertEquals(value, 37, 0.0);
    }

    @Test(expected = IllegalArgumentException.class)
    public void celsiusBelowAbsoluteZero() {
        Temperature temperature = new Temperature();
        temperature.setCelsius(-500);
    }

    @Test(expected = IllegalArgumentException.class)
    public void fahrenheitBelowAbsoluteZero() {
        Temperature temperature = new Temperature();
        temperature.setFahrenheit(-500);
    }
}

Temperature.java

[edit | edit source]
/** Temperature converter. Provides temperature conversion functions. Supports Fahrenheit and Celius temperatures.

Examples:

    Temperature temperature = new Temperature();
    temperature.setCelsius(37);
    System.out.println(temp.getFarenheit());

    temperature = new Temperature();
    temperature.setFahrenheit(98.6);
    System.out.println(temp.getCelcius());

    temperature = new Temperature();
    System.out.println(temp4.toCelsius(98.6));
    System.out.println(temp4.toFahrenheit(37));

**/

public class Temperature {
    private double _celsius;
    private double _fahrenheit;

    // Returns absolute zero Celsius.
    public double getAbsoluteZeroCelsius() {
        return -273.15;
    }

    //Returns absolute zero Fahrenheit.
    public double getAbsoluteZeroFahrenheit() {
        return -459.67;
    }

    // Returns Celsius value.
    public double getCelsius() {
        return this._celsius;
    }

    // Sets Celsius value.
    public void setCelsius(double value) {
        this._celsius = this.validateCelsius(value);
        this._fahrenheit = this.toFahrenheit(value);
    }

    // Returns Fahrenheit value.
    public double getFahrenheit() {
        return this._fahrenheit;
    }

    // Sets Celsius value.
    public void setFahrenheit(double value) {
        this._fahrenheit = this.validateFahrenheit(value);
        this._celsius = this.toCelsius(value);
    }

    // Converts Fahrenheit temperature to Celsius.
    public double toCelsius(double fahrenheit) {
        fahrenheit = validateFahrenheit(fahrenheit);
        return (fahrenheit - 32) * 5.0 / 9.0;
    }

    // Converts Celsius temperature to Fahrenheit.
    public double toFahrenheit(double celsius) {
        celsius = validateCelsius(celsius);
        return celsius * 9.0 / 5.0 + 32;
    }

    // Validates Celsius temperature.
    // Throws error if Celsius temperature is below absolute zero.
    private double validateCelsius(double celsius) {
        if (celsius < this.getAbsoluteZeroCelsius()) {
            throw new IllegalArgumentException(
                "celsius must not be below absolute zero (" +
                this.getAbsoluteZeroCelsius() + "). Received " + celsius);
        }

        return celsius;
    }

    // Validates Fahrenheit temperature.
    // Throws error if Fahrenheit temperature is below absolute zero.
    private double validateFahrenheit(double fahrenheit) {
        if (fahrenheit < this.getAbsoluteZeroFahrenheit()) {
            throw new IllegalArgumentException(
                "fahrenheit must not be below absolute zero (" +
                this.getAbsoluteZeroFahrenheit() + "). Received " + fahrenheit);
        }

        return fahrenheit;
    }
}

Try It

[edit | edit source]

Copy and paste the code above into one of the following free online development environments or use your own Java compiler / interpreter / IDE.

See Also

[edit | edit source]