-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
633c493
commit ad6cba6
Showing
3 changed files
with
65 additions
and
1 deletion.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
src/main/java/com/github/piotryuxuan/springbootplayground/api/ApiController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.github.piotryuxuan.springbootplayground.api; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("/api/v1") | ||
public class ApiController { | ||
|
||
@GetMapping("answer") | ||
public ResponseEntity<int[]> getAnswer() { | ||
int[] array = { 42 }; | ||
return new ResponseEntity<>(array, HttpStatus.OK); | ||
} | ||
|
||
@GetMapping("addition") | ||
public ResponseEntity<Long> getAddition(@RequestParam Long a, @RequestParam Long b) { | ||
return new ResponseEntity<>(a + b, HttpStatus.OK); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/test/java/com/github/piotryuxuan/springbootplayground/api/ApiControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.github.piotryuxuan.springbootplayground.api; | ||
|
||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
|
||
@SpringBootTest | ||
@AutoConfigureMockMvc | ||
class ApiControllerTest { | ||
|
||
@Autowired | ||
private MockMvc mockMvc; | ||
|
||
@Test | ||
public void testGetAnswer() throws Exception { | ||
mockMvc.perform(get("/api/v1/answer") | ||
.contentType(MediaType.APPLICATION_JSON)) | ||
.andExpect(status().isOk()) | ||
.andExpect(content().json("[42]")); | ||
} | ||
|
||
@Test | ||
public void testGetAddition() throws Exception { | ||
mockMvc.perform(get("/api/v1/addition") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.param("a", "3") | ||
.param("b", "3")) | ||
.andExpect(status().isOk()) | ||
.andExpect(content().json("6")); | ||
} | ||
|
||
} |