Junit in Spring Boot

1. What is Spring Boot?

Spring Boot is a framework that simplifies creating standalone, production-ready Spring applications. It provides:

  • Auto-configuration
  • Embedded servers (Tomcat/Jetty)
  • Opinionated defaults
  • Easy dependency management

2. What is JUnit?

JUnit is a testing framework for Java, mainly used for unit testing. With Spring Boot, we often use JUnit 5 along with Spring Test to test components, services, or controllers.


3. Setting up Dependencies

For a Maven project, your pom.xml should include:

<dependencies>
    <!-- Spring Boot Starter Web -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- Spring Boot Starter Test -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

The spring-boot-starter-test includes JUnit 5, Mockito, and other testing tools.


4. Example Spring Boot Application

Suppose we have a simple REST API that returns a greeting.

Application Class

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

Controller

package com.example.demo.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GreetingController {

    @GetMapping("/greet")
    public String greet() {
        return "Hello, World!";
    }
}

5. Writing a JUnit Test

We can write a test to check if the /greet endpoint works.

package com.example.demo;

import com.example.demo.controller.GreetingController;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;

public class GreetingControllerTest {

    @Test
    void testGreet() {
        GreetingController controller = new GreetingController();
        String response = controller.greet();
        assertThat(response).isEqualTo("Hello, World!");
    }
}

6. Using Spring Boot Test for Integration

If you want to test the REST API with Spring Boot context:

package com.example.demo;

import com.example.demo.controller.GreetingController;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.web.servlet.MockMvc;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@WebMvcTest(GreetingController.class)
public class GreetingControllerIntegrationTest {

    @Autowired
    private MockMvc mockMvc;

    @Test
    void testGreetEndpoint() throws Exception {
        mockMvc.perform(get("/greet"))
                .andExpect(status().isOk())
                .andExpect(content().string("Hello, World!"));
    }
}

✅ Here:

  • @WebMvcTest loads only the controller layer.
  • MockMvc simulates HTTP requests.

This gives both unit testing (simple logic) and integration testing (API endpoints).

Leave a Reply