12 January 2026

#JUnit

#JUnit

Key Concepts


S.No Topic Sub-Topics
1 Introduction to JUnit What is JUnit?, Importance of Unit Testing, History of JUnit, Versions overview, Use cases
2 JUnit Architecture Core classes, Test runners, Test lifecycle, Annotations overview, Test suites
3 JUnit 4 vs JUnit 5 Key differences, Annotations, Assertions, Extension model, Migration strategies
4 JUnit Annotations @Test, @Before, @After, @BeforeClass, @AfterClass
5 JUnit 5 Annotations @Test, @BeforeEach, @AfterEach, @BeforeAll, @AfterAll
6 Assertions in JUnit assertEquals, assertTrue, assertFalse, assertNotNull, assertThrows
7 Parameterized Tests Introduction, @ParameterizedTest, @ValueSource, @CsvSource, Custom parameter providers
8 JUnit Test Suites Purpose, Creating test suites, Including multiple classes, @Suite annotation, Running suites
9 Exception Testing assertThrows, Expected exceptions, Handling exceptions in tests, Try-catch in tests, Best practices
10 Timeouts in Tests Using @Test(timeout), assertTimeout, assertTimeoutPreemptively, Long-running tests, Best practices
11 Assumptions in JUnit assumeTrue, assumeFalse, Conditional test execution, Environment-specific tests, Integration with CI
12 Test Lifecycle Methods Setup and teardown, @BeforeEach/@AfterEach, @BeforeAll/@AfterAll, Resource management, Best practices
13 Nested Tests Introduction, @Nested annotation, Structuring tests, Inner classes, Scope and lifecycle
14 Tagging Tests @Tag annotation, Grouping tests, Running specific tags, Excluding tags, Integration with CI/CD
15 JUnit Extensions Introduction, @ExtendWith annotation, Custom extensions, Parameter resolvers, Test lifecycle hooks
16 Mocking with Mockito Mockito basics, @Mock, @InjectMocks, when-thenReturn, Verifying interactions
17 JUnit with Spring Boot @SpringBootTest, @WebMvcTest, @MockBean, Context loading, Integration tests
18 Behavior Driven Testing Introduction to BDD, JUnit + Cucumber, Feature files, Step definitions, Integration examples
19 Testing Exceptions and Edge Cases Edge case identification, Boundary testing, assertThrows, Negative testing, Best practices
20 JUnit Test Reports Generating reports, Maven Surefire plugin, Gradle reports, HTML reports, CI integration
21 Mocking Static Methods Mockito inline, PowerMockito, Limitations, Use cases, Best practices
22 Parameterized and CSV Tests @CsvSource, @CsvFileSource, @MethodSource, Dynamic tests, Practical examples
23 Dynamic Tests @TestFactory, DynamicTest.stream, Custom dynamic tests, Use cases, Best practices
24 Integration Testing with JUnit Introduction, Database tests, REST API testing, Spring integration, Environment setup
25 Code Coverage Jacoco integration, Measuring coverage, Analyzing reports, Coverage thresholds, Best practices
26 Continuous Integration JUnit in CI/CD, Jenkins integration, GitHub Actions, Pipeline setup, Reporting
27 Best Practices in JUnit Writing clean tests, DRY principle, Readable assertions, Test naming conventions, Test isolation
28 Debugging Unit Tests Using IDE debugger, Common failures, Stack traces, Logging in tests, Fixing flaky tests
29 Advanced Assertions assertAll, assertIterableEquals, assertLinesMatch, assertTimeout, Custom assertions
30 JUnit Projects & Labs Hands-on projects, Full coverage examples, Spring Boot testing, CI/CD integration, Practice exercises

Interview question

1. JUnit Basics

  1. What is JUnit and why is it used?
  2. Explain the differences between JUnit 4 and JUnit 5.
  3. What are the advantages of using JUnit in Java projects?
  4. How do you write your first JUnit test case?
  5. What is the naming convention for test methods?
  6. Explain the role of the @Test annotation.
  7. What is the default test runner in JUnit?
  8. How do you disable a test in JUnit?
  9. What are assumptions in JUnit?
  10. Explain JUnit?s role in TDD (Test-Driven Development).
  11. What is the difference between unit tests and integration tests?
  12. How do you set up a JUnit environment in a Maven project?
  13. Can JUnit be used for testing private methods?
  14. What is the default order of test execution in JUnit?
  15. How do you test void methods in JUnit?
  16. How do you skip tests conditionally?
  17. What is the difference between JUnit and TestNG?
  18. Explain the concept of test lifecycle in JUnit.
  19. What is the purpose of @DisplayName in JUnit 5?
  20. How do you tag and filter tests in JUnit?

2. Annotations

  1. Explain the usage of @BeforeEach and @AfterEach.
  2. What is the purpose of @BeforeAll and @AfterAll?
  3. How do you create a setup method in JUnit?
  4. Difference between @BeforeClass (JUnit 4) and @BeforeAll (JUnit 5).
  5. What happens if @BeforeAll is not static?
  6. Can you use multiple annotations on the same method?
  7. How do you use @Disabled in JUnit 5?
  8. What is @RepeatedTest in JUnit 5?
  9. Explain @Nested test classes.
  10. How is @TestFactory used in dynamic tests?
  11. Explain @Tag annotation with examples.
  12. What is the difference between @Test and @ParameterizedTest?
  13. What is @ExtendWith used for?
  14. How do you use @TempDir in JUnit?
  15. What does @Timeout do in JUnit 5?
  16. Can you annotate constructors in JUnit with @BeforeEach?
  17. Explain @EnabledOnOs and @EnabledOnJre annotations.
  18. How do you handle conditional test execution using annotations?
  19. What is @Order used for in JUnit tests?
  20. Can annotations be customized in JUnit?

3. Assertions

  1. What is an assertion in JUnit?
  2. Difference between assertEquals and assertSame.
  3. How do you test for exceptions using assertions?
  4. Explain the usage of assertTrue and assertFalse.
  5. What is assertNull and assertNotNull?
  6. How do you compare arrays in JUnit?
  7. Explain assertAll with example.
  8. What is the difference between fail() and assertThrows()?
  9. How do you write custom assertions?
  10. What is the purpose of Hamcrest in JUnit assertions?
  11. Explain the difference between Hamcrest and AssertJ.
  12. How do you use assertLinesMatch in JUnit?
  13. What is assertIterableEquals used for?
  14. How do you write assertions for collections?
  15. Explain assertTimeout and assertTimeoutPreemptively.
  16. What is the difference between soft and hard assertions?
  17. Can you write assertions for floating-point numbers?
  18. How do you compare objects in JUnit tests?
  19. Explain the usage of assertDoesNotThrow.
  20. What happens when an assertion fails?

4. Parameterized Tests

  1. What is a parameterized test in JUnit?
  2. How do you write a parameterized test using @ValueSource?
  3. Difference between @CsvSource and @CsvFileSource.
  4. How do you test with enum values in JUnit?
  5. How do you write parameterized tests with @MethodSource?
  6. Explain how arguments are resolved in parameterized tests.
  7. How do you test with multiple parameters?
  8. What are custom argument providers?
  9. How do you reuse test data across parameterized tests?
  10. What is the advantage of parameterized tests?
  11. How do you use ArgumentsAccessor?
  12. What is @ArgumentsSource annotation?
  13. How do you test edge cases with parameterized tests?
  14. Explain the difference between parameterized tests in JUnit 4 vs JUnit 5.
  15. Can parameterized tests be combined with @BeforeEach?
  16. What happens when parameterized test data is invalid?
  17. How do you test null inputs in parameterized tests?
  18. What are some best practices for parameterized testing?
  19. How do you test with complex objects?
  20. Can you combine parameterized and dynamic tests?

5. Test Suites

  1. What is a test suite in JUnit?
  2. How do you run multiple test classes together?
  3. Explain @SelectPackages annotation.
  4. Explain @SelectClasses annotation.
  5. How do you include/exclude tests in a suite?
  6. Can test suites be nested?
  7. Difference between JUnit 4 @Suite and JUnit 5 suite engine.
  8. How do you run test suites from Maven?
  9. How do you integrate test suites with Gradle?
  10. What is the benefit of test suites?
  11. Can you use filters with test suites?
  12. How do you configure test discovery?
  13. How do you execute only tagged tests in a suite?
  14. What happens if a suite includes disabled tests?
  15. How do you execute JUnit 4 suites inside JUnit 5?
  16. What is @IncludeClassNamePatterns?
  17. What is @ExcludeTags used for?
  18. Explain discovery selectors in JUnit.
  19. What are suite-level lifecycle methods?
  20. How do you create custom suite runners?

6. Mockito & JUnit

  1. What is Mockito?
  2. How do you create a mock in JUnit?
  3. What is the purpose of @Mock annotation?
  4. How do you use @InjectMocks in tests?
  5. Explain stubbing in Mockito.
  6. What is verify() used for?
  7. How do you reset a mock?
  8. How do you use ArgumentCaptor?
  9. What is the difference between spy() and mock()?
  10. How do you mock exceptions in JUnit tests?
  11. Can you mock static methods with Mockito?
  12. How do you mock final classes?
  13. What is the difference between real and mock objects?
  14. How do you mock collections?
  15. How do you handle void methods in Mockito?
  16. What is the difference between @MockBean and @Mock?
  17. How do you mock private methods?
  18. Explain deep stubs in Mockito.
  19. How do you verify the number of interactions?
  20. What are common pitfalls in using mocks?

7. Spring & JUnit Integration

  1. How do you write a test with @SpringBootTest?
  2. What is @MockBean used for?
  3. Explain the difference between @Mock and @MockBean.
  4. How do you load application context in JUnit?
  5. How do you test Spring MVC controllers?
  6. What is @DataJpaTest used for?
  7. How do you test REST APIs using MockMvc?
  8. Explain @WebMvcTest annotation.
  9. How do you test Spring Boot configuration classes?
  10. How do you handle transactions in Spring tests?
  11. How do you use @TestConfiguration?
  12. What is @AutoConfigureMockMvc?
  13. How do you test caching in Spring?
  14. Explain the use of @Sql annotation in testing.
  15. How do you test services with external dependencies?
  16. What is TestEntityManager used for?
  17. How do you test asynchronous methods in Spring?
  18. Explain how to use TestRestTemplate.
  19. How do you test application events in Spring?
  20. What is the role of @ActiveProfiles in testing?

8. Exception Testing

  1. How do you test exceptions in JUnit 4?
  2. How do you test exceptions in JUnit 5?
  3. Explain the usage of assertThrows.
  4. What is ExpectedException in JUnit 4?
  5. How do you test custom exceptions?
  6. How do you verify exception messages?
  7. How do you test multiple exceptions in a single method?
  8. Can you test checked and unchecked exceptions differently?
  9. How do you test exceptions in parameterized tests?
  10. What is the difference between fail() and assertThrows()?
  11. How do you test runtime exceptions?
  12. How do you test for null pointer exceptions?
  13. How do you combine assertions with exception testing?
  14. How do you log exceptions during tests?
  15. How do you suppress exceptions in JUnit?
  16. How do you create reusable exception assertions?
  17. What happens when no exception is thrown in assertThrows?
  18. Can you test exceptions in dynamic tests?
  19. How do you handle exception hierarchies?
  20. Explain common pitfalls in exception testing.

9. Test Execution & Reports

  1. How do you run JUnit tests from Eclipse/IntelliJ?
  2. How do you run tests using Maven?
  3. How do you run tests using Gradle?
  4. How do you execute tests from the command line?
  5. How do you run a single test class?
  6. How do you run a single test method?
  7. How do you generate XML test reports?
  8. How do you generate HTML test reports?
  9. What is the Surefire plugin in Maven?
  10. How do you configure test execution in Gradle?
  11. How do you run tests in parallel?
  12. How do you configure timeout for all tests?
  13. How do you rerun failed tests?
  14. How do you ignore flaky tests?
  15. How do you integrate JUnit tests with Jenkins?
  16. How do you configure reporting plugins?
  17. How do you publish test reports in CI/CD?
  18. How do you measure code coverage with JUnit?
  19. How do you generate Jacoco reports?
  20. What are best practices in test reporting?

10. JUnit Extensions & Best Practices

  1. What is the extension model in JUnit 5?
  2. How do you write a simple JUnit extension?
  3. What is @ExtendWith used for?
  4. What built-in extensions are available in JUnit 5?
  5. How do you use the TempDir extension?
  6. How do you use the Timeout extension?
  7. What is ExtensionContext in JUnit?
  8. How do you chain multiple extensions?
  9. How do you share state across extensions?
  10. How do you implement a logging extension?
  11. What are some best practices in writing unit tests?
  12. How do you name your test methods effectively?
  13. What is the Arrange-Act-Assert pattern?
  14. How do you avoid flaky tests?
  15. How do you organize test packages?
  16. How do you reuse common test data?
  17. How do you make tests maintainable?
  18. What are some anti-patterns in testing?
  19. How do you improve performance of test suites?
  20. What are enterprise-level strategies for JUnit testing?

Related Topics