Package com.codename1.backend.test


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.