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 — some test more and a service · hafiddev0/java-angular-basic@de30259

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • README.md
  • backend/src
    • main/java/dev/marco/example/springboot
      • feature
        • FeatureController.java
        • FeatureService.java
        • OperationValues.java
      • hello
        • HelloController.java
    • test/java/dev/marco/example/springboot
      • FeatureControllerEmbeddedServerTest.java
      • FeatureControllerMockMvcTest.java
      • HelloControllerEmbeddedServerTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎README.md‎

Copy file name to clipboardExpand all lines: README.md
+2-2Lines changed: 2 additions & 2 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ https://marco.dev/deploy-java-angular-one
88
And a demo of the installation in this video:
99
https://youtu.be/xCNCCZLvd0Y
1010

11-
To the basic example I added some 'showcase' extra features:
11+
To the basic example I added some 'showcases' extra features:
1212
- Test with MockMvc
1313
- Test with RestTemplateTest
14-
- OpenApi using Spring Doc (code first). Swagger-ui is accessible here: http://localhost:8080/swagger-ui
14+
- OpenApi using Spring Doc (code first). Swagger-ui is accessible here: http://localhost:8080/swagger-ui
1515

Collapse file

‎backend/src/main/java/dev/marco/example/springboot/feature/FeatureController.java‎

Copy file name to clipboard
+36Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package dev.marco.example.springboot.feature;
2+
3+
import org.json.JSONException;
4+
import org.json.JSONObject;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.http.MediaType;
7+
import org.springframework.http.ResponseEntity;
8+
import org.springframework.web.bind.annotation.*;
9+
10+
/**
11+
* The goal of this controller is to show the test of the services and some REST methods
12+
*/
13+
@RestController
14+
@CrossOrigin(origins = {"${app.dev.frontend.local"})
15+
public class FeatureController {
16+
17+
@Autowired
18+
FeatureService featureService;
19+
20+
@GetMapping(value = "/feature", consumes = MediaType.APPLICATION_JSON_VALUE)
21+
public ResponseEntity<String> getSum(@RequestBody OperationValues operationValues) throws JSONException {
22+
Integer sumResult = featureService.getSum(operationValues.getValueA(), operationValues.getValueB());
23+
JSONObject result = new JSONObject();
24+
result.put("result", sumResult);
25+
return ResponseEntity.ok(result.toString());
26+
}
27+
28+
@PostMapping(value = "/feature", consumes = MediaType.APPLICATION_JSON_VALUE)
29+
public ResponseEntity<String> postSum(@RequestBody OperationValues operationValues) throws JSONException {
30+
Integer sumResult = featureService.getSum(operationValues.getValueA(), operationValues.getValueB());
31+
JSONObject result = new JSONObject();
32+
result.put("result", sumResult);
33+
return ResponseEntity.ok(result.toString());
34+
}
35+
36+
}
Collapse file

‎backend/src/main/java/dev/marco/example/springboot/feature/FeatureService.java‎

Copy file name to clipboard
+14Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package dev.marco.example.springboot.feature;
2+
3+
import org.springframework.stereotype.Service;
4+
5+
/**
6+
* Simple service for example purposes
7+
*/
8+
@Service
9+
public class FeatureService {
10+
11+
public Integer getSum(Integer valueA, Integer valueB) {
12+
return valueA + valueB;
13+
}
14+
}
Collapse file

‎backend/src/main/java/dev/marco/example/springboot/feature/OperationValues.java‎

Copy file name to clipboard
+29Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package dev.marco.example.springboot.feature;
2+
3+
public class OperationValues {
4+
private Integer valueA;
5+
private Integer valueB;
6+
7+
public OperationValues() {
8+
}
9+
10+
public OperationValues(Integer valueA, Integer valueB) {
11+
this.valueA = valueA;
12+
this.valueB = valueB;
13+
}
14+
public Integer getValueA() {
15+
return valueA;
16+
}
17+
18+
public Integer getValueB() {
19+
return valueB;
20+
}
21+
22+
public void setValueA(Integer valueA) {
23+
this.valueA = valueA;
24+
}
25+
26+
public void setValueB(Integer valueB) {
27+
this.valueB = valueB;
28+
}
29+
}
Collapse file

‎…/example/springboot/HelloController.java‎ ‎…le/springboot/hello/HelloController.java‎backend/src/main/java/dev/marco/example/springboot/HelloController.java renamed to backend/src/main/java/dev/marco/example/springboot/hello/HelloController.java backend/src/main/java/dev/marco/example/springboot/HelloController.java renamed to backend/src/main/java/dev/marco/example/springboot/hello/HelloController.java

Copy file name to clipboardExpand all lines: backend/src/main/java/dev/marco/example/springboot/hello/HelloController.java
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package dev.marco.example.springboot;
1+
package dev.marco.example.springboot.hello;
22

33
import org.springframework.http.MediaType;
44
import org.springframework.web.bind.annotation.CrossOrigin;
Collapse file

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

Copy file name to clipboard
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package dev.marco.example.springboot;
2+
3+
/**
4+
* * We didn't implement the version with the RestTemplate because spring doesn't like GET requests with body.
5+
* * You can find more details about this issue in this post: https://marco.dev/spring-boot-test-get-body
6+
*/
7+
public class FeatureControllerEmbeddedServerTest {
8+
}
Collapse file

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

Copy file name to clipboard
+38Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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.http.MediaType;
8+
import org.springframework.test.web.servlet.MockMvc;
9+
10+
import static org.hamcrest.Matchers.containsString;
11+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
12+
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
13+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
14+
15+
/**
16+
* The goal of this class is to test the controller using a MockMvc object without embedded server
17+
*
18+
* We didn't implement the version with the RestTemplate because spring doesn't like GET requests with body.
19+
* You can find more details about this issue in this post: https://marco.dev/spring-boot-test-get-body
20+
*/
21+
@SpringBootTest
22+
@AutoConfigureMockMvc // we mock the http request and we don't need a server
23+
public class FeatureControllerMockMvcTest {
24+
25+
@Autowired
26+
private MockMvc mockMvc; // injected with @AutoConfigureMockMvc
27+
28+
@Test
29+
public void shouldReturnOurText() throws Exception {
30+
this.mockMvc
31+
.perform(get("/feature")
32+
.contentType(MediaType.APPLICATION_JSON)
33+
.content("{\"valueA\": 5, \"valueB\": 7}"))
34+
.andDo(print()) // we log the result
35+
.andExpect(content().string(("{\"result\":12}")));
36+
}
37+
38+
}
Collapse file

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

Copy file name to clipboardExpand all lines: backend/src/test/java/dev/marco/example/springboot/HelloControllerEmbeddedServerTest.java
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package dev.marco.example.springboot;
22

3+
import dev.marco.example.springboot.hello.HelloController;
34
import org.assertj.core.api.Assertions;
45
import org.junit.jupiter.api.Test;
56
import org.springframework.beans.factory.annotation.Autowired;

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.