Recall & Review
beginner
What does the JUnit method
assertTrue(condition) check?It checks that the given
condition is true. If the condition is false, the test fails.Click to reveal answer
beginner
What happens when
assertFalse(condition) is used and the condition is true?The test fails because
assertFalse expects the condition to be false.Click to reveal answer
beginner
How would you use
assertTrue to check if a number is positive?Use
assertTrue(number > 0). This passes if the number is greater than zero.Click to reveal answer
beginner
Why is it important to use
assertTrue and assertFalse in tests?They help confirm that conditions in your code behave as expected, catching errors early.
Click to reveal answer
intermediate
Can
assertTrue and assertFalse include a message? How?Yes. You can add a message as the first argument, like
assertTrue("Must be true", condition). This message shows if the test fails.Click to reveal answer
What does
assertTrue(condition) do in a JUnit test?✗ Incorrect
assertTrue expects the condition to be true. If it is false, the test fails.
Which method would you use to check that a condition is false?
✗ Incorrect
assertFalse checks that the condition is false.
What happens if
assertFalse(condition) receives a true condition?✗ Incorrect
The test fails because assertFalse expects false.
How can you add a custom failure message to
assertTrue?✗ Incorrect
The message comes first: assertTrue("message", condition).
Which of these is a good use of
assertTrue?✗ Incorrect
Checking if a user is logged in returns true or false, so assertTrue(user.isLoggedIn()) is valid.
Explain how
assertTrue and assertFalse help in writing tests.Think about what happens when conditions are not met.
You got /4 concepts.
Write a simple example using
assertTrue to check if a number is even.Even numbers have zero remainder when divided by 2.
You got /3 concepts.