Starter for testing Spring Boot applications with libraries including JUnit Jupiter, Hamcrest and Mockito
npx @tessl/cli install tessl/maven-org-springframework-boot--spring-boot-starter-test@3.5.0Spring 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.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>// 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;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");
}
}Spring Boot Starter Test is built around several key components:
@MockBean and @SpyBean annotations (deprecated since 3.4.0)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 {
}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
}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 {};
}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);
}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);
}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;
}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
}This starter automatically provides access to these testing libraries: