Backend API. Server-side code: this runs in a Codename One backend, not in the app on the device.
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.
Types
annotation BackendTest | Runs a test class against the application the build wired: what @SpringBootTest does. |
class ContentResultMatchers | Expectations of the response body, from MockMvcResultMatchers.content. |
class CookieResultMatchers | Expectations of the cookies a response sets, from MockMvcResultMatchers.cookie. |
annotation DataJpaTest | Tests persistence without the rest of the application: what Spring’s @DataJpaTest does. |
class HeaderResultMatchers | Expectations of the response headers, from MockMvcResultMatchers.header. |
class HttpEntity | A body and its headers, for TestRestTemplate.exchange. |
class HttpHeaders | Request or response headers: names in any case, each with its values in order. |
enum HttpMethod | The methods TestRestTemplate.exchange sends. |
enum HttpStatus | The common response statuses, so assertEquals(HttpStatus.OK, response.getStatusCode()) reads as it does in Spring. |
interface HttpStatusCode | A response status: an HttpStatus for the common ones, or any other code. |
class JsonPathResultMatchers | Expectations of one JSONPath into the JSON body, from MockMvcResultMatchers.jsonPath. |
annotation LocalServerPort | Injects the port the BackendTest’s server listens on into an int field. |
interface ManagementContract | The operational contract of a server with management on, as tests a test class inherits: implement it on a BackendTest with BackendTest.Feature.MANAGEMENT and a port, and its default @Test methods check the probes, the endpoint authentication, the metric schema and the Prometheus scrape against that application. |
class MediaType | Media types, for contentType(...) and accept(...). |
class MockMultipartFile | A file part for MockMvcRequestBuilders.multipart. |
class MockMultipartRequestBuilder | A multipart/form-data request, from MockMvcRequestBuilders.multipart: files and fields, encoded as a browser’s form post would be. |
class MockMvc | Calls a running backend’s routes in the same process: no socket, no port, the same answer. |
class MockMvcRequestBuilders | The requests MockMvc sends, for static import: |
class MockMvcResultHandlers | Handlers for andDo(...), for static import. |
class MockMvcResultMatchers | The expectations andExpect(...) takes, for static import: |
class MockRequestBuilder | One request for MockMvc, built by MockMvcRequestBuilders. |
class MockResponse | What a MockMvc request came back with. |
annotation MockitoBean | Replaces every bean of the field’s type with a Mockito mock, and injects it: |
class MvcResult | One MockMvc exchange: what was sent and what came back. |
class OtlpTestCollector | An OTLP/HTTP receiver for tests: what a test points the exporters of a BackendTest.Feature.TELEMETRY application at, to see that they send and that they flush when the application stops. |
interface RequestPostProcessor | Changes a request MockMvc is about to send: what MockRequestBuilder.with takes. |
class ResponseEntity | A TestRestTemplate response: status, headers and the body converted to the type asked for. |
class ResultActions | What to do with a MockMvc result: assert on it, act on it, or return it. |
interface ResultHandler | Something done with a MockMvc result, such as MockMvcResultHandlers.print. |
interface ResultMatcher | One expectation of a MockMvc result, from MockMvcResultMatchers. |
class SecurityMockMvcRequestPostProcessors | What a test says about one request’s security, for static import: |
class StatusResultMatchers | Expectations of the response status, from MockMvcResultMatchers.status. |
annotation TestConfiguration | A class whose @Bean methods add beans for a BackendTest, and only for it. |
class TestRestTemplate | An HTTP client for a BackendTest served 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. |
class TestSecurityContextHolder | Who the calling thread’s tests are running as. |
annotation WebMvcTest | Tests controllers without the rest of the application: what Spring’s @WebMvcTest does. |
annotation WithAnonymousUser | Runs a test as nobody: for one test of a class that is otherwise WithMockUser. |
annotation WithMockUser | 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. |