Testing
Generated service tests
testCode: "true" in env.yml generates JUnit 5 + Mockito unit tests of the services, with sample
data for every field type (composite keys included) and the CRUD and search methods. They go to
src/test/java under the service package. Entities with access rules run as the application itself
(RunAsSystemExtension); a test of the rules themselves signs in as a user.
Flow tests over HTTP
The samples test the application as a client sees it: @SpringBootTest with MockMvc, signing in
through the generated endpoint and keeping the session. From the pet store's MetricsTest:
@SpringBootTest
@AutoConfigureMockMvc // Boot 4: org.springframework.boot.webmvc.test.autoconfigure
class MetricsTest {
@Autowired MockMvc mvc;
@Test
void eachMetricIsWhatTheBusinessMeansByIt() throws Exception {
MockHttpSession user = signIn("user", "user");
MockHttpSession admin = signIn("admin", "admin");
String shipped = order(user, 1000, 2);
mvc.perform(post("/api/petstore/purchaseOrder/" + shipped + "/ship").session(admin))
.andExpect(status().isOk());
// ...
}
}
What the pet store's tests cover, for example: ShopFlowTest (sign-in, owners, the 409 of a refused
step, a deletable state, Korean messages, 404/405/415 answers), MetricsTest (the exported metric
SQL against the database), SemanticModelTest (every exported column exists).
On Spring Boot 4, @AutoConfigureMockMvc is in org.springframework.boot.webmvc.test.autoconfigure
with spring-boot-starter-webmvc-test.
Ports
- Replace a port:
@MockitoBean CourierApiPort courier;(Spring Boot 3.4+), returning a success, an error body and an exception. - A
@SpringBootTestthat starts the class with@EnableElastiCoregets the port beans (from 3.0.261022-rc1). - A fake controller in the same server needs
webEnvironment = DEFINED_PORTwithhttpPort: spring.
See HTTP ports.
Architecture tests
archTest: true in env.yml generates <Domain>ArchitectureTest once - ArchUnit rules for the
layers: the domain does not depend on outer layers, the application not on adapters or persistence
(ports only), the domain knows no web (and with mapping: xml no jakarta.persistence). It needs
testImplementation 'com.tngtech.archunit:archunit-junit5:1.3.0'.
A Korean JVM
The samples run their tests with -Duser.language=ko -Duser.country=KR, checking that an answer
without Accept-Language is not in the machine's language.