这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions spring-web-modules/spring-mvc-test/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
plugins {
id 'java'
id 'org.springframework.boot' version '3.3.2'
id 'io.spring.dependency-management' version '1.1.6'
}

group = 'com.testing'
version = '0.0.1-SNAPSHOT'

java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
}

repositories {
mavenCentral()
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}

tasks.named('test') {
useJUnitPlatform()
}
1 change: 1 addition & 0 deletions spring-web-modules/spring-mvc-test/settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rootProject.name = 'mvc'
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.testing.mvc;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MvcApplication {

public static void main(String[] args) {
SpringApplication.run(MvcApplication.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.testing.mvc;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import java.util.List;

@RestController
public class SortingController {

private final SortingService sortingService;

public SortingController(SortingService sortingService){
this.sortingService=sortingService;
}

@GetMapping
public ResponseEntity<String> helloWorld(){
return ResponseEntity.ok("Hello, World!");
}

@PostMapping
public ResponseEntity<List<Integer>> sort(@RequestBody List<Integer> arr){
return ResponseEntity.ok(sortingService.sortArray(arr));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.testing.mvc;

import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SortingService {

public List<Integer> sortArray(List<Integer> arr){
return arr.stream().sorted().toList();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
spring.application.name=mvc
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.testing.mvc.springboottest;

import com.fasterxml.jackson.databind.ObjectMapper;
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;

import java.util.Arrays;
import java.util.List;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@AutoConfigureMockMvc
@SpringBootTest
class SortingControllerIntegrationTest {

@Autowired
private MockMvc mockMvc;

@Autowired
private ObjectMapper objectMapper;

@Test
void whenHelloWorldMethodIsCalled_thenReturnSuccessString() throws Exception {
mockMvc.perform(get("/"))
.andExpect(status().isOk())
.andExpect(content().string("Hello, World!"));
}

@Test
void whenSortMethodIsCalled_thenReturnSortedArray() throws Exception {
List<Integer> input = Arrays.asList(5, 3, 8, 1, 9, 2);
List<Integer> sorted = Arrays.asList(1, 2, 3, 5, 8, 9);

mockMvc.perform(post("/").contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(input)))
.andExpect(status().isOk())
.andExpect(content().json(objectMapper.writeValueAsString(sorted)));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.testing.mvc.springboottest;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.http.MediaType;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;

import java.util.Arrays;
import java.util.List;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class SortingControllerWithWebEnvironmentIntegrationTest {

@LocalServerPort
private int port;

@Autowired
private TestRestTemplate restTemplate;

@Autowired
private ObjectMapper objectMapper;

@Test
void whenHelloWorldMethodIsCalled_thenReturnSuccessString() {
ResponseEntity<String> response = restTemplate.getForEntity("http://localhost:" + port + "/", String.class);
Assertions.assertEquals(HttpStatus.OK, response.getStatusCode());
Assertions.assertEquals("Hello, World!", response.getBody());
}

@Test
void whenSortMethodIsCalled_thenReturnSortedArray() throws Exception {
List<Integer> input = Arrays.asList(5, 3, 8, 1, 9, 2);
List<Integer> sorted = Arrays.asList(1, 2, 3, 5, 8, 9);

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);

ResponseEntity<List> response = restTemplate.postForEntity("http://localhost:" + port + "/",
new HttpEntity<>(objectMapper.writeValueAsString(input), headers), List.class);

Assertions.assertEquals(HttpStatus.OK, response.getStatusCode());
Assertions.assertEquals(sorted, response.getBody());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.testing.mvc.webmvctest;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.testing.mvc.SortingController;
import com.testing.mvc.SortingService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;

import java.util.Arrays;
import java.util.List;

import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@WebMvcTest(SortingController.class)
class SortingControllerUnitTest {

@Autowired
private MockMvc mockMvc;

@MockBean
private SortingService sortingService;

@Autowired
private ObjectMapper objectMapper;

@Test
void whenHelloWorldMethodIsCalled_thenReturnSuccessString() throws Exception {
mockMvc.perform(get("/"))
.andExpect(status().isOk());
}

@Test
void whenSortMethodIsCalled_thenReturnSortedArray() throws Exception {
List<Integer> input = Arrays.asList(5, 3, 8, 1, 9, 2);
List<Integer> sorted = Arrays.asList(1, 2, 3, 5, 8, 9);

when(sortingService.sortArray(input)).thenReturn(sorted);

mockMvc.perform(post("/").contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(input)))
.andExpect(status().isOk())
.andExpect(content().json(objectMapper.writeValueAsString(sorted)));
}

}