GeezerVison is showing github.com — tests added with comments · lechathei/java-angular-basic@4058783
File tree
Expand file treeCollapse file tree
Open diff view settings
dev/marco/example/springboot
test/java/dev/marco/example/springboot
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: backend/pom.xml +6Lines changed: 6 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
24 | 24 | <artifactId>frontend</artifactId> |
25 | 25 | <version>${project.version}</version> |
26 | 26 | <type>jar</type> |
| 27 | + <!-- we add optional to work with the backend package without building the frontend --> |
| 28 | + <optional>true</optional> |
27 | 29 | </dependency> |
| 30 | + <dependency> |
| 31 | + <groupId>org.springframework.boot</groupId> |
| 32 | + <artifactId>spring-boot-starter-test</artifactId> |
| 33 | + </dependency> |
28 | 34 | </dependencies> |
29 | 35 |
|
30 | 36 | <build> |
|
Collapse file
Copy file name to clipboard +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +# url of the development frontend |
| 2 | +app.dev.frontend.local=http://localhost:4200 |
Collapse file
Copy file name to clipboardExpand all lines: backend/src/main/java/dev/marco/example/springboot/HelloController.java +4-2Lines changed: 4 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
9 | 9 | import java.util.Map; |
10 | 10 |
|
11 | 11 | @RestController |
12 | | -// we allow localhost:4200 for testing purposes |
13 | | -@CrossOrigin(origins = "http://localhost:4200") |
| 12 | +// we allow cors requests from our frontend environment |
| 13 | +// note the curly braces that creates an array of strings ... required by the annotation |
| 14 | +@CrossOrigin(origins = {"${app.dev.frontend.local"}) |
14 | 15 | public class HelloController { |
15 | 16 |
|
| 17 | + // simple GET response for our example purpose, we return a JSON structure |
16 | 18 | @RequestMapping(value = "/message", produces = MediaType.APPLICATION_JSON_VALUE) |
17 | 19 | public Map<String, String> index() { |
18 | 20 | return Collections.singletonMap("message", "Greetings from Spring Boot!"); |
|
Collapse file
Copy file name to clipboard +40Lines changed: 40 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +package dev.marco.example.springboot; |
| 2 | + |
| 3 | +import org.assertj.core.api.Assertions; |
| 4 | +import org.junit.jupiter.api.Test; |
| 5 | +import org.springframework.beans.factory.annotation.Autowired; |
| 6 | +import org.springframework.boot.test.context.SpringBootTest; |
| 7 | +import org.springframework.boot.test.web.client.TestRestTemplate; |
| 8 | +import org.springframework.boot.web.server.LocalServerPort; |
| 9 | + |
| 10 | +/** |
| 11 | + * The goal of this class is to show how the Embedded Server is used to test the REST service |
| 12 | + */ |
| 13 | + |
| 14 | +// SpringBootTest launch an instance of our application for tests purposes |
| 15 | +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) |
| 16 | +class HelloControllerEmbeddedServerTest { |
| 17 | + |
| 18 | + @Autowired |
| 19 | + private HelloController helloController; |
| 20 | + |
| 21 | + // inject the runtime port, it requires the webEnvironment |
| 22 | + @LocalServerPort |
| 23 | + private int port; |
| 24 | + |
| 25 | + // we use TestRestTemplate, it's an alternative to RestTemplate specific for tests |
| 26 | + // to use this template a webEnvironment is mandatory |
| 27 | + @Autowired |
| 28 | + private TestRestTemplate restTemplate; |
| 29 | + |
| 30 | + @Test |
| 31 | + void index() { |
| 32 | + // we test that our controller is not null |
| 33 | + Assertions.assertThat(helloController).isNotNull(); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + void indexResultTest() { |
| 38 | + Assertions.assertThat(restTemplate.getForObject("http://localhost:" + port + "/message", String.class)).contains("from Spring Boot"); |
| 39 | + } |
| 40 | +} |
Collapse file
Copy file name to clipboard +32Lines changed: 32 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +package dev.marco.example.springboot; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | +import org.springframework.beans.factory.annotation.Autowired; |
| 5 | +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; |
| 6 | +import org.springframework.boot.test.context.SpringBootTest; |
| 7 | +import org.springframework.test.web.servlet.MockMvc; |
| 8 | + |
| 9 | +import static org.hamcrest.Matchers.containsString; |
| 10 | +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; |
| 11 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 12 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; |
| 13 | + |
| 14 | +/** |
| 15 | + * The goal of this class is to test the controller using a MockMvc object without embedded server |
| 16 | + */ |
| 17 | +@SpringBootTest |
| 18 | +@AutoConfigureMockMvc // we mock the http request and we don't need a server |
| 19 | +public class HelloControllerMockMvcTest { |
| 20 | + |
| 21 | + @Autowired |
| 22 | + private MockMvc mockMvc; // injected with @AutoConfigureMockMvc |
| 23 | + |
| 24 | + @Test |
| 25 | + public void shouldReturnOurText() throws Exception { |
| 26 | + this.mockMvc |
| 27 | + .perform(get("/message")) // perform a request that can be chained |
| 28 | + .andDo(print()) // we log the result |
| 29 | + .andExpect(content().string(containsString(" from Spring"))); // we check that the Body of the answer contains our expectation |
| 30 | + } |
| 31 | + |
| 32 | +} |
Collapse file
Copy file name to clipboardExpand all lines: pom.xml +5Lines changed: 5 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
29 | 29 | <artifactId>spring-boot-starter-web</artifactId> |
30 | 30 | <version>2.4.3</version> |
31 | 31 | </dependency> |
| 32 | + <dependency> |
| 33 | + <groupId>org.springframework.boot</groupId> |
| 34 | + <artifactId>spring-boot-starter-test</artifactId> |
| 35 | + <version>2.4.3</version> |
| 36 | + </dependency> |
32 | 37 | </dependencies> |
33 | 38 | </project> |