Tackling a Second Test

Let’s write a second test to see if our concerns are warranted. Taking a look at the assignment to the match local variable (starting at line 30 in Profile.java), it appears that match gets set to true when the criterion weight is DontCare. Code in the remainder of the method suggests that matches() should return true if a sole criterion sets match to true.

Each unit test in JUnit requires its own context: JUnit doesn’t run tests in any easily determinable order, so we can’t have one test depend on the results of another. Further, JUnit creates a new instance of the ProfileTest class for each of its two test methods.

We must then make sure our second test, matchAnswersTrueForAnyDontCareCriteria, similarly creates a ...

Get Pragmatic Unit Testing in Java 8 with JUnit now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.