forked from mongoding/spring-boot-side
-
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
Showing
11 changed files
with
370 additions
and
15 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
@@ -1 +1,3 @@ | ||
spring boot 学习 | ||
spring boot 学习 | ||
https://segmentfault.com/a/1190000010854811 | ||
https://www.cnblogs.com/goingforward/p/7486278.html |
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
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
134 changes: 134 additions & 0 deletions
134
springboot-test/src/main/java/org/spring/springboot/dto/ResponseDTO.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,134 @@ | ||
package org.spring.springboot.dto; | ||
|
||
|
||
import java.io.Serializable; | ||
|
||
public class ResponseDTO<T> implements Serializable { | ||
|
||
|
||
private static final long serialVersionUID = 1L; | ||
|
||
private boolean success; | ||
|
||
private int code; | ||
|
||
private String msg; | ||
|
||
private T data; | ||
|
||
public ResponseDTO() { | ||
} | ||
|
||
public ResponseDTO(int code, String msg, T data) { | ||
super(); | ||
this.code = code; | ||
this.msg = msg; | ||
this.data = data; | ||
} | ||
|
||
|
||
public ResponseDTO(boolean success, int code, String msg, T data) { | ||
super(); | ||
this.success = success; | ||
this.code = code; | ||
this.msg = msg; | ||
this.data = data; | ||
} | ||
|
||
|
||
/** | ||
* 成功结果 (code="1",msg="",data=data) | ||
* | ||
* @param data | ||
* @return | ||
*/ | ||
public static <R> ResponseDTO<R> success(R data, String msg) { | ||
ResponseDTO<R> result = new ResponseDTO<R>(); | ||
result.setSuccess(true); | ||
result.setCode(1); | ||
result.setMsg(msg); | ||
result.setData(data); | ||
return result; | ||
} | ||
|
||
public static <R> ResponseDTO<R> success(R data) { | ||
|
||
return success(data, null); | ||
} | ||
|
||
public static <R> ResponseDTO<R> success() { | ||
return success(null); | ||
} | ||
|
||
/** | ||
* 失败结果 | ||
* | ||
* @param code | ||
* @param msg | ||
* @param data | ||
* @return | ||
*/ | ||
public static <R> ResponseDTO<R> error(int code, String msg, R data) { | ||
ResponseDTO<R> result = new ResponseDTO<R>(); | ||
result.setSuccess(false); | ||
result.setCode(code); | ||
result.setMsg(msg); | ||
result.setData(data); | ||
return result; | ||
} | ||
|
||
public static <R> ResponseDTO<R> error() { | ||
|
||
return error(null); | ||
} | ||
|
||
public static <R> ResponseDTO<R> error(String msg) { | ||
|
||
return error(-1, msg); | ||
} | ||
|
||
public static <R> ResponseDTO<R> error(int code, String msg) { | ||
|
||
return error(code, msg, null); | ||
} | ||
|
||
|
||
public boolean isSuccess() { | ||
return success; | ||
} | ||
|
||
public void setSuccess(boolean success) { | ||
this.success = success; | ||
} | ||
|
||
public int getCode() { | ||
return code; | ||
} | ||
|
||
public void setCode(int code) { | ||
this.code = code; | ||
} | ||
|
||
public String getMsg() { | ||
return msg; | ||
} | ||
|
||
public void setMsg(String msg) { | ||
this.msg = msg; | ||
} | ||
|
||
public T getData() { | ||
return data; | ||
} | ||
|
||
public void setData(T data) { | ||
this.data = data; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "ResponseDTO [success=" + success + ", code=" + code + ", msg=" + msg + ", data=" + data + "]"; | ||
} | ||
|
||
|
||
} |
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
44 changes: 44 additions & 0 deletions
44
springboot-test/src/test/java/org/spring/springboot/BookControllerMockTest.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,44 @@ | ||
package org.spring.springboot; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.spring.springboot.mvc.controller.BookController; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; | ||
import org.springframework.test.web.servlet.result.MockMvcResultHandlers; | ||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers; | ||
import org.springframework.test.web.servlet.setup.MockMvcBuilders; | ||
|
||
import static org.hamcrest.CoreMatchers.equalTo; | ||
|
||
public class BookControllerMockTest { | ||
|
||
private MockMvc mvc; //初始化执行 | ||
|
||
@Before | ||
public void setUp() { | ||
mvc = MockMvcBuilders.standaloneSetup(new BookController()).build(); | ||
} | ||
|
||
//验证controller是否正常响应并打印返回结果 | ||
@Test | ||
public void getHello() throws Exception { | ||
mvc.perform(MockMvcRequestBuilders.get("/hello") | ||
.accept(MediaType.APPLICATION_JSON)) | ||
.andExpect(MockMvcResultMatchers.status() | ||
.isOk()) | ||
.andDo(MockMvcResultHandlers.print()) | ||
.andReturn(); | ||
} | ||
|
||
//验证controller是否正常响应并判断返回结果是否正确 | ||
@Test | ||
public void testHello() throws Exception { | ||
mvc.perform(MockMvcRequestBuilders.get("/hello") | ||
.accept(MediaType.APPLICATION_JSON)) | ||
.andExpect(MockMvcResultMatchers.status().isOk()) | ||
.andExpect(MockMvcResultMatchers.content().string(equalTo("Hello World"))); | ||
} | ||
|
||
} |
72 changes: 72 additions & 0 deletions
72
springboot-test/src/test/java/org/spring/springboot/BookControllerTest.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,72 @@ | ||
package org.spring.springboot; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.spring.springboot.dto.ResponseDTO; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.web.client.TestRestTemplate; | ||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.util.LinkedMultiValueMap; | ||
import org.springframework.util.MultiValueMap; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class BookControllerTest { | ||
|
||
@Autowired | ||
private TestRestTemplate testRestTemplate; | ||
|
||
@Test | ||
public void get() { | ||
Map<String, String> multiValueMap = new HashMap<>(); | ||
multiValueMap.put("username", "lake");//传值,但要在url上配置相应的参数 | ||
ResponseDTO result = testRestTemplate.getForObject("/test/get?username={username}", ResponseDTO.class, multiValueMap); | ||
Assert.assertEquals(result.getCode(), 0); | ||
} | ||
|
||
@Test | ||
public void post() { | ||
MultiValueMap multiValueMap = new LinkedMultiValueMap(); | ||
multiValueMap.add("username", "lake"); | ||
ResponseDTO result = testRestTemplate.postForObject("/test/post", multiValueMap, ResponseDTO.class); | ||
Assert.assertEquals(result.getCode(), 0); | ||
} | ||
|
||
|
||
@Test | ||
public void getHeader() { | ||
HttpHeaders headers = new HttpHeaders(); | ||
headers.set("token", "xxxxxx"); | ||
HttpEntity formEntity = new HttpEntity(headers); | ||
String[] urlVariables = new String[]{"admin"}; | ||
ResponseEntity<ResponseDTO> result = testRestTemplate.exchange("/test/getHeader?username={username}", HttpMethod.GET, formEntity, ResponseDTO.class, urlVariables); | ||
Assert.assertEquals(result.getBody().getCode(), 0); | ||
} | ||
|
||
@Test | ||
public void putHeader() { | ||
HttpHeaders headers = new HttpHeaders(); | ||
headers.set("token", "xxxxxx"); | ||
MultiValueMap multiValueMap = new LinkedMultiValueMap(); | ||
multiValueMap.add("username", "lake"); | ||
HttpEntity formEntity = new HttpEntity(multiValueMap, headers); | ||
ResponseEntity<ResponseDTO> result = testRestTemplate.exchange("/test/putHeader", HttpMethod.PUT, formEntity, ResponseDTO.class); | ||
Assert.assertEquals(result.getBody().getCode(), 0); | ||
} | ||
|
||
@Test | ||
public void delete() { | ||
HttpHeaders headers = new HttpHeaders(); | ||
headers.set("token", "xxxxx"); | ||
MultiValueMap multiValueMap = new LinkedMultiValueMap(); | ||
multiValueMap.add("username", "lake"); | ||
HttpEntity formEntity = new HttpEntity(multiValueMap, headers); | ||
String[] urlVariables = new String[]{"admin"}; | ||
ResponseEntity<ResponseDTO> result = testRestTemplate.exchange("/test/delete?username={username}", HttpMethod.DELETE, formEntity, ResponseDTO.class, urlVariables); | ||
Assert.assertEquals(result.getBody().getCode(), 0); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
springboot-test/src/test/java/org/spring/springboot/TestSpringBoot.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,14 @@ | ||
package org.spring.springboot; | ||
|
||
import org.junit.runner.RunWith; | ||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
@RunWith(SpringRunner.class) | ||
@SpringBootTest(classes = SpringbootApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | ||
@EnableAutoConfiguration | ||
public class TestSpringBoot { | ||
|
||
|
||
} |
Oops, something went wrong.