0% found this document useful (0 votes)
47 views

3. JUnit Interview Questions

Uploaded by

govardhan v
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

3. JUnit Interview Questions

Uploaded by

govardhan v
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

JUnit Interview Questions

1. What is JUnit, and why is it used in Java development?

JUnit is a popular open-source unit testing framework for Java.


It's used to write and run repeatable automated tests to ensure
that code works as expected. JUnit helps developers improve
code quality, catch bugs early, and facilitate refactoring.
JUnit Interview Questions

2. What are the main annotations used in JUnit 5?

The main annotations in JUnit 5 are:

`@Test`: Marks a method as a test method


`@BeforeEach`: Executed before each test method
`@AfterEach`: Executed after each test method
`@BeforeAll`: Executed once before all test methods in the class
`@AfterAll`: Executed once after all test methods in the class
`@Disabled`: Used to disable a test method or class
JUnit Interview Questions

3. How do you assert equality in JUnit?

In JUnit 5, you can assert equality using the `assertEquals()` method


from the `Assertions` class. For example:

Code Screen shot here...

For objects, this method uses the `equals()` method for comparison.
JUnit Interview Questions

4. What is the difference between `@BeforeEach` and `@BeforeAll`?

`@BeforeEach`: The annotated method runs before each test


method in the class. It's used to set up the test environment for
each individual test.
`@BeforeAll`: The annotated method runs once before all test
methods in the class. It must be static and is used for operations
that are expensive and shared by all tests in the class.
JUnit Interview Questions

5. How can you test for exceptions in JUnit 5?

In JUnit 5, you can test for exceptions using the `assertThrows()`


method. For example:

Code Screen shot here...


You can also capture the exception to perform additional
assertions:

Code Screen shot here...


JUnit Interview Questions

6. What is a test suite in JUnit?

A test suite in JUnit is a collection of test classes that can be run


together. In JUnit 5, you can create a test suite using the `@Suite`
annotation:

Code Screen shot here...


JUnit Interview Questions

7. How do you parameterize tests in JUnit 5?

JUnit 5 provides the `@ParameterizedTest` annotation along with


various source annotations like `@ValueSource`, `@CsvSource`,
`@MethodSource`, etc. For example:

Code Screen shot here...


JUnit Interview Questions

8. What is the purpose of the `@DisplayName` annotation?

The `@DisplayName` annotation is used to declare a custom


display name for a test class or test method. This name is used
instead of the default name in test reports and test runners,
making tests more readable and descriptive.
Code Screen shot here...
JUnit Interview Questions

9. How do you use assumptions in JUnit 5?

Assumptions in JUnit 5 are used to run tests only if certain conditions


are met. If an assumption fails, the test is skipped. You can use methods
like `assumeTrue()`, `assumeFalse()`, or `assumingThat()`. For example:

Code Screen shot here...


JUnit Interview Questions

10. What is the difference between `fail()` and `assertFalse()`?

`fail()` unconditionally fails the test. It's often used to mark a


code path that shouldn't be reached or for tests that are not
yet implemented.
`assertFalse()` fails the test only if the given condition is true.
It's used to assert that a specific condition is false.
JUnit Interview Questions

11. How can you time out a test in JUnit 5?

You can use the `@Timeout` annotation to specify a timeout for a


test method:

Code Screen shot here...


JUnit Interview Questions

12. What is the purpose of the `@RepeatedTest` annotation?

The `@RepeatedTest` annotation is used to repeat a test a specified


number of times. It's useful for testing the consistency of results or
for performance testing. For example:

Code Screen shot here...


JUnit Interview Questions

13. How do you disable a test in JUnit 5?

You can disable a test by using the `@Disabled` annotation. This


can be applied to individual test methods or entire test classes:

Code Screen shot here...


JUnit Interview Questions

14. What is the difference between `assertTrue()` and `assertThat()`?

`assertTrue()` is used to assert that a condition is true.


`assertThat()` is part of JUnit's support for more expressive
assertions using matchers. It allows for more readable and
flexible assertions.

Code Screen shot here...


JUnit Interview Questions

15. How can you group tests in JUnit 5?

JUnit 5 provides the `@Tag` annotation to group tests. You can then
run groups of tests based on their tags:

Code Screen shot here...


JUnit Interview Questions

16. What is the purpose of the `@Nested` annotation in JUnit 5?

The `@Nested` annotation is used to create nested test classes. This


allows for a hierarchical structure of tests, which can be useful for
organizing related tests and sharing setup code:
JUnit Interview Questions

17. How do you handle test lifecycle in JUnit 5?

JUnit 5 provides several annotations to manage the test lifecycle:

`@BeforeAll`: Run once before all tests


`@AfterAll`: Run once after all tests
`@BeforeEach`: Run before each test
`@AfterEach`: Run after each test

These annotations help in setting up and tearing down test


environments.
JUnit Interview Questions

18. What is the difference between `assertSame()` and


`assertEquals()`?

`assertSame()` checks if two objects refer to the exact same


object in memory (using `==`).
`assertEquals()` checks if two objects are equal in value
(using the `equals()` method).
JUnit Interview Questions

19. How can you run JUnit tests from the command line?

You can run JUnit tests from the command line using tools like
Maven or Gradle, or directly using the JUnit Console Launcher.
For example, with Maven:

Code Screen shot here...


With the JUnit Console Launcher:

Code Screen shot here...


JUnit Interview Questions

20. What is the purpose of the `@TempDir` annotation in JUnit 5?

The `@TempDir` annotation is used to create and manage temporary


directories for tests. JUnit will create a temporary directory before the
test and delete it after the test:

Code Screen shot here...


JUnit Interview Questions

21. How do you test private methods in JUnit?

Generally, it's recommended to test private methods indirectly


through public methods. However, if necessary, you can use
reflection to access private methods:

Code Screen shot here...


JUnit Interview Questions

22. What is the difference between `@ExtendWith` and


`@RunWith`?

`@RunWith` is used in JUnit 4 to specify a custom test


runner.
`@ExtendWith` is the JUnit 5 equivalent, used to register
extensions. It's more flexible and can be used multiple times
on a test class.
JUnit Interview Questions

23. How can you use mocks in JUnit tests?

While JUnit itself doesn't provide mocking capabilities, it's often


used with mocking frameworks like Mockito. Here's an example
using Mockito with JUnit 5:

Code Screen shot here...


JUnit Interview Questions

24. What is the purpose of the `@TestFactory` annotation?

The `@TestFactory` annotation is used to generate dynamic tests in


JUnit 5. It allows you to create tests programmatically:

Code Screen shot here...


JUnit Interview Questions

25. How do you handle expected exceptions in JUnit 5?

In JUnit 5, you use the `assertThrows()` method to test for expected


exceptions:

Code Screen shot here...


JUnit Interview Questions

26. What is the difference between `fail()` and `Assertions.fail()`?

There is no functional difference. `fail()` is a static import from the


`Assertions` class, while `Assertions.fail()` is the full method call.
The choice between them is usually a matter of coding style and
preference.
JUnit Interview Questions

27. How can you use custom assertions in JUnit?

You can create custom assertions by extending the


`org.junit.jupiter.api.Assertions` class or by using assertion libraries
like AssertJ. Here's a simple example of a custom assertion:

Code Screen shot here...


JUnit Interview Questions

28. What is the purpose of the `@TestMethodOrder` annotation?

The `@TestMethodOrder` annotation is used to specify the order in


which test methods within a test class should be executed. For
example:

Code Screen shot here...


JUnit Interview Questions

29. How do you test asynchronous code in JUnit 5?

JUnit 5 provides support for testing asynchronous code through the


`assertTimeout()` and `assertTimeoutPreemptively()` methods:

Code Screen shot here...


JUnit Interview Questions

30. What is the difference between unit testing and integration


testing?

Unit testing focuses on testing individual components or


functions in isolation, often using mocks or stubs for
dependencies.
Integration testing involves testing how different components
work together, often using real dependencies and external
resources.

You might also like