Skip to content

Commit de30259

Browse files
marco76marco76
authored andcommitted
some test more and a service
1 parent 3a41ceb commit de30259

File tree

8 files changed

+129
-3
lines changed

8 files changed

+129
-3
lines changed

README.md

Lines changed: 2 additions & 2 deletions
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

Lines 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+
}
Lines 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+
}
Lines 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+
}

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

Lines 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;
Lines 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+
}
Lines 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+
}

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

Lines 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)