Unit Testing/JUnit/Create a Simple Test
< Unit Testing | JUnit
Create a Java Program
editCreate the following Java program. Save it as Multiply.java
public class Multiply {
public double multiply(double x, double y) {
return x * y;
}
}
Create a Test Program
editCreate the following JUnit test program. Save it as Main.java
import org.junit.*;
import org.junit.runner.*;
import static org.junit.Assert.*;
public class Main {
public static void main(String[] args) {
org.junit.runner.JUnitCore.main("Main");
}
@Test
public void multiply2x2Test() {
Multiply multiply = new Multiply();
double value = multiply.multiply(2, 2);
org.junit.Assert.assertEquals(value, 4, 0.0);
}
}
Test Success
editTest the program by running it and observe the results.
Test Failure
editChange the multiply source code somehow, such as multiplying by 0 rather than multiplying by y. Test the program by running it again and observe the results.