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 BackendTestRuns a test class against the application the build wired: what @SpringBootTest does.
class ContentResultMatchersExpectations of the response body, from MockMvcResultMatchers.content.
class CookieResultMatchersExpectations of the cookies a response sets, from MockMvcResultMatchers.cookie.
annotation DataJpaTestTests persistence without the rest of the application: what Spring’s @DataJpaTest does.
class HeaderResultMatchersExpectations of the response headers, from MockMvcResultMatchers.header.
class HttpEntityA body and its headers, for TestRestTemplate.exchange.
class HttpHeadersRequest or response headers: names in any case, each with its values in order.
enum HttpMethodThe methods TestRestTemplate.exchange sends.
enum HttpStatusThe common response statuses, so assertEquals(HttpStatus.OK, response.getStatusCode()) reads as it does in Spring.
interface HttpStatusCodeA response status: an HttpStatus for the common ones, or any other code.
class JsonPathResultMatchersExpectations of one JSONPath into the JSON body, from MockMvcResultMatchers.jsonPath.
annotation LocalServerPortInjects the port the BackendTest’s server listens on into an int field.
interface ManagementContractThe 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 MediaTypeMedia types, for contentType(...) and accept(...).
class MockMultipartFileA file part for MockMvcRequestBuilders.multipart.
class MockMultipartRequestBuilderA multipart/form-data request, from MockMvcRequestBuilders.multipart: files and fields, encoded as a browser’s form post would be.
class MockMvcCalls a running backend’s routes in the same process: no socket, no port, the same answer.
class MockMvcRequestBuildersThe requests MockMvc sends, for static import:
class MockMvcResultHandlersHandlers for andDo(...), for static import.
class MockMvcResultMatchersThe expectations andExpect(...) takes, for static import:
class MockRequestBuilderOne request for MockMvc, built by MockMvcRequestBuilders.
class MockResponseWhat a MockMvc request came back with.
annotation MockitoBeanReplaces every bean of the field’s type with a Mockito mock, and injects it:
class MvcResultOne MockMvc exchange: what was sent and what came back.
class OtlpTestCollectorAn 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 RequestPostProcessorChanges a request MockMvc is about to send: what MockRequestBuilder.with takes.
class ResponseEntityA TestRestTemplate response: status, headers and the body converted to the type asked for.
class ResultActionsWhat to do with a MockMvc result: assert on it, act on it, or return it.
interface ResultHandlerSomething done with a MockMvc result, such as MockMvcResultHandlers.print.
interface ResultMatcherOne expectation of a MockMvc result, from MockMvcResultMatchers.
class SecurityMockMvcRequestPostProcessorsWhat a test says about one request’s security, for static import:
class StatusResultMatchersExpectations of the response status, from MockMvcResultMatchers.status.
annotation TestConfigurationA class whose @Bean methods add beans for a BackendTest, and only for it.
class TestRestTemplateAn 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 TestSecurityContextHolderWho the calling thread’s tests are running as.
annotation WebMvcTestTests controllers without the rest of the application: what Spring’s @WebMvcTest does.
annotation WithAnonymousUserRuns a test as nobody: for one test of a class that is otherwise WithMockUser.
annotation WithMockUserRuns 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.