← All interview questions

Skill Zuno · skillzunos.com · Fullstack Pro

JUnit interview questions

Prepare for JUnit interviews on Skill Zuno. This practice book covers JUnit interview questions across New, Easy, Medium, Hard, and Diff levels so you can work through real interview-style problems on skillzunos.com.

Open JUnit on Skill Zuno

Also see the JUnit tutorial.

JUnit is the standard unit testing framework for Java and Kotlin. JUnit 5, known as JUnit Jupiter, adds a modern annotation-based programming model, parameterised tests, nested tests and extensions. JUnit interviews cover test lifecycle annotations, assertions, parameterised tests, mocking with Mockito, Spring Boot testing and organising fast, reliable test suites.

Sample JUnit interview questions

What is the difference between @BeforeEach and @BeforeAll?

@BeforeEach runs before every test method. @BeforeAll runs once before all tests in the class and must be static, unless the class uses the per-class test instance lifecycle.

How do you test that a method throws an exception?

Use assertThrows(IllegalArgumentException.class, () -> service.call(bad)). It returns the exception so you can also check its message.

What is a parameterised test?

One test method run with several inputs, supplied by @ValueSource, @CsvSource or @MethodSource, which removes copy-pasted tests for each case.

What does @InjectMocks do in Mockito?

It creates the class under test and injects the fields annotated with @Mock into it through its constructor, setters or fields.

Why use @WebMvcTest instead of @SpringBootTest?

@WebMvcTest loads only the web layer, controllers and related beans, so tests start much faster. @SpringBootTest loads the whole application context for integration tests.

What you can practice

  • JUnit interview questions for beginners and experienced developers
  • Level-based practice: New, Easy, Medium, Hard, and Diff
  • Fullstack Pro topics alongside JUnit on skillzunos.com

More Fullstack Pro interview questions

All interview questions