GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — tests added with comments · AsmaStack/java-angular-basic@4058783

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • backend
    • pom.xml
    • src
      • main/java
        • application.properties
        • dev/marco/example/springboot
          • HelloController.java
      • test/java/dev/marco/example/springboot
        • HelloControllerEmbeddedServerTest.java
        • HelloControllerMockMvcTest.java
  • pom.xml
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎backend/pom.xml‎

Copy file name to clipboardExpand all lines: backend/pom.xml
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,13 @@
2424
<artifactId>frontend</artifactId>
2525
<version>${project.version}</version>
2626
<type>jar</type>
27+
<!-- we add optional to work with the backend package without building the frontend -->
28+
<optional>true</optional>
2729
</dependency>
30+
<dependency>
31+
<groupId>org.springframework.boot</groupId>
32+
<artifactId>spring-boot-starter-test</artifactId>
33+
</dependency>
2834
</dependencies>
2935

3036
<build>
Collapse file

‎backend/src/main/java/application.properties‎

Copy file name to clipboard
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# url of the development frontend
2+
app.dev.frontend.local=http://localhost:4200
Collapse file

‎backend/src/main/java/dev/marco/example/springboot/HelloController.java‎

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 numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
import java.util.Map;
1010

1111
@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"})
1415
public class HelloController {
1516

17+
// simple GET response for our example purpose, we return a JSON structure
1618
@RequestMapping(value = "/message", produces = MediaType.APPLICATION_JSON_VALUE)
1719
public Map<String, String> index() {
1820
return Collections.singletonMap("message", "Greetings from Spring Boot!");
Collapse file

‎backend/src/test/java/dev/marco/example/springboot/HelloControllerEmbeddedServerTest.java‎

Copy file name to clipboard
+40Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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

‎backend/src/test/java/dev/marco/example/springboot/HelloControllerMockMvcTest.java‎

Copy file name to clipboard
+32Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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

‎pom.xml‎

Copy file name to clipboardExpand all lines: pom.xml
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,10 @@
2929
<artifactId>spring-boot-starter-web</artifactId>
3030
<version>2.4.3</version>
3131
</dependency>
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-test</artifactId>
35+
<version>2.4.3</version>
36+
</dependency>
3237
</dependencies>
3338
</project>

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.