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.00
# Spring Boot Starter Test
1
2
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.
3
4
## Package Information
5
6
- **Package Name**: spring-boot-starter-test
7
- **Package Type**: maven
8
- **Language**: Java
9
- **Installation**:
10
```xml
11
<dependency>
12
<groupId>org.springframework.boot</groupId>
13
<artifactId>spring-boot-starter-test</artifactId>
14
<scope>test</scope>
15
</dependency>
16
```
17
18
## Core Imports
19
20
```java
21
// Main test annotation
22
import org.springframework.boot.test.context.SpringBootTest;
23
24
// Mock integration (deprecated - use Spring Framework's MockitoBean/MockitoSpyBean instead)
25
import org.springframework.boot.test.mock.mockito.MockBean;
26
import org.springframework.boot.test.mock.mockito.SpyBean;
27
28
// Test slices
29
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
30
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
31
import org.springframework.boot.test.autoconfigure.json.JsonTest;
32
33
// Web testing utilities
34
import org.springframework.boot.test.web.client.TestRestTemplate;
35
import org.springframework.test.web.servlet.MockMvc;
36
37
// Context testing
38
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
39
```
40
41
## Basic Usage
42
43
```java
44
import org.springframework.boot.test.context.SpringBootTest;
45
import org.springframework.boot.test.mock.mockito.MockBean; // Deprecated: Use org.springframework.test.context.bean.override.mockito.MockitoBean
46
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
47
import org.junit.jupiter.api.Test;
48
import static org.assertj.core.api.Assertions.assertThat;
49
import static org.mockito.Mockito.when;
50
51
// Example domain class
52
class User {
53
private String email;
54
private String name;
55
56
public User(String email, String name) {
57
this.email = email;
58
this.name = name;
59
}
60
61
public String getEmail() { return email; }
62
public String getName() { return name; }
63
}
64
65
// Example service interface
66
interface UserService {
67
User findByEmail(String email);
68
}
69
70
@SpringBootTest
71
class ApplicationIntegrationTest {
72
73
@MockBean
74
private UserService userService;
75
76
@Test
77
void contextLoads() {
78
// Test that Spring context loads successfully
79
assertThat(userService).isNotNull();
80
}
81
82
@Test
83
void mockBeanWorks() {
84
// Mock a service method
85
when(userService.findByEmail("test@example.com"))
86
.thenReturn(new User("test@example.com", "Test User"));
87
88
User user = userService.findByEmail("test@example.com");
89
assertThat(user.getName()).isEqualTo("Test User");
90
}
91
}
92
```
93
94
## Architecture
95
96
Spring Boot Starter Test is built around several key components:
97
98
- **Test Slices**: Focused testing configurations that load only the parts of the application context needed for specific layers (web, data, JSON, etc.)
99
- **Mock Integration**: Seamless Mockito integration with Spring context through `@MockBean` and `@SpyBean` annotations (deprecated since 3.4.0)
100
- **Context Runners**: Utilities for testing ApplicationContext configurations and auto-configuration
101
- **Auto-Configuration**: Automatic setup of testing infrastructure based on classpath and annotations
102
- **Aggregated Libraries**: Provides access to all major testing libraries through a single dependency
103
104
## Capabilities
105
106
### Core Testing Framework
107
108
Main integration testing capabilities with Spring Boot context loading, configuration, and lifecycle management.
109
110
```java { .api }
111
@Target(ElementType.TYPE)
112
@Retention(RetentionPolicy.RUNTIME)
113
@SpringBootTest
114
public @interface SpringBootTest {
115
String[] properties() default {};
116
String[] args() default {};
117
Class<?>[] classes() default {};
118
WebEnvironment webEnvironment() default WebEnvironment.MOCK;
119
UseMainMethod useMainMethod() default UseMainMethod.WHEN_AVAILABLE;
120
121
enum WebEnvironment {
122
MOCK, RANDOM_PORT, DEFINED_PORT, NONE
123
}
124
125
enum UseMainMethod {
126
YES, NO, WHEN_AVAILABLE
127
}
128
}
129
130
@Target(ElementType.TYPE)
131
@Retention(RetentionPolicy.RUNTIME)
132
public @interface TestConfiguration {
133
}
134
```
135
136
[Core Testing Framework](./core-testing.md)
137
138
### Mock Integration
139
140
Mockito integration with Spring context for seamless mocking of beans and services. Note: `@MockBean` and `@SpyBean` are deprecated since Spring Boot 3.4.0.
141
142
```java { .api }
143
@Target({ElementType.FIELD, ElementType.PARAMETER})
144
@Retention(RetentionPolicy.RUNTIME)
145
public @interface MockBean {
146
String name() default "";
147
Class<?>[] classes() default {};
148
Class<?>[] extraInterfaces() default {};
149
Answers answer() default Answers.RETURNS_DEFAULTS;
150
boolean serializable() default false;
151
MockReset reset() default MockReset.AFTER;
152
}
153
154
@Target({ElementType.FIELD, ElementType.PARAMETER})
155
@Retention(RetentionPolicy.RUNTIME)
156
public @interface SpyBean {
157
String name() default "";
158
Class<?>[] classes() default {};
159
MockReset reset() default MockReset.AFTER;
160
boolean proxyTargetAware() default true;
161
}
162
163
public enum MockReset {
164
BEFORE, AFTER, NONE
165
}
166
```
167
168
[Mock Integration](./mock-integration.md)
169
170
### Test Slices
171
172
Auto-configured test slices for focused testing of specific application layers with minimal context loading.
173
174
```java { .api }
175
// Web layer testing
176
@Target(ElementType.TYPE)
177
@Retention(RetentionPolicy.RUNTIME)
178
public @interface WebMvcTest {
179
Class<?>[] value() default {};
180
Class<?>[] controllers() default {};
181
boolean useDefaultFilters() default true;
182
ComponentScan.Filter[] includeFilters() default {};
183
ComponentScan.Filter[] excludeFilters() default {};
184
String[] properties() default {};
185
}
186
187
// Data layer testing
188
@Target(ElementType.TYPE)
189
@Retention(RetentionPolicy.RUNTIME)
190
public @interface DataJpaTest {
191
Class<?>[] entities() default {};
192
boolean showSql() default true;
193
boolean useDefaultFilters() default true;
194
ComponentScan.Filter[] includeFilters() default {};
195
ComponentScan.Filter[] excludeFilters() default {};
196
String[] properties() default {};
197
}
198
199
// JSON testing
200
@Target(ElementType.TYPE)
201
@Retention(RetentionPolicy.RUNTIME)
202
public @interface JsonTest {
203
boolean useDefaultFilters() default true;
204
ComponentScan.Filter[] includeFilters() default {};
205
ComponentScan.Filter[] excludeFilters() default {};
206
String[] properties() default {};
207
}
208
```
209
210
[Test Slices](./test-slices.md)
211
212
### Web Testing Utilities
213
214
Pre-configured web testing tools including TestRestTemplate, MockMvc integration, and WebTestClient support.
215
216
```java { .api }
217
public class TestRestTemplate extends RestTemplate {
218
public TestRestTemplate();
219
public TestRestTemplate(HttpClientOption... httpClientOptions);
220
public TestRestTemplate(String username, String password, HttpClientOption... httpClientOptions);
221
222
public <T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Object... uriVariables);
223
public <T> ResponseEntity<T> postForEntity(String url, Object request, Class<T> responseType, Object... uriVariables);
224
public TestRestTemplate withBasicAuth(String username, String password);
225
}
226
```
227
228
[Web Testing Utilities](./web-testing.md)
229
230
### JSON Testing
231
232
Specialized JSON testing utilities with marshalling support for Jackson, Gson, and JSON-B.
233
234
```java { .api }
235
public class JacksonTester<T> extends AbstractJsonMarshalTester<T> {
236
public JsonContent<T> write(T object) throws IOException;
237
public T parseObject(String json) throws IOException;
238
public T read(Resource resource) throws IOException;
239
}
240
241
public class JsonContentAssert extends AbstractAssert<JsonContentAssert, CharSequence> {
242
public JsonContentAssert isEqualToJson(CharSequence expected);
243
public JsonContentAssert hasJsonPathValue(CharSequence expression);
244
public JsonContentAssert extractingJsonPathValue(String expression);
245
}
246
```
247
248
[JSON Testing](./json-testing.md)
249
250
### Context Testing
251
252
Application context testing utilities for testing auto-configuration and context behavior.
253
254
```java { .api }
255
public class ApplicationContextRunner extends AbstractApplicationContextRunner<ApplicationContextRunner, ConfigurableApplicationContext, AssertableApplicationContext> {
256
public ApplicationContextRunner withConfiguration(Configurations configurations);
257
public ApplicationContextRunner withPropertyValues(String... pairs);
258
public ApplicationContextRunner withSystemProperties(String... pairs);
259
public ApplicationContextRunner withUserConfiguration(Class<?>... configurationClasses);
260
public ApplicationContextRunner run(ContextConsumer<? super AssertableApplicationContext> consumer);
261
}
262
263
@FunctionalInterface
264
public interface ContextConsumer<C extends ApplicationContext> {
265
void accept(C context) throws Throwable;
266
}
267
```
268
269
[Context Testing](./context-testing.md)
270
271
### Output Capture
272
273
Console output testing utilities for capturing and asserting on system output during tests.
274
275
```java { .api }
276
public interface CapturedOutput {
277
String getOut();
278
String getErr();
279
String getAll();
280
}
281
282
public class OutputCaptureExtension implements BeforeEachCallback, AfterEachCallback, ParameterResolver {
283
// JUnit Jupiter extension for output capture
284
}
285
```
286
287
[Output Capture](./output-capture.md)
288
289
## Key Testing Libraries Included
290
291
This starter automatically provides access to these testing libraries:
292
293
- **JUnit Jupiter**: Modern testing framework with parameterized tests and extensions
294
- **Mockito**: Mock objects and stubbing with Spring integration
295
- **AssertJ**: Fluent assertion library for readable test assertions
296
- **Hamcrest**: Matcher-based assertions for flexible test conditions
297
- **JSONPath**: JSON query language for testing JSON structures
298
- **JSONAssert**: JSON comparison and assertion utilities
299
- **XMLUnit**: XML comparison and testing capabilities
300
- **Awaitility**: Asynchronous system testing with polling and waiting
301
- **Spring Test**: Spring Framework's comprehensive testing support