Issue
For Example I have this method: Number.java
public static int add(int nr1, int nr2) {
return nr2 + nr2;
}
As you see I return number2 + number2
In my Test I use that it would throw the error that it was expected 9 instead of the number which was calculate in the main method.
@Test
public void test() throws Exception {
int nr1 = 4;
int nr2 = 5;
int sum = Number.add(nr1, nr2);
assertEquals(9, summe);
}
I tried this to get an message if the program fails but it doesn´t work as I thought:
@Test
public void test() throws Exception {
int nr1 = 4;
int nr2 = 5;
int sum = Number.add(nr1, nr2);
assertEquals(9, sum);
try {
assertEquals(9, sum);
}catch(AssertionError e) {
String message = e.getMessage();
message = "Did you calculate the right numbers together?";
}
}
Solution
Use assertEquals(String message, int expected, int actual)
instead.
something like
assertEquals("Did you calculate the right numbers together?", 9, sum)
Source : jUnit javadoc
Answered By - Smile
Answer Checked By - Willingham (JavaFixing Volunteer)