Unit testing is the process of testing small parts of code, called units, to make sure each part works correctly by itself.
Click to reveal answer
beginner
Why do we write unit tests?
We write unit tests to find bugs early, make code easier to change safely, and ensure each part of the program works as expected.
Click to reveal answer
beginner
What is a test case in unit testing?
A test case is a small piece of code that checks if a specific function or method returns the right result for given inputs.
Click to reveal answer
beginner
What does @Test annotation do in JUnit?
The @Test annotation tells JUnit that the method below it is a test case to run automatically.
Click to reveal answer
intermediate
What is the difference between unit testing and integration testing?
Unit testing checks small parts alone, while integration testing checks if different parts work well together.
Click to reveal answer
What is the main goal of unit testing?
ATest database performance
BTest the whole application at once
CTest user interface design
DTest individual parts of code separately
✗ Incorrect
Unit testing focuses on testing small parts of code individually to ensure they work correctly.
Which JUnit annotation marks a method as a test case?
A@Test
B@Verify
C@Check
D@Run
✗ Incorrect
The @Test annotation tells JUnit to run the method as a test case.
What should a unit test ideally test?
AMultiple classes working together
BThe entire system's performance
CA single function or method
DUser acceptance criteria
✗ Incorrect
Unit tests focus on testing a single function or method in isolation.
Which of the following is NOT a benefit of unit testing?
AFinding bugs early
BReplacing manual testing completely
CMaking code easier to maintain
DHelping safe code changes
✗ Incorrect
Unit testing helps but does not replace all manual testing; other tests are still needed.
What does a failing unit test indicate?
AThe tested code may have a bug
BThe test code is wrong
CThe program is finished
DThe test passed successfully
✗ Incorrect
A failing test usually means the code being tested does not behave as expected and may have a bug.
Explain what unit testing is and why it is important.
Think about testing one small piece of code at a time.
You got /4 concepts.
Describe how JUnit helps in unit testing Java code.
Focus on JUnit features that make testing easier.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of a unit test in JUnit?
easy
A. To check small parts of code for errors early
B. To test the entire application at once
C. To deploy the application automatically
D. To write documentation for the code
Solution
Step 1: Understand unit test scope
Unit tests focus on small parts or units of code, not the whole app.
Step 2: Identify main goal
The goal is to find errors early by testing these small parts individually.
Final Answer:
To check small parts of code for errors early -> Option A
Quick Check:
Unit test purpose = check small code parts early [OK]
Hint: Unit tests check small code parts, not whole app [OK]
Common Mistakes:
Confusing unit tests with integration or system tests
Thinking unit tests deploy or document code
Assuming unit tests cover the entire application
2. Which annotation is used in JUnit to mark a method as a test?
easy
A. @Run
B. @Assert
C. @Test
D. @Check
Solution
Step 1: Recall JUnit test annotation
JUnit uses @Test to mark methods as tests.
Step 2: Verify other options
@Run, @Check, and @Assert are not valid JUnit test annotations.
Final Answer:
@Test -> Option C
Quick Check:
JUnit test method = @Test annotation [OK]
Hint: JUnit test methods use @Test annotation [OK]
Common Mistakes:
Using @Run instead of @Test
Confusing @Assert with test annotation
Assuming @Check is a JUnit annotation
3. What will be the result of this JUnit test?
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class CalcTest {
@Test
void testSum() {
int result = 2 + 3;
assertEquals(5, result);
}
}
medium
A. Test passes successfully
B. Test fails with assertion error
C. Compilation error due to missing import
D. Runtime exception occurs
Solution
Step 1: Analyze the test logic
The test calculates 2 + 3 and stores 5 in result.
Step 2: Check assertion
assertEquals(5, result) compares expected 5 with actual 5, so it matches.
Final Answer:
Test passes successfully -> Option A
Quick Check:
2+3=5 matches expected 5 [OK]
Hint: If expected equals actual, test passes [OK]
Common Mistakes:
Assuming test fails when values match
Thinking import is missing (it's present)
Expecting runtime errors without cause
4. Identify the error in this JUnit test code:
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class SampleTest {
@Test
public void testMultiply() {
int result = 3 * 4
assertEquals(12, result);
}
}
medium
A. Incorrect import statement
B. Wrong assertion method used
C. Test method missing @Test annotation
D. Missing semicolon after multiplication statement
Solution
Step 1: Check syntax in testMultiply method
The line 'int result = 3 * 4' is missing a semicolon at the end.
Step 2: Verify other parts
@Test annotation is present, assertion method is correct, imports are correct.
Final Answer:
Missing semicolon after multiplication statement -> Option D
Quick Check:
Java statements need semicolons [OK]
Hint: Look for missing semicolons in Java test methods [OK]
Common Mistakes:
Ignoring missing semicolon errors
Thinking @Test annotation is missing
Assuming wrong assertion method
5. You want to write a unit test that checks if a method throws an exception when given invalid input. Which JUnit 5 feature should you use?
hard
A. Use @BeforeEach to catch exceptions
B. assertThrows to verify the exception is thrown
C. assertEquals to compare exception messages
D. Use @Disabled to skip the test
Solution
Step 1: Understand exception testing in JUnit 5
JUnit 5 provides assertThrows to check if a specific exception is thrown by code.
Step 2: Evaluate other options
assertEquals compares values, not exceptions; @BeforeEach runs before tests; @Disabled skips tests.
Final Answer:
assertThrows to verify the exception is thrown -> Option B
Quick Check:
Use assertThrows for exception tests [OK]
Hint: Use assertThrows to test exceptions in JUnit 5 [OK]
Common Mistakes:
Using assertEquals to check exceptions
Misusing @BeforeEach for exception handling
Disabling tests instead of testing exceptions
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.