0
0
JUnittesting~5 mins

assertTrue and assertFalse in JUnit - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AAlways passes the test
BFails the test if condition is false
CPasses the test if condition is false
DThrows an exception regardless of condition
Which method would you use to check that a condition is false?
AassertFalse
BassertNull
CassertEquals
DassertTrue
What happens if assertFalse(condition) receives a true condition?
ATest is skipped
BTest passes
CTest fails
DTest throws an error
How can you add a custom failure message to assertTrue?
AassertTrue(condition, message)
BassertTrue()
CassertTrue(condition)
DassertTrue(message, condition)
Which of these is a good use of assertTrue?
AassertTrue(user.isLoggedIn())
BassertTrue(user == null)
CassertTrue(false)
DassertTrue(5 < 3)
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.