SpringTestify is a powerful testing library that simplifies Spring Boot testing with declarative annotations, in-memory database support, and test data generation.
- ✨ Simplified testing - Reduce boilerplate code with custom annotations
- 💾 In-memory database - One-click setup for H2, HSQLDB, Derby
- 🍃 MongoDB support - Embedded MongoDB for document-based testing
- 🌐 Controller testing - Streamlined REST API testing
- 🔧 Service layer testing - Automatic mocking for dependencies
- 📊 Test data generation - Generate data with real-world characteristics
- 🏗️ Modular design - Use only what you need
- ⚡ Smart context caching - Powered by spring-test-smart-context for optimized test execution
<!-- All modules - single dependency -->
<dependency>
<groupId>io.github.acailic</groupId>
<artifactId>springtestify-all</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>test</scope>
</dependency>
<!-- Or include individual modules -->
<dependency>
<groupId>io.github.acailic</groupId>
<artifactId>springtestify-core</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>test</scope>
</dependency>// All modules - single dependency
testImplementation 'io.github.acailic:springtestify-all:1.0-SNAPSHOT'
// Or include individual modules
testImplementation 'io.github.acailic:springtestify-core:1.0-SNAPSHOT'SpringTestify makes testing Spring applications much simpler:
@SpringTestify
@InMemoryDb(type = DbType.H2)
class UserServiceIntegrationTest {
@Autowired
private UserService userService;
@Test
void shouldCreateUser() {
User user = new User("john.doe@example.com", "John Doe");
User createdUser = userService.createUser(user);
assertThat(createdUser.getId()).isNotNull();
}
}Contains fundamental annotations:
@SpringTestify- Main annotation that enables SpringTestify features@InMemoryDb- Configures in-memory database@ControllerTest- Simplifies controller testing@ServiceTest- Simplifies service testing@RepositoryTest- Simplifies repository testing@DataSetup- Loads data from files@GenerateTestData- Generates test data automatically@PerformanceTest- Validates performance requirements
Provides in-memory database support with:
- Multiple database types (H2, HSQLDB, Derby)
- MongoDB support with embedded MongoDB
- Automatic schema migration
- SQL script execution
- Data loading from JSON, CSV, etc.
@SpringTestify
@InMemoryDb(type = DbType.POSTGRES_COMPATIBLE, migrate = true)
@DataSetup(value = "initial-data.sql")
class OrderRepositoryTest {
// Test with PostgreSQL-compatible in-memory database
}Easily test MongoDB applications with embedded MongoDB:
@SpringTestify
@InMemoryDb(type = DbType.MONGODB)
class ProductRepositoryTest {
@Autowired
private MongoTemplate mongoTemplate;
@Autowired
private ProductRepository productRepository;
@Test
void shouldStoreAndRetrieveProduct() {
// Test with embedded MongoDB
}
}Makes testing REST controllers easy:
@ControllerTest(path = "/api/users")
class UserControllerTest extends AbstractControllerTest {
@Test
void shouldGetUsersList() throws Exception {
assertThat(performGet("/"))
.isSuccessful()
.hasJsonPath("$[0].email");
}
}Provides tools for testing service layer with automatic mock creation:
@ServiceTest(UserService.class)
class UserServiceTest extends AbstractServiceTest {
@Autowired
private UserService userService;
@Autowired
private UserRepository userRepository; // Automatically mocked
@Test
void shouldFindUserById() {
// Given
String userId = "1";
User user = new User(userId, "test@example.com");
when(userRepository.findById(userId)).thenReturn(Optional.of(user));
// When
User foundUser = userService.getUserById(userId);
// Then
assertThat(foundUser).isEqualTo(user);
verifyMock(UserRepository.class).findById(userId);
}
}Generates realistic test data using either property-based generation or Faker:
@SpringTestify
// Property-based generation
@GenerateTestData(entity = User.class, count = 10, properties = {"role=ADMIN:2,USER:8"})
// Faker-based generation
@FakerData(
entity = User.class,
count = 5,
fields = {
"name=name.fullName",
"email=internet.emailAddress",
"phoneNumber=phoneNumber.cellPhone",
"address=address.fullAddress"
}
)
class UserServiceIntegrationTest {
@Autowired
private TestDataRegistry testData;
@Test
void shouldFindUsers() {
// Access the generated data
List<User> users = testData.findAll(User.class);
assertThat(users).hasSize(15); // 10 from GenerateTestData + 5 from FakerData
}
}Use Java Faker to generate realistic test data with built-in providers:
@FakerData(
entity = Customer.class,
count = 10,
locale = "en",
fields = {
"firstName=name.firstName",
"lastName=name.lastName",
"email=internet.emailAddress",
"phoneNumber=phoneNumber.cellPhone",
"address=address.streetAddress",
"city=address.city",
"country=address.country",
"company=company.name",
"jobTitle=job.title"
}
)
class CustomerServiceTest {
@Test
void shouldCreateCustomersWithRealisticData() {
List<Customer> customers = testData.findAll(Customer.class);
assertThat(customers)
.hasSize(10)
.allSatisfy(customer -> {
assertThat(customer.getEmail()).contains("@");
assertThat(customer.getPhoneNumber()).isNotBlank();
});
}
}Available Faker Providers:
address- Addresses, cities, countriesancient- Ancient names, heroes, godsanimal- Animal names and typesapp- App names and versionsartist- Artist names and genresaviation- Aircraft, airportsbank- Account numbers, BIC, IBANbook- Book titles, authors, genresbusiness- Company names, buzzwordscat- Cat names and breedschuckNorris- Chuck Norris factscolor- Color names and hex codescommerce- Product names, pricescompany- Company names, industriescrypto- Cryptocurrency, walletsdateTime- Dates and timesdog- Dog names and breedsfood- Dishes, ingredientsgameOfThrones- Characters, houseshacker- Hacker phrasesharryPotter- Characters, locationshipster- Hipster wordsinternet- Emails, domains, URLsjob- Job titles, fieldslordOfTheRings- Characters, locationslorem- Lorem ipsum textmusic- Genres, bandsname- Full names, first/last namesnumber- Random numbersphoneNumber- Phone numberspokemon- Pokemon namesrickAndMorty- Characters, locationsshakespeare- Quotes, playsspace- Planets, galaxiesstock- Stock market termssuperhero- Hero names, powersteam- Team names, sportsuniversity- University namesweather- Weather descriptionsyoda- Yoda quotes
Use the fluent API for more complex data generation:
@GenerateTestData(
entity = Product.class,
count = 20,
properties = {
PropertyValueBuilder.property("category")
.value("ELECTRONICS").count(10)
.value("BOOKS").count(5)
.value("CLOTHING").count(5)
.build(),
PropertyValueBuilder.property("price").range(9.99, 199.99).build()
}
)Annotations can be combined for powerful testing capabilities:
@SpringTestify
@InMemoryDb(type = DbType.H2)
@GenerateTestData(entity = User.class, count = 5)
@GenerateTestData(entity = Order.class, count = 10)
@PerformanceTest(threshold = "100ms")
class ComplexIntegrationTest {
// Your tests with in-memory database and generated data
}For MongoDB applications, SpringTestify provides specialized support:
@SpringTestify
@InMemoryDb(type = DbType.MONGODB)
@GenerateTestData(
entity = Product.class,
count = 15,
properties = {
PropertyValueBuilder.property("category")
.value("ELECTRONICS").count(5)
.value("BOOKS").count(5)
.value("CLOTHING").count(5)
.build()
}
)
class MongoDBIntegrationTest {
@Autowired
private MongoTemplate mongoTemplate;
@Autowired
private ProductRepository productRepository;
@Test
void shouldQueryProductsByCategory() {
// Test MongoDB query methods with pre-generated data
}
}This project is licensed under the MIT License - see the LICENSE file for details.