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.How aBackendTestreaches the application.Expectations of the response body, fromMockMvcResultMatchers.content().Expectations of the cookies a response sets, fromMockMvcResultMatchers.cookie().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.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.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.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.