Challenge - 5 Problems
JUnit Test Class Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of JUnit test class with multiple test methods
Consider the following JUnit 5 test class. What will be the output when running this test class?
JUnit
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; public class CalculatorTest { @Test void testAdd() { assertEquals(5, 2 + 3); } @Test void testSubtract() { assertEquals(1, 3 - 2); } @Test void testMultiply() { assertEquals(6, 2 * 3); } }
Attempts:
2 left
💡 Hint
Check each assertion carefully to see if the expected and actual values match.
✗ Incorrect
Each test method uses assertEquals with correct expected and actual values. All assertions are true, so all tests pass.
❓ assertion
intermediate2:00remaining
Correct assertion to verify exception thrown in JUnit test
You want to test that a method throws an IllegalArgumentException when given a negative number. Which assertion correctly tests this in JUnit 5?
Attempts:
2 left
💡 Hint
Look for the JUnit 5 method designed to check exceptions.
✗ Incorrect
assertThrows expects the exception class and a lambda that runs the code. It passes if the exception is thrown.
🔧 Debug
advanced2:00remaining
Identify the problem in this JUnit test class setup
Examine the following JUnit 5 test class. What is the main issue that will cause tests not to run as expected?
JUnit
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; public class ServiceTest { private static Service service; @BeforeAll static void setup() { service = new Service(); } @Test void testServiceMethod() { assertTrue(service.isReady()); } }
Attempts:
2 left
💡 Hint
Check the rules for @BeforeAll methods in JUnit 5.
✗ Incorrect
In JUnit 5, methods annotated with @BeforeAll must be static unless the test class is annotated with @TestInstance(Lifecycle.PER_CLASS).
❓ framework
advanced2:00remaining
Best practice for organizing test classes in a large Java project
In a large Java project using JUnit, what is the best practice for organizing test classes to keep tests maintainable and clear?
Attempts:
2 left
💡 Hint
Think about how to easily find tests related to each class.
✗ Incorrect
Keeping test classes in the same package as the classes they test helps with package-private access and organization. Using a separate source folder for tests keeps production and test code separate.
🧠 Conceptual
expert2:00remaining
Understanding test class lifecycle annotations in JUnit 5
Which statement correctly describes the difference between @BeforeEach and @BeforeAll annotations in JUnit 5 test classes?
Attempts:
2 left
💡 Hint
Think about how often each setup method runs during the test class execution.
✗ Incorrect
@BeforeEach runs before every test method to set up fresh state. @BeforeAll runs once before any tests to set up shared resources.