提交 680619ca 编写于 作者: lakernote's avatar lakernote

controller层单元测试

上级 ee53c296
package com.laker.admin.module.sys.controller;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest // 告诉 Spring Boot 寻找一个主配置类(例如,一个带有的@SpringBootApplication)并使用它来启动 Spring 应用程序上下文
public class TestingWebApplicationTests {
@Autowired
SysUserController sysUserController;
@Test
public void contextLoads() {
Assert.assertNotNull(sysUserController.getAll());
}
}
\ No newline at end of file
package com.laker.admin.module.sys.controller;
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.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
// 在这个测试中,启动了完整的 Spring 应用程序上下文,但没有服务器。
@SpringBootTest // 告诉 Spring Boot 寻找一个主配置类(例如,一个带有的@SpringBootApplication)并使用它来启动 Spring 应用程序上下文
@AutoConfigureMockMvc
public class TestingWebApplicationTests2 {
@Autowired
private MockMvc mockMvc;
@Test
public void shouldReturnDefaultMessage() throws Exception {
this.mockMvc.perform(get("/"))
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().string(""));
}
}
\ No newline at end of file
package com.laker.admin.module.sys.controller;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.server.LocalServerPort;
/**
* 启动一个真实的tomcat容器
* WebEnvironment.DEFINED_PORT:用程序定义的端口
* WebEnvironment.RANDOM_PORT:使用随机端口,配合@LocalServerPort注解使用
*/
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class TestingWebApplicationTests3 {
@Autowired
SysUserController sysUserController;
@LocalServerPort
int port;
@Test
public void contextLoads() {
Assert.assertNotNull(sysUserController);
System.out.println("单元测试tomcat端口为:" + port);
}
}
\ No newline at end of file
package com.laker.admin.module.sys.controller;
import org.junit.Assert;
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;
/**
* 启动一个真实的tomcat容器
* WebEnvironment.DEFINED_PORT:用程序定义的端口
* WebEnvironment.RANDOM_PORT:使用随机端口,配合@LocalServerPort注解使用
*/
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class TestingWebApplicationTests4 {
@Autowired
SysUserController sysUserController;
@Autowired
TestRestTemplate restTemplate;
@Test
public void contextLoads() {
Assert.assertNotNull(sysUserController);
Assert.assertTrue(this.restTemplate.getForObject("http://localhost:8080" + "/",
String.class) != null);
}
}
\ No newline at end of file
package com.laker.admin.module.sys.controller;
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 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;
/**
* 启动一个真实的tomcat容器
* WebEnvironment.DEFINED_PORT:用程序定义的端口
* WebEnvironment.RANDOM_PORT:使用随机端口,配合@LocalServerPort注解使用
*/
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@AutoConfigureMockMvc
public class TestingWebApplicationTests5 {
@Autowired
private MockMvc mvc;
@Test
public void contextLoads() throws Exception {
mvc.perform(get("/")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content()
.contentTypeCompatibleWith(MediaType.APPLICATION_JSON));
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册