testing library
What a test of the data layer needs: the slice of a @DataTest class
(the application's data beans, an in-memory database) and the
transaction that is rolled back around each test.
A test class marked @DataTest() needs none of it by hand:
spring_core_dart_generator writes the calls into the generated
test/spring/<name>.test.spring.dart. Import this library to write the
same tests as functions, with springBootTest of
spring_boot_starter_test (a dev_dependency; this starter does not
depend on it, the generated code and the test join the two):
import 'package:spring_boot_starter_data/spring_boot_starter_data.dart';
import 'package:spring_boot_starter_data/testing.dart';
import 'package:spring_boot_starter_test/spring_boot_starter_test.dart';
import 'package:test/test.dart';
import '../bin/shop.spring.dart'; // springApplication
void main() {
// The repositories of the shop on an in-memory SQLite database.
final data = DataTestSetup(springApplication);
final app = springBootTest(
springApplication,
properties: data.properties,
initializers: [data.initializer],
);
test('saves a book, rolled back afterwards', () {
return runInTestTransaction(app.context, () async {
final books = app.getBean<BookRepository>();
await books.save(Book(title: 'Dune'));
expect(await books.count(), 1);
});
});
}
Spring (Java): @DataJdbcTest, @AutoConfigureTestDatabase and the
transaction of @Transactional tests (spring-boot-test-autoconfigure
and spring-test).
Classes
- DataTestSetup
-
What makes a test context the slice of a
@DataTest: the data beans of an application and the starter's auto-configuration, an in-memory database, and nothing else. - DataTestSlice
-
Removes the beans of an application that a
@DataTestleaves out, before any bean is created: its repositories and the other data beans stay, its services, controllers and other components go. - TestDatabase
-
Replaces the SQL databases of the properties by in-memory SQLite ones
before the context starts, as
@DataTestdoes: eachspring.datasource.urlandspring.datasources.<name>.urlis set tosqlite::memory:.
Functions
-
runInTestTransaction(
BeanFactory beans, FutureOr< void> body(), {bool rollback = true, String? transactionManager, List<FutureOr< before = const [], List<void> Function()>FutureOr< after = const []}) → Future<void> Function()>void> -
Runs a test in a transaction of every
PlatformTransactionManagerits repositories write through, and rolls them back afterwards (or commits them, withrollback: false): what a@DataTestclass does around each test.