JUnit

JUnit

Top Interview Questions

About JUnit

 

Introduction to JUnit

JUnit is a popular unit testing framework for Java that helps developers write and run automated tests to ensure their code works as expected. It plays a crucial role in test-driven development (TDD) and continuous integration (CI) processes. JUnit allows developers to test individual units of code—such as methods or classes—independently, helping to identify bugs early in the development cycle.

Originally developed by Kent Beck and Erich Gamma, JUnit has become the de facto standard for Java testing. It is open-source, lightweight, and widely supported by IDEs such as Eclipse, IntelliJ IDEA, and NetBeans, as well as build tools like Maven and Gradle.


What is Unit Testing?

Unit testing is a software testing technique where individual components (units) of an application are tested in isolation. A unit typically refers to the smallest testable part of an application, such as a method or function.

Key goals of unit testing:

  • Verify correctness of code

  • Detect bugs early

  • Improve code quality

  • Facilitate code refactoring

  • Provide documentation through test cases

JUnit makes unit testing easy and systematic for Java applications.


Evolution of JUnit Versions

JUnit has evolved over time to support modern Java features.

JUnit 3

  • Based on inheritance (extends TestCase)

  • Test methods had to start with test

  • Limited annotations

  • Less flexible

JUnit 4

  • Introduced annotations like @Test, @Before, @After

  • Removed the need to extend TestCase

  • More readable and flexible

JUnit 5 (Latest)

JUnit 5 is a major redesign and consists of three sub-projects:

  1. JUnit Platform – Launches testing frameworks

  2. JUnit Jupiter – New programming model and annotations

  3. JUnit Vintage – Supports JUnit 3 and 4 tests

JUnit 5 supports Java 8+ features, including lambda expressions and streams.


Key Features of JUnit

1. Annotations-Based Testing

JUnit uses annotations to define test methods and lifecycle events, making tests clean and readable.

2. Assertions

Assertions verify expected results against actual outcomes.

3. Test Runners

JUnit provides runners to execute tests automatically.

4. Parameterized Tests

Allows running the same test with multiple input values.

5. Integration Support

Easily integrates with CI/CD tools and build frameworks.

6. Exception Testing

JUnit can validate that expected exceptions are thrown.


Common JUnit Annotations

Some of the most widely used JUnit annotations include:

  • @Test – Marks a method as a test case

  • @BeforeEach – Runs before each test

  • @AfterEach – Runs after each test

  • @BeforeAll – Runs once before all tests

  • @AfterAll – Runs once after all tests

  • @Disabled – Skips a test

  • @DisplayName – Custom name for test

  • @ParameterizedTest – Runs test with parameters

These annotations help manage the test lifecycle efficiently.


Assertions in JUnit

Assertions are used to validate test outcomes. If an assertion fails, the test fails.

Common assertion methods include:

  • assertEquals(expected, actual)

  • assertNotEquals()

  • assertTrue(condition)

  • assertFalse(condition)

  • assertNull(object)

  • assertNotNull(object)

  • assertThrows(exception, executable)

Assertions improve code confidence by verifying logic correctness.


Writing a Simple JUnit Test

A basic JUnit test follows three steps:

  1. Arrange – Prepare test data

  2. Act – Execute the method

  3. Assert – Verify the result

Example structure:

  • Create a test class

  • Annotate test methods with @Test

  • Use assertions to validate results

JUnit tests are usually placed in a separate test directory, keeping them independent of production code.


Test Lifecycle in JUnit

JUnit provides lifecycle methods to manage setup and cleanup:

  • Setup methods initialize objects before tests run

  • Teardown methods release resources after tests finish

This ensures tests are isolated, repeatable, and consistent.


Parameterized Tests

Parameterized tests allow running the same test logic with multiple input values. This reduces code duplication and improves coverage.

Examples include:

  • Testing multiple inputs for mathematical operations

  • Validating edge cases

  • Testing boundary values

JUnit supports parameter sources such as:

  • Values

  • CSV files

  • Method sources


Exception Testing

JUnit provides mechanisms to test whether a method throws an expected exception.

This is useful for:

  • Input validation testing

  • Error-handling logic

  • Security checks

JUnit ensures exceptions are thrown only when expected.


Mocking and JUnit

JUnit itself does not provide mocking capabilities, but it integrates seamlessly with frameworks like:

  • Mockito

  • EasyMock

  • PowerMock

Mocking allows developers to simulate dependencies such as databases, APIs, or services, making unit tests faster and more reliable.


JUnit and Test-Driven Development (TDD)

In TDD, developers:

  1. Write tests before code

  2. Write minimal code to pass tests

  3. Refactor code

JUnit is widely used for TDD because it:

  • Encourages small, focused tests

  • Supports rapid feedback

  • Improves design quality


JUnit in Continuous Integration (CI)

JUnit integrates well with CI tools like:

  • Jenkins

  • GitHub Actions

  • GitLab CI

  • Bamboo

CI pipelines automatically run JUnit tests whenever code is committed, ensuring:

  • Early bug detection

  • Code stability

  • Faster releases

JUnit generates test reports that help teams analyze test results easily.


Advantages of JUnit

  • Open-source and free

  • Easy to learn and use

  • Reduces bugs in production

  • Encourages clean code design

  • Saves time and cost in the long run

  • Strong community and documentation

  • Excellent IDE and tool support


Limitations of JUnit

  • Limited to Java ecosystem

  • Requires additional libraries for mocking

  • Not suitable for UI or performance testing

  • Poorly written tests can be hard to maintain

Despite these limitations, JUnit remains ideal for unit testing Java applications.


Best Practices for JUnit Testing

  • Write small, independent tests

  • Use meaningful test names

  • Avoid testing multiple things in one test

  • Mock external dependencies

  • Keep tests fast and repeatable

  • Maintain high test coverage

  • Run tests frequently

Following best practices ensures maintainable and effective test suites.


Real-World Use Cases of JUnit

  • Testing business logic in enterprise applications

  • Validating REST APIs

  • Ensuring correctness of utility methods

  • Regression testing during code changes

  • Supporting agile and DevOps workflows

JUnit is widely used in banking, e-commerce, telecom, healthcare, and enterprise software development.

Fresher Interview Questions

 

1. What is JUnit?

Answer:
JUnit is a unit testing framework for Java used to test individual units of code such as methods or classes. It helps developers ensure that the code works correctly by writing automated test cases.


2. What is unit testing?

Answer:
Unit testing is the process of testing the smallest testable parts of an application, such as methods or functions, to verify that they work as expected independently.


3. Why is JUnit important?

Answer:
JUnit is important because:

  • It helps find bugs early

  • Improves code quality

  • Supports automation testing

  • Makes code easier to maintain

  • Saves time during regression testing


4. What are the main features of JUnit?

Answer:

  • Annotations for defining test methods

  • Assertions to validate expected results

  • Test runners to execute tests

  • Test suites to group multiple tests

  • Supports Test-Driven Development (TDD)


5. What are JUnit annotations?

Answer:
JUnit annotations are special keywords that define how a test method should behave.


6. List common JUnit annotations.

Answer:

  • @Test – Marks a method as a test

  • @BeforeEach – Runs before each test

  • @AfterEach – Runs after each test

  • @BeforeAll – Runs once before all tests

  • @AfterAll – Runs once after all tests

  • @Disabled – Skips a test

  • @DisplayName – Custom test name


7. What is the @Test annotation?

Answer:
@Test is used to indicate that a method is a test case that JUnit should execute.


8. What is an assertion in JUnit?

Answer:
Assertions are used to verify expected outcomes of a test. If an assertion fails, the test fails.


9. List common assertion methods.

Answer:

  • assertEquals()

  • assertNotEquals()

  • assertTrue()

  • assertFalse()

  • assertNull()

  • assertNotNull()

  • assertThrows()


10. What is assertEquals()?

Answer:
It checks whether the expected value matches the actual value.


11. What happens if an assertion fails?

Answer:
The test case fails, and JUnit reports the failure with details.


12. What is @BeforeEach?

Answer:
It runs before every test method and is used for setup tasks like initializing objects.


13. What is @AfterEach?

Answer:
It runs after every test method and is used for cleanup activities.


14. What is @BeforeAll?

Answer:
It runs once before all test methods in the class. The method must be static.


15. What is @AfterAll?

Answer:
It runs once after all tests have executed. The method must be static.


16. What is a test case?

Answer:
A test case is a set of conditions used to verify whether a piece of code works correctly.


17. What is a test suite?

Answer:
A test suite is a collection of test cases that are executed together.


18. What is Test-Driven Development (TDD)?

Answer:
TDD is a development approach where:

  1. Write test cases first

  2. Write code to pass the tests

  3. Refactor the code


19. What is JUnit 4 vs JUnit 5?

Answer:

Feature JUnit 4 JUnit 5
Annotations Limited More advanced
Architecture Single Modular
Java Version Java 5+ Java 8+

20. What is JUnit Jupiter?

Answer:
JUnit Jupiter is the programming and extension model for writing tests in JUnit 5.


21. Can JUnit test private methods?

Answer:
No, JUnit cannot directly test private methods. They should be tested indirectly through public methods.


22. What is @Disabled annotation?

Answer:
It is used to skip or ignore a test method.


23. What is exception testing in JUnit?

Answer:
It verifies whether a method throws an expected exception using assertThrows().


24. What is parameterized testing?

Answer:
Parameterized tests allow running the same test multiple times with different input values.


25. What is mocking?

Answer:
Mocking is creating fake objects to simulate real dependencies during testing.


26. Which mocking framework is commonly used with JUnit?

Answer:
Mockito is commonly used with JUnit.


27. What is regression testing?

Answer:
Regression testing ensures that new changes do not break existing functionality.


28. Can JUnit be used for integration testing?

Answer:
Yes, JUnit can be used for integration testing, but it is mainly designed for unit testing.


29. What IDEs support JUnit?

Answer:

  • Eclipse

  • IntelliJ IDEA

  • NetBeans


30. What is the role of JUnit in CI/CD?

Answer:
JUnit tests are run automatically in CI/CD pipelines to ensure code quality before deployment.


31. What is @DisplayName?

Answer:
It provides a custom name for test methods to improve readability.


32. What is the advantage of automated testing?

Answer:

  • Faster testing

  • Reliable results

  • Reusable tests

  • Reduces manual effort


33. Can JUnit tests be run from the command line?

Answer:
Yes, JUnit tests can be executed using build tools like Maven or Gradle.


34. What is Maven’s role in JUnit?

Answer:
Maven manages dependencies and helps run JUnit tests using plugins like Surefire.


35. Is JUnit open-source?

Answer:
Yes, JUnit is free and open-source.


36. What are the limitations of JUnit?

Answer:

  • Cannot test UI directly

  • Not suitable for performance testing

  • Requires additional tools for mocking


37. Who uses JUnit?

Answer:
Java developers, automation testers, QA engineers, and DevOps teams.


38. When should unit tests be written?

Answer:
Unit tests should be written during development, preferably before or alongside code.


39. What is code coverage?

Answer:
Code coverage measures how much of the code is tested by unit tests.


40. Is JUnit suitable for beginners?

Answer:
Yes, JUnit is easy to learn and highly suitable for beginners in Java testing.


41. What is a Test Runner in JUnit?

Answer:
A Test Runner is a component that executes test cases and displays results such as passed, failed, or ignored tests.
Examples:

  • JUnit Console Runner

  • IDE-based runners (Eclipse, IntelliJ)


42. What is a Test Fixture?

Answer:
A test fixture is a fixed environment used to run tests. It includes:

  • Test data

  • Objects initialization

  • Database connections
    Annotations like @BeforeEach and @AfterEach are used to manage fixtures.


43. What is assertThrows()?

Answer:
assertThrows() checks whether a method throws a specific exception.

Example:

assertThrows(ArithmeticException.class, () -> divide(10, 0));

44. What is assertAll()?

Answer:
assertAll() allows multiple assertions to be executed together. Even if one fails, others still run.


45. What is the difference between assertTrue() and assertEquals()?

Answer:

assertTrue() assertEquals()
Checks condition Compares values
Boolean result Expected vs actual
Used for logic Used for data

46. What is @RepeatedTest?

Answer:
It runs the same test multiple times.

Example:

@RepeatedTest(5)
void repeatTest() { }

47. What is @Timeout?

Answer:
It fails a test if it exceeds a given time limit.


48. What is @Tag?

Answer:
@Tag is used to group and filter tests.

Example:

@Tag("smoke")

49. What is @Nested?

Answer:
@Nested is used to create inner test classes for better structure and readability.


50. What is @ParameterizedTest?

Answer:
It runs the same test with different input values.

Example:

@ParameterizedTest
@ValueSource(ints = {1, 2, 3})

51. What is @CsvSource?

Answer:
It provides multiple arguments using CSV format.

Example:

@CsvSource({"1,2,3", "2,3,5"})

52. What is @MethodSource?

Answer:
It supplies test data from a method.


53. What is a disabled test?

Answer:
A disabled test is skipped during execution using @Disabled.


54. What is the difference between @BeforeEach and @BeforeAll?

Answer:

  • @BeforeEach: Runs before every test

  • @BeforeAll: Runs once before all tests (static)


55. What is a failure vs error in JUnit?

Answer:

Failure Error
Assertion fails Unexpected exception
Test logic issue Runtime issue

56. What is assumeTrue()?

Answer:
It runs a test only if a condition is true; otherwise, the test is skipped.


57. What is test ordering?

Answer:
Test ordering controls the sequence of test execution using @TestMethodOrder.


58. What is @Order?

Answer:
It specifies the order of test execution.


59. What is mocking in unit testing?

Answer:
Mocking simulates real objects to isolate the unit under test.


60. Why is Mockito used with JUnit?

Answer:
Mockito helps create mock objects for:

  • Database calls

  • External services

  • APIs


61. What is a stub?

Answer:
A stub provides fixed responses during testing.


62. What is the difference between mock and stub?

Answer:

Mock Stub
Verifies behavior Returns fixed data
Interaction-based State-based

63. What is @ExtendWith?

Answer:
It integrates extensions like Mockito with JUnit 5.


64. What is dependency injection in tests?

Answer:
Injecting required objects into a class instead of creating them directly.


65. What is code smell in unit tests?

Answer:
Poor test design that makes tests hard to maintain.


66. What is flaky test?

Answer:
A test that passes or fails randomly without code changes.


67. What is test isolation?

Answer:
Each test runs independently without affecting others.


68. Can JUnit tests run in parallel?

Answer:
Yes, JUnit 5 supports parallel execution.


69. What is continuous testing?

Answer:
Running tests automatically whenever code changes.


70. What is Surefire plugin?

Answer:
Maven Surefire plugin executes JUnit tests during the build.


71. What is Failsafe plugin?

Answer:
Used for integration testing in Maven.


72. What is @TempDir?

Answer:
Creates temporary directories for tests.


73. What is @TestFactory?

Answer:
Creates dynamic tests at runtime.


74. What is dynamic testing?

Answer:
Tests generated during execution instead of compile time.


75. What is JUnit Console Launcher?

Answer:
A command-line tool to run JUnit tests.


76. What is test coverage tool?

Answer:
Tools like JaCoCo measure how much code is tested.


77. What is white-box testing?

Answer:
Testing based on internal code structure.


78. What is black-box testing?

Answer:
Testing without knowing internal code.


79. What are best practices in JUnit?

Answer:

  • Write small test cases

  • Use meaningful names

  • Avoid test dependencies

  • Mock external systems


80. What is the future of JUnit?

Answer:
JUnit remains the standard testing framework for Java with strong community support.

Experienced Interview Questions

 

1. How do you design effective unit tests in real projects?

Answer:
Effective unit tests should:

  • Test only one unit of behavior

  • Be independent and isolated

  • Be fast and repeatable

  • Avoid external dependencies using mocks

  • Follow AAA pattern (Arrange, Act, Assert)


2. What challenges have you faced while writing JUnit tests?

Answer:
Common challenges include:

  • Testing legacy code

  • Mocking static or final methods

  • Handling external dependencies

  • Writing maintainable test cases

  • Dealing with flaky tests


3. Explain JUnit 5 architecture.

Answer:
JUnit 5 consists of:

  1. JUnit Platform – Launches testing frameworks

  2. JUnit Jupiter – New programming model and annotations

  3. JUnit Vintage – Supports JUnit 3 & 4 tests


4. What is the difference between unit testing and integration testing?

Answer:

Unit Testing Integration Testing
Tests individual units Tests combined components
Uses mocks Uses real dependencies
Faster Slower
Isolated End-to-end flow

5. How do you handle database testing in JUnit?

Answer:

  • Use in-memory databases (H2)

  • Mock repository layers

  • Use @Transactional with rollback

  • Separate unit and integration tests


6. How do you mock external APIs in JUnit?

Answer:
By using:

  • Mockito for mocking services

  • WireMock for REST APIs

  • TestContainers for real environments


7. Explain @ExtendWith(MockitoExtension.class).

Answer:
It integrates Mockito with JUnit 5 and initializes mock objects automatically.


8. What is TestContainers?

Answer:
TestContainers allows running real dependencies like databases in Docker containers during tests.


9. How do you test exceptions in JUnit 5?

Answer:
Using assertThrows():

assertThrows(CustomException.class, () -> service.process());

10. What is parameterized testing and where have you used it?

Answer:
Parameterized testing allows running a test with multiple inputs.
Used for:

  • Validation logic

  • Boundary testing

  • Business rules


11. Difference between @BeforeEach and @BeforeAll in real use?

Answer:

  • @BeforeEach → Reset state before each test

  • @BeforeAll → Expensive setup like DB connections


12. What is test isolation and why is it important?

Answer:
Each test should run independently to avoid side effects and flaky results.


13. What is a flaky test and how do you fix it?

Answer:
A flaky test behaves inconsistently.
Fix by:

  • Removing timing dependencies

  • Using mocks

  • Resetting shared states

  • Avoiding parallel conflicts


14. How do you enable parallel test execution?

Answer:
JUnit 5 supports parallel execution using configuration properties.


15. What is @TestInstance?

Answer:
Controls test instance lifecycle:

  • PER_METHOD (default)

  • PER_CLASS (useful for shared state)


16. What is dynamic testing in JUnit?

Answer:
Tests created at runtime using @TestFactory.


17. Difference between assertAll() and multiple assertions?

Answer:
assertAll() executes all assertions even if one fails, improving diagnostics.


18. How do you test private methods?

Answer:
Private methods are tested indirectly via public methods. Reflection is avoided.


19. What are assumptions in JUnit?

Answer:
Assumptions (assumeTrue) skip tests when conditions are not met.


20. How do you group and filter tests?

Answer:
Using @Tag and build-tool configurations.


21. What is Maven Surefire vs Failsafe?

Answer:

Surefire Failsafe
Unit tests Integration tests
test phase verify phase

22. How do you measure code coverage?

Answer:
Using tools like JaCoCo, integrated with CI pipelines.


23. What is mutation testing?

Answer:
Mutation testing checks the quality of tests by introducing small changes in code.


24. How do you write maintainable tests?

Answer:

  • Use descriptive method names

  • Avoid duplication

  • Follow naming conventions

  • Use helper methods

  • Keep tests simple


25. Explain mocking vs spying.

Answer:

Mock Spy
Fake object Real object
No real logic Partial real logic
Fully controlled Partial control

26. What is @MockBean?

Answer:
Used in Spring Boot tests to mock beans in the application context.


27. How do you test REST controllers?

Answer:
Using:

  • MockMvc

  • WebTestClient

  • RestAssured


28. What is the role of JUnit in CI/CD?

Answer:
JUnit ensures:

  • Automated regression testing

  • Build validation

  • Early bug detection


29. How do you handle legacy code testing?

Answer:

  • Refactor slowly

  • Add tests before changes

  • Use seams and mocks


30. What is test-driven development (TDD)?

Answer:
Write tests first, then code, then refactor.


31. What is behavior-driven development (BDD)?

Answer:
BDD focuses on behavior using readable scenarios (JUnit + Cucumber).


32. What are common JUnit anti-patterns?

Answer:

  • Overusing mocks

  • Testing implementation details

  • Large test methods

  • Test dependency


33. How do you test time-dependent logic?

Answer:
By mocking clocks or using fixed timestamps.


34. What is @TempDir used for?

Answer:
Creates temporary directories for file system tests.


35. How do you debug failing JUnit tests?

Answer:

  • Use logs

  • Debug mode

  • Check assertions

  • Reproduce locally


36. Difference between JUnit and TestNG?

Answer:
JUnit is simpler and standard; TestNG provides advanced configurations.


37. What is contract testing?

Answer:
Ensures services agree on request-response formats.


38. How do you test microservices using JUnit?

Answer:

  • Unit tests for services

  • Integration tests with TestContainers

  • Contract tests


39. What is the most important JUnit best practice?

Answer:
Tests should be fast, isolated, and readable.


40. What kind of JUnit questions are asked for 4 years?

Answer:

  • Real project scenarios

  • Mockito integration

  • CI/CD usage

  • Performance & reliability

  • Debugging flaky tests


41. How do you decide what should and should not be unit tested?

Answer:
Unit test:

  • Business logic

  • Calculations

  • Decision-making code

Do not unit test:

  • Framework code

  • Getters/setters

  • Third-party libraries


42. How do you test void methods in JUnit?

Answer:
By:

  • Verifying state changes

  • Verifying interactions using Mockito (verify())


43. How do you verify method calls in JUnit?

Answer:
Using Mockito:

verify(service).process();

44. What is ArgumentCaptor and when do you use it?

Answer:
ArgumentCaptor captures arguments passed to a mocked method for validation.


45. Difference between @Mock and @InjectMocks?

Answer:

@Mock @InjectMocks
Creates mock Injects mocks
Standalone object Tested class

46. What is @Spy and when to use it?

Answer:
A spy wraps a real object and allows partial mocking.


47. How do you mock static methods?

Answer:
Using Mockito (inline mock maker) or PowerMockito.


48. What is PowerMockito?

Answer:
PowerMockito is used to mock:

  • Static methods

  • Constructors

  • Final classes


49. How do you test scheduled jobs?

Answer:

  • Trigger methods directly

  • Mock time

  • Avoid real scheduling in unit tests


50. How do you test asynchronous code?

Answer:

  • Use CompletableFuture

  • Awaitility library

  • Timeouts in assertions


51. How do you test file upload/download logic?

Answer:
Using @TempDir and mock multipart files.


52. What is @DirtiesContext?

Answer:
Resets Spring context after test execution.


53. How do you manage test data?

Answer:

  • Builders

  • Test factories

  • Separate test resources


54. What is the Arrange-Act-Assert pattern?

Answer:

  • Arrange: Setup data

  • Act: Execute method

  • Assert: Verify result


55. What is test refactoring?

Answer:
Improving test structure without changing behavior.


56. How do you test REST APIs with JUnit?

Answer:

  • MockMvc

  • RestAssured

  • WebTestClient


57. What is @WebMvcTest?

Answer:
Loads only web layer components for controller testing.


58. What is @SpringBootTest?

Answer:
Loads full application context.


59. Difference between @MockBean and @Mock?

Answer:
@MockBean replaces Spring bean, @Mock is standalone.


60. What is contract testing and why is it needed?

Answer:
Ensures services communicate correctly without breaking changes.


61. How do you handle environment-specific tests?

Answer:
Using profiles and assumptions.


62. How do you test security logic?

Answer:

  • Mock authentication

  • Use Spring Security Test support


63. What is @ActiveProfiles?

Answer:
Activates specific Spring profiles during tests.


64. What is Test Pyramid?

Answer:
A model defining ratio of:

  • Unit tests (majority)

  • Integration tests

  • UI tests


65. How do you reduce test execution time?

Answer:

  • Parallel execution

  • Mock dependencies

  • Avoid full context loading


66. What is mutation testing?

Answer:
Introduces faults to verify test effectiveness.


67. What is @DisabledOnOs?

Answer:
Disables tests for specific operating systems.


68. How do you test email functionality?

Answer:
Mock mail sender and verify interactions.


69. What is a test seam?

Answer:
A place where behavior can be altered for testing.


70. How do you handle flaky integration tests?

Answer:

  • Isolate resources

  • Retry logic

  • Use containers


71. How do you test caching logic?

Answer:
Verify cache hits/misses using mocks.


72. How do you test message queues?

Answer:
Use embedded brokers or TestContainers.


73. What is @Nested used for in large test classes?

Answer:
Improves structure and readability.


74. What is @TestMethodOrder?

Answer:
Controls execution order of test methods.


75. How do you test configuration properties?

Answer:
Use @TestPropertySource.


76. What is @DynamicPropertySource?

Answer:
Provides dynamic properties at runtime.


77. How do you test retry logic?

Answer:
Mock failures and verify retries.


78. What is the difference between stubbing and mocking?

Answer:
Stubbing returns data; mocking verifies behavior.


79. How do you ensure tests are readable?

Answer:

  • Descriptive names

  • @DisplayName

  • Clear structure


80. What are red flags in JUnit interviews?

Answer:

  • Overusing mocks

  • Not understanding test isolation

  • Writing brittle tests


81. How do you handle legacy systems without tests?

Answer:

  • Add characterization tests

  • Refactor gradually


82. How do you test feature flags?

Answer:
Mock flag states.


83. How do you test pagination logic?

Answer:
Verify boundaries and page sizes.


84. How do you test concurrency issues?

Answer:
Use parallel tests and synchronization checks.


85. What is the most common mistake in JUnit testing?

Answer:
Testing implementation instead of behavior.


86. How do you explain JUnit expertise in interviews?

Answer:
By discussing:

  • Real problems

  • Testing strategies

  • Tool integration


87. What makes a good unit test?

Answer:
Fast, reliable, readable, and maintainable.


88. How do you handle CI test failures?

Answer:

  • Analyze logs

  • Reproduce locally

  • Fix root cause


89. How do you test configuration-driven behavior?

Answer:
Override properties in tests.


90. Final interviewer favorite question: How do you improve test quality?

Answer:

  • Code reviews

  • Mutation testing

  • Refactoring tests