Package com.codename1.backend.test
Testing a backend the way Spring Boot tests are written, on the JVM and as a compiled native test.
A class annotated BackendTest gets the application the build wired, started
in the test with the test profile -- an in-memory database, tables created --
and its @Autowired fields filled from it:
@BackendTest
class GreeterApiTest {
@Autowired MockMvc mvc;
@Autowired Greeter greeter;
@Test
void greets() throws Exception {
mvc.perform(get("/greet/{name}", "Ada"))
.andExpect(status().isOk())
.andExpect(content().string("Hello, Ada"));
}
}
The same source runs two ways. Under Maven Surefire it is an ordinary JUnit 5
test. With -Dcn1.backend.compiledTests=true the build translates it with the
application and runs it as a native binary, the way the server ships -- there
is no JUnit and no reflection in that binary, so the build finds the tests in
the bytecode and writes the calls itself.
The names follow Spring's so a test ports by changing its imports:
MockMvc, perform, MockMvcRequestBuilders, MockMvcResultMatchers,
TestConfiguration, MockitoBean, LocalServerPort and TestRestTemplate.
What is left out is what Spring keeps for compatibility with servlets.
-
ClassDescriptionRuns a test class against the application the build wired: what
@SpringBootTestdoes.A production featureBackendTest.features()can add to the test's application.How aBackendTestreaches the application.Expectations of the response body, fromMockMvcResultMatchers.content().Expectations of the cookies a response sets, fromMockMvcResultMatchers.cookie().Tests persistence without the rest of the application: what Spring's@DataJpaTestdoes.Expectations of the response headers, fromMockMvcResultMatchers.header().HttpEntity<T>A body and its headers, forTestRestTemplate.exchange(String, HttpMethod, HttpEntity, Class, Object...).Request or response headers: names in any case, each with its values in order.The methodsTestRestTemplate.exchange(String, HttpMethod, HttpEntity, Class, Object...)sends.The common response statuses, soassertEquals(HttpStatus.OK, response.getStatusCode())reads as it does in Spring.A response status: anHttpStatusfor the common ones, or any other code.Expectations of one JSONPath into the JSON body, fromMockMvcResultMatchers.jsonPath(String, Object...).Injects the port theBackendTest's server listens on into anintfield.The operational contract of a server with management on, as tests a test class inherits: implement it on aBackendTestwithBackendTest.Feature.MANAGEMENTand a port, and its default@Testmethods check the probes, the endpoint authentication, the metric schema and the Prometheus scrape against that application.Media types, forcontentType(...)andaccept(...).Replaces every bean of the field's type with a Mockito mock, and injects it:A file part forMockMvcRequestBuilders.multipart(String, Object...).Amultipart/form-datarequest, fromMockMvcRequestBuilders.multipart(String, Object...): files and fields, encoded as a browser's form post would be.Calls a running backend's routes in the same process: no socket, no port, the same answer.The requestsMockMvcsends, for static import:Handlers forandDo(...), for static import.The expectationsandExpect(...)takes, for static import:One request forMockMvc, built byMockMvcRequestBuilders.What aMockMvcrequest came back with.OneMockMvcexchange: what was sent and what came back.An OTLP/HTTP receiver for tests: what a test points the exporters of aBackendTest.Feature.TELEMETRYapplication at, to see that they send and that they flush when the application stops.Changes a requestMockMvcis about to send: whatMockRequestBuilder.with(RequestPostProcessor)takes.ATestRestTemplateresponse: status, headers and the body converted to the type asked for.What to do with aMockMvcresult: assert on it, act on it, or return it.Something done with aMockMvcresult, such asMockMvcResultHandlers.print().One expectation of aMockMvcresult, fromMockMvcResultMatchers.What a test says about one request's security, for static import:SecurityMockMvcRequestPostProcessors.csrf(), to refine.SecurityMockMvcRequestPostProcessors.jwt(), to refine.Expectations of the response status, fromMockMvcResultMatchers.status().A class whose@Beanmethods add beans for aBackendTest, and only for it.An HTTP client for aBackendTestserved on a port --webEnvironment = RANDOM_PORT-- through the backend's own outbound client, so a compiled test sends real requests from the native binary too.Who the calling thread's tests are running as.Tests controllers without the rest of the application: what Spring's@WebMvcTestdoes.Runs a test as nobody: for one test of a class that is otherwiseWithMockUser.Runs a test, or every test of a class, as a signed-in user who exists nowhere but in the test: no user store is asked and no password checked.