CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-springframework-boot--spring-boot-starter-test

Starter for testing Spring Boot applications with libraries including JUnit Jupiter, Hamcrest and Mockito

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

index.mddocs/

Spring Boot Starter Test

Spring Boot Starter Test is a comprehensive testing starter that provides a complete testing framework for Spring Boot applications. It aggregates essential testing libraries including JUnit Jupiter, Mockito, Hamcrest, AssertJ, Awaitility, and Spring Boot's own testing framework with auto-configuration for test slices, mock integration, and specialized testing utilities.

Package Information

  • Package Name: spring-boot-starter-test
  • Package Type: maven
  • Language: Java
  • Installation:
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>

Core Imports

// Main test annotation
import org.springframework.boot.test.context.SpringBootTest;

// Mock integration (deprecated - use Spring Framework's MockitoBean/MockitoSpyBean instead)
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.mock.mockito.SpyBean;

// Test slices
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.autoconfigure.json.JsonTest;

// Web testing utilities
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.test.web.servlet.MockMvc;

// Context testing
import org.springframework.boot.test.context.runner.ApplicationContextRunner;

Basic Usage

import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean; // Deprecated: Use org.springframework.test.context.bean.override.mockito.MockitoBean
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;

// Example domain class
class User {
    private String email;
    private String name;
    
    public User(String email, String name) {
        this.email = email;
        this.name = name;
    }
    
    public String getEmail() { return email; }
    public String getName() { return name; }
}

// Example service interface
interface UserService {
    User findByEmail(String email);
}

@SpringBootTest
class ApplicationIntegrationTest {

    @MockBean
    private UserService userService;

    @Test
    void contextLoads() {
        // Test that Spring context loads successfully
        assertThat(userService).isNotNull();
    }

    @Test
    void mockBeanWorks() {
        // Mock a service method
        when(userService.findByEmail("test@example.com"))
            .thenReturn(new User("test@example.com", "Test User"));
        
        User user = userService.findByEmail("test@example.com");
        assertThat(user.getName()).isEqualTo("Test User");
    }
}

Architecture

Spring Boot Starter Test is built around several key components:

  • Test Slices: Focused testing configurations that load only the parts of the application context needed for specific layers (web, data, JSON, etc.)
  • Mock Integration: Seamless Mockito integration with Spring context through @MockBean and @SpyBean annotations (deprecated since 3.4.0)
  • Context Runners: Utilities for testing ApplicationContext configurations and auto-configuration
  • Auto-Configuration: Automatic setup of testing infrastructure based on classpath and annotations
  • Aggregated Libraries: Provides access to all major testing libraries through a single dependency

Capabilities

Core Testing Framework

Main integration testing capabilities with Spring Boot context loading, configuration, and lifecycle management.

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@SpringBootTest
public @interface SpringBootTest {
    String[] properties() default {};
    String[] args() default {};
    Class<?>[] classes() default {};
    WebEnvironment webEnvironment() default WebEnvironment.MOCK;
    UseMainMethod useMainMethod() default UseMainMethod.WHEN_AVAILABLE;
    
    enum WebEnvironment {
        MOCK, RANDOM_PORT, DEFINED_PORT, NONE
    }
    
    enum UseMainMethod {
        YES, NO, WHEN_AVAILABLE
    }
}

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface TestConfiguration {
}

Core Testing Framework

Mock Integration

Mockito integration with Spring context for seamless mocking of beans and services. Note: @MockBean and @SpyBean are deprecated since Spring Boot 3.4.0.

@Target({ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface MockBean {
    String name() default "";
    Class<?>[] classes() default {};
    Class<?>[] extraInterfaces() default {};
    Answers answer() default Answers.RETURNS_DEFAULTS;
    boolean serializable() default false;
    MockReset reset() default MockReset.AFTER;
}

@Target({ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface SpyBean {
    String name() default "";
    Class<?>[] classes() default {};
    MockReset reset() default MockReset.AFTER;
    boolean proxyTargetAware() default true;
}

public enum MockReset {
    BEFORE, AFTER, NONE
}

Mock Integration

Test Slices

Auto-configured test slices for focused testing of specific application layers with minimal context loading.

// Web layer testing
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface WebMvcTest {
    Class<?>[] value() default {};
    Class<?>[] controllers() default {};
    boolean useDefaultFilters() default true;
    ComponentScan.Filter[] includeFilters() default {};
    ComponentScan.Filter[] excludeFilters() default {};
    String[] properties() default {};
}

// Data layer testing
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface DataJpaTest {
    Class<?>[] entities() default {};
    boolean showSql() default true;
    boolean useDefaultFilters() default true;
    ComponentScan.Filter[] includeFilters() default {};
    ComponentScan.Filter[] excludeFilters() default {};
    String[] properties() default {};
}

// JSON testing
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface JsonTest {
    boolean useDefaultFilters() default true;
    ComponentScan.Filter[] includeFilters() default {};
    ComponentScan.Filter[] excludeFilters() default {};
    String[] properties() default {};
}

Test Slices

Web Testing Utilities

Pre-configured web testing tools including TestRestTemplate, MockMvc integration, and WebTestClient support.

public class TestRestTemplate extends RestTemplate {
    public TestRestTemplate();
    public TestRestTemplate(HttpClientOption... httpClientOptions);
    public TestRestTemplate(String username, String password, HttpClientOption... httpClientOptions);
    
    public <T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Object... uriVariables);
    public <T> ResponseEntity<T> postForEntity(String url, Object request, Class<T> responseType, Object... uriVariables);
    public TestRestTemplate withBasicAuth(String username, String password);
}

Web Testing Utilities

JSON Testing

Specialized JSON testing utilities with marshalling support for Jackson, Gson, and JSON-B.

public class JacksonTester<T> extends AbstractJsonMarshalTester<T> {
    public JsonContent<T> write(T object) throws IOException;
    public T parseObject(String json) throws IOException;
    public T read(Resource resource) throws IOException;
}

public class JsonContentAssert extends AbstractAssert<JsonContentAssert, CharSequence> {
    public JsonContentAssert isEqualToJson(CharSequence expected);
    public JsonContentAssert hasJsonPathValue(CharSequence expression);
    public JsonContentAssert extractingJsonPathValue(String expression);
}

JSON Testing

Context Testing

Application context testing utilities for testing auto-configuration and context behavior.

public class ApplicationContextRunner extends AbstractApplicationContextRunner<ApplicationContextRunner, ConfigurableApplicationContext, AssertableApplicationContext> {
    public ApplicationContextRunner withConfiguration(Configurations configurations);
    public ApplicationContextRunner withPropertyValues(String... pairs);
    public ApplicationContextRunner withSystemProperties(String... pairs);
    public ApplicationContextRunner withUserConfiguration(Class<?>... configurationClasses);
    public ApplicationContextRunner run(ContextConsumer<? super AssertableApplicationContext> consumer);
}

@FunctionalInterface
public interface ContextConsumer<C extends ApplicationContext> {
    void accept(C context) throws Throwable;
}

Context Testing

Output Capture

Console output testing utilities for capturing and asserting on system output during tests.

public interface CapturedOutput {
    String getOut();
    String getErr();
    String getAll();
}

public class OutputCaptureExtension implements BeforeEachCallback, AfterEachCallback, ParameterResolver {
    // JUnit Jupiter extension for output capture
}

Output Capture

Key Testing Libraries Included

This starter automatically provides access to these testing libraries:

  • JUnit Jupiter: Modern testing framework with parameterized tests and extensions
  • Mockito: Mock objects and stubbing with Spring integration
  • AssertJ: Fluent assertion library for readable test assertions
  • Hamcrest: Matcher-based assertions for flexible test conditions
  • JSONPath: JSON query language for testing JSON structures
  • JSONAssert: JSON comparison and assertion utilities
  • XMLUnit: XML comparison and testing capabilities
  • Awaitility: Asynchronous system testing with polling and waiting
  • Spring Test: Spring Framework's comprehensive testing support

docs

context-testing.md

core-testing.md

index.md

json-testing.md

mock-integration.md

output-capture.md

test-slices.md

web-testing.md

tile.json