Friday, March 28, 2025

JUnit Interview Questions And Answers

 What is JUnit?

JUnit is a widely used testing framework for Java applications. It provides annotations, assertions, and test runners to simplify the process of writing and running unit tests.

What are annotations in JUnit?

 Annotations in JUnit define the lifecycle of a test. Some common annotations are:

  • @Test: Marks a method as a test method
  • @Before: Runs before each test method (JUnit 4).
  • @After: Runs after each test method (JUnit 4).
  • @BeforeClass: Runs once before all tests (JUnit 4).
  • @AfterClass: Runs once after all tests (JUnit 4).
  • @Ignore: Ignores a test method (JUnit 4).
  • @Test(expected = Exception.class): Tests that the specified exception is thrown (JUnit 4).
What is the difference between @Before and @BeforeClass?
  • @Before: Runs before each individual test method.
  • @BeforeClass: Runs once before all the tests in the test class. It’s a static method.

What is the difference between @After and @AfterClass?

  • @AfterClass: Runs once after all tests have been executed. It’s a static method.
  • @After: Runs after each individual test method.
What are test suites in JUnit?

 A test suite is a collection of test cases that are run together. You can use the @RunWith and @Suite annotations to define a test suite, allowing you to group multiple tests together to run them sequentially.

What is the purpose of assert methods in JUnit?

 Assertions in JUnit are used to check the expected outcomes of tests. Some common assert methods    are:
  • assertTrue(condition): Checks if the condition is true.
  • assertFalse(condition): Checks if the condition is false.

  • assertEquals(expected, actual): Checks if the expected value matches the actual value.

  • assertNotNull(object): Checks if the object is not null.

  • assertNull(object): Checks if the object is null.

What is the @Ignore annotation in JUnit?

The @Ignore annotation is used to temporarily disable a test method or a test class from being executed. This is useful when a test is not relevant or is under development.

What is the purpose of the @Test(expected = Exception.class) annotation in JUnit?

This annotation is used to specify that the test should pass only if the given exception is thrown during the execution of the test method.

How do you handle timeouts in JUnit tests?

You can use the @Test(timeout = 1000) annotation to specify that a test should fail if it takes longer than the given timeout (in milliseconds).

What is the difference between JUnit 4 and JUnit 5?

  • Architecture: JUnit 5 is modular (JUnit Platform, Jupiter, Vintage), while JUnit 4 is monolithic.

  • Annotations: JUnit 5 introduces @BeforeEach, @AfterEach, @BeforeAll, @AfterAll (replacing JUnit 4's @Before, @After, @BeforeClass, @AfterClass).

  • Test Lifecycle: JUnit 5 allows non-static lifecycle methods (@BeforeAll, @AfterAll), whereas JUnit 4 requires static methods.

  • Test Execution Order: JUnit 5 supports customizable execution order with @TestMethodOrder, while JUnit 4 runs tests in code order.

  • Extensions: JUnit 5 uses @ExtendWith for extensions, replacing JUnit 4’s @Rule/@ClassRule.

  • Parameterization: JUnit 5 provides easier parameterized tests with @ParameterizedTest, compared to JUnit 4's @RunWith(Parameterized.class).

  • Exception Handling: JUnit 5 uses assertThrows() for exception assertions, while JUnit 4 uses @Test(expected = ...).

  • Parallel Execution: JUnit 5 supports parallel test execution natively, unlike JUnit 4.

  • Backward Compatibility: JUnit 5 supports JUnit 4 tests via the Vintage engine.

Tuesday, March 25, 2025

Spring Web Interview Questions and Answers

  •  What are REST services ?

REST (Representational State Transfer) is an architectural style for building web services that interact using standard HTTP methods (GET, POST, PUT, DELETE, etc.). It relies on stateless communication and uses resource-based URIs (Uniform Resource Identifiers) for addressing.

Friday, March 21, 2025

Spring Boot Interview Questions and Answers

  • What is SpringBoot ? How it is different from Spring framework ?
Spring Boot is an extension of the Spring Framework that simplifies the development of Java applications by providing auto-configuration, an embedded server, and opinionated defaults.

Saturday, November 26, 2022

Spring Boot 3 : New features and migration

Spring Framework 6, released on November 2022, is the major version change and has many high-impacting features, changes and upgrades into the Spring development ecosystem. Similarly, Spring boot 3 is a major version upgrade after 4.5 years of Spring boot 2. Released on November 2022, Spring boot 3 is based on Spring Framework 6 and has lot of changes. In this article we will go through the features and migration approach to Spring boot 3.

Thursday, November 24, 2022

Microservices Design Patterns : Aggregator Pattern

When breaking the business functionality into several smaller logical pieces of code, it becomes necessary to think about how to collaborate data returned by each service. The aggregator pattern helps to address this problem. In this article we will further understand about the aggregator pattern.

Microservices Design Patterns - API Gateway

When an application is broken into smaller microservices, it brings in few challenges that needs to be addressed. These include abstracting producer information, handling multiple protocols, data transformation or format conversion for different consumers and so on. To handle these challenges we can use an API gateway design pattern. In this article we'll understand further about this pattern.

Friday, October 28, 2022

Microservices Design Patterns - Strangler Pattern

Microservices are getting more and more popular these days. Many companies are migrating their monolith applications to microservices. However this is easier said than done. Decomposing a monolith with microservices comes with a lot of challenges. In this article we will learn about the Strangler Pattern that can be used to decompose or refactor a monolith into microservices.

Saturday, June 11, 2022

Leetcode : Two Sum

Overview:

Two Sum is a programming problem where you are given an array and a target value and you have to find the indices of the elements in the array that add up to the target value. This is an “easy” level problem in LeetCode. In this article, we will attempt to solve this problem

Monday, September 4, 2017

Spring + Quartz : Scheduler Job with failure recovery

Most IT applications need scheduled processing e.g. maintenance, data flow, clean up etc. which are not initiated by user action. To handle this we can schedule jobs for a particular interval. But what if the job fails or server is down? We do not want to miss the data processing for the interval when job was down or failing. This article explains how to handle such scenario using a Spring and Quartz scheduler.

Sunday, December 25, 2016

MapStruct: Transferring data from one bean to another

Converting data from one form to another is a highly utilized concept in IT industry. MapStruct allows annotation based bean conversion by generating mapper implementation at compile time. This makes sure there is no performance overhead at run-time.