What if you could catch bugs instantly without running your program by hand every time?
Why @Test annotation in JUnit? - Purpose & Use Cases
Imagine you have a big Java program and you want to check if each part works correctly. You try running the program and checking results by hand every time you make a change.
Manually running the program and checking results is slow and tiring. You might miss errors or forget to test some parts. It's easy to make mistakes and hard to keep track of what you tested.
The @Test annotation lets you mark methods as tests. The testing tool runs these tests automatically and tells you if they pass or fail. This saves time and avoids human errors.
public void checkAddition() {
int result = add(2, 3);
if (result != 5) {
System.out.println("Test failed");
}
}@Test
public void checkAddition() {
assertEquals(5, add(2, 3));
}With @Test, you can run many tests quickly and get clear results, making your code more reliable and easier to improve.
A developer changes a feature and runs all @Test methods to instantly see if anything broke, avoiding bugs before users find them.
Manual testing is slow and error-prone.
@Test marks methods to run automatically as tests.
This makes testing faster, safer, and more organized.
Practice
@Test annotation in JUnit?Solution
Step 1: Understand the role of
The@Test@Testannotation tells JUnit which methods are test cases to execute.Step 2: Eliminate unrelated options
Options B, C, and D do not relate to marking test methods.Final Answer:
To mark a method as a test case that JUnit will run -> Option AQuick Check:
@Testmarks test methods [OK]
@Test means 'run this method as test' [OK]- Thinking
@Testchanges method visibility - Confusing
@Testwith import statements - Assuming
@Testsets return type
@Test?Solution
Step 1: Recall JUnit test method rules
Test methods must be public, return void, and take no parameters.Step 2: Check each option
public void testMethod() {} matches all rules. Options B, C, and D violate visibility, return type, or parameters.Final Answer:
public void testMethod() {} -> Option CQuick Check:
Public, void, no params = correct test method [OK]
- Making test methods private
- Adding parameters to test methods
- Using non-void return types
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class SampleTest {
@Test
public void checkSum() {
int result = 2 + 3;
assertEquals(5, result);
}
}Solution
Step 1: Analyze the test method logic
The method calculates 2 + 3 and stores in result, then asserts result equals 5.Step 2: Check assertion correctness
Since 2 + 3 is 5, the assertionassertEquals(5, result)will pass.Final Answer:
Test passes successfully -> Option BQuick Check:
2 + 3 = 5, assertion true [OK]
- Assuming test fails without checking assertion
- Confusing compilation errors with test failures
- Ignoring that test methods return void
import org.junit.jupiter.api.Test;
public class ErrorTest {
@Test
private void testHidden() {
// test code
}
}Solution
Step 1: Check method visibility
JUnit requires test methods to be public so it can access and run them.Step 2: Validate other method properties
Test methods must be void and parameterless; private visibility is the error here.Final Answer:
Test method must be public, not private -> Option AQuick Check:
Test methods must be public [OK]
- Making test methods private
- Adding parameters to test methods
- Confusing lifecycle annotations with test method requirements
@Test for this purpose in JUnit 5?Solution
Step 1: Recall JUnit 5 exception testing
JUnit 5 usesassertThrows()inside@Testmethods to check exceptions.Step 2: Eliminate outdated or incorrect options
Use@Test(expected = Exception.class)annotation parameter is from JUnit 4 and invalid in JUnit 5. Addthrows Exceptionto the test method signature only declares exception, not tests it. Write a try-catch block without@Testmisses@Test.Final Answer:
Use@TestwithassertThrows()inside the method -> Option DQuick Check:
JUnit 5 uses assertThrows() inside @Test [OK]
- Using JUnit 4 style
@Test(expected=)in JUnit 5 - Only declaring throws without assertion
- Not using
@Testannotation
Ready to prove it?
Pass the test and this pattern moves unknown
