If you are viewing the source code for junit4, https://github.com/junit-team/junit4/blob/master/src/main/java/junit/framework/Assert.java#L71
You will get why test1 is not working
Part 1:
static public void assertEquals(String message, Object expected, Object actual) { if (expected == null && actual == null) { return; } if (expected != null && expected.equals(actual)) { return; } failNotEquals(message, expected, actual);
Part 2:
static public void failNotEquals(String message, Object expected, Object actual) { fail(format(message, expected, actual));
Part # 3:
public static String format(String message, Object expected, Object actual) { String formatted = ""; if (message != null && message.length() > 0) { formatted = message + " "; } return formatted + "expected:<" + expected + "> but was:<" + actual + ">"; }
So, you received a message about the return of part number 3 as
java.lang.AssertionError: expected [20] but found [12]
For a full-sized understanding of exceptions. JUnit rule. Go through the tutorial :
Waiting for Exceptions JUnit Rule
To make the claim that an exception was thrown using JUnit, it is common to use the try / fail / catch iteration or the expected @Test annotation element. Although it was shorter than the previous one, there is an argument that using the expected does not support all cases that you can test. An example is performing additional testing after an exception or testing against the actual exception message.
JUnit 4.7 introduces the next progression, @Rule, which offers the best of both worlds. These articles weigh the pros and cons of each approach and examine in more detail the syntax of each. Idiom try / fail / catch
A typical pattern is to catch an exception or fail clearly if it has never been thrown.
@Test public void example1() { try { find("something"); fail(); } catch (NotFoundException e) { assertThat(e.getMessage(), containsString("could not find something")); }
which would highlight the error as follows.
java.lang.AssertionError: expected an exception at org.junit.Assert.fail(Assert.java:91) at bad.roboot.example.ExceptionTest.example1(ExceptionTest.java:20) ...
The idiom has potential advantages in that it makes it possible to argue against the actual exclusion, as well as perform additional work after waiting. Besides the noise, the main disadvantage is that it is very easy to forget to turn on the call of failure. To be honest, do the test first, where we always run the red test, this will not be a problem, but too often things slip through the network. In practice, I have seen too many examples with a missing failure giving false positives.
@Test (expected = Exception.class)
Using the expected element, we can rewrite the test as follows.
@Test (expected = NotFoundException.class) public void example2() throws NotFoundException { find("something");
which will lead to the next failure.
java.lang.AssertionError: Expected exception: bad.robot.example.NotFoundException
Resource Link: What exactly does assertEquals check when approving lists?