
# Differences from Spring

`spring_boot_starter_test` follows Spring Boot's test support: `@SpringBootTest`, `@TestBean`, `MockMvc`, `ApplicationContextRunner`, Testcontainers. This page lists where it does not, and why.

## Why

Dart's test runner is `package:test`, a test file is a `main` function, and there is no reflection to create mocks or scan test classes at run time. The starter keeps Spring's names and shapes, and builds them on functions the generator can write.

## Example

```dart
// Here: a function, with test classes generated into it
final app = springBootTest(springApplication, profiles: ['test']);
```

```java
// Spring: an annotated class
@SpringBootTest
@ActiveProfiles("test")
class ShopTest { ... }
```

## The differences

| Spring Boot | Here | Why |
|---|---|---|
| `@SpringBootTest` on a class | `springBootTest` for the tests of a `main` or `group`; classes are generated into it, with `void main() => runSpringTests();` | A test file is a `main` function |
| The context cache shares contexts across test classes | Each `springBootTest` starts its own context, shared by the tests of its `main` or `group` | Each test file is compiled and run on its own |
| `@MockitoBean` generates mocks and spies, reset after each test | `BeanOverride` and `@TestBean` take a ready-made object; nothing is reset | No reflection; use any mock library and reset in `setUp` |
| An override without an existing bean fails | The instance is added | |
| `@AutoConfigureMockMvc` | Not needed: `app.mockMvc` always works with the web starter | |
| `status().isOk()`, `jsonPath(...)` | `andExpect(status:, json:)` with matchers | Dart's `matcher` |
| `WebTestClient`, `TestRestTemplate` | None: a Feign client or `HttpClient` against `localServerPort` | |
| Test slices (`@WebMvcTest`, `@DataJpaTest`...) and `@TestConfiguration` | Only `@DataTest` of `spring_boot_starter_data`; extra modules or `ApplicationContextRunner` for a part of the configuration | |
| Tests see the machine's environment | Empty unless `systemEnvironment` (`@TestEnvironment`) sets it | Tests that do not depend on the machine |
| `@DirtiesContext` with `classMode` and `methodMode` | On a class: after each test; on a method: after it; no modes | Each class has its own context anyway |
| `@DynamicPropertySource` static method, `DynamicPropertyRegistrar` beans | `dynamicProperties:` parameter, or a static `@DynamicPropertySource()` method; no registrar beans | |
| `@Nested` inner classes | `@Nested()` fields holding an instance | Dart has no inner classes |
| Constructor injection in test classes | Fields only | The generated code creates the class |
| `OutputCaptureExtension` | `captureLogs` and `@CapturedOutput()` for `SpringLog` records | |
| `ApplicationContextRunner.withBean(Class, Supplier)` | `withBean<T>(supplier)`; `withBeanOverride` and `withSystemEnvironment` added | Type arguments instead of classes |
| `withAllowCircularReferences(true)` | Fails every run | No early references |
| `@Testcontainers` fails without Docker | The class is skipped; `CONTAINERS_REQUIRED=true` makes it fail | |
| Testcontainers' modules, networks, Compose | `PostgresContainer`, `KeycloakContainer`, `GenericContainer`, `Network`; no Compose | |

## Name clash

`package:test` (through `matcher`) and `spring_core_dart` both define `Description` (`spring_core_dart`'s is the `@Description` annotation). It matters only when a test file uses it: `import 'package:test/test.dart' hide Description;`.

## See also

- [spring_boot_starter_test](index.md)
- [Differences of spring_boot_dart](../spring-boot-dart/differences.md)
