文章目录
1、springMVC测试
pom依赖
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>provided</scope>
</dependency>
<dependency>
<!-- springMVC项目,启动需要使用外部的tomcat,测试启动需要引入内置tomcat。因此,本依赖只能在测试类启动时,才能引入-->
<!-- 项目用到了websocket,Junit测试时,需要引入内置的tomcat-->
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-websocket</artifactId>
<version>8.5.15</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${org.springframework-version}</version>
<scope>test</scope>
</dependency>
代码
测试基类
import org.junit.runner.RunWith;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
/**
* @author zhangjie
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ComponentScan(basePackages = "com.arges")
@ContextConfiguration(locations = { "classpath:applicationContext.xml" })//, "classpath:redis-context.xml", "classpath:logback.xml"
@WebAppConfiguration("src/main/webapp")
public class BaseTest
{
}
具体的测试类需要继承基类,才能自动注入spring管理对象
public class VSRTest extends BaseTest
{
@Autowired
public VSRApiService vsrService;
String channelCode = "00000000001181000038$1$0$0";
String puid = "00000000001181000038";
Long businessId = 1L;
@Test
public void pushTransmission() throws Exception {
List<String> clientIdList = new ArrayList<>();
String subject = "";
String content = "";
boolean success = vsrService.pushTransmission(systemType, endpointType, clientIdList, subject, content);
System.out.println("create result: " + success);
}
}
2、springboot 测试
pom依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.1.4.RELEASE</version>
<scope>test</scope>
</dependency>
代码
测试基类
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {HystrixProviderApplication.class})
class BaseTestOfSpringBoot {
}
具体的测试类需要继承基类,才能自动注入spring管理对象
3、利用MockMvc测试
基类添加注解:@AutoConfigureMockMvc
获取MockMvc实例
@Autowired
private MockMvc mockMvc;
测试代码
@Test
public void testMockUsage() throws Exception {
// 初始化数据
ObjectMapper jsonMapper = new ObjectMapper();
Map<String, String> requestBody = new HashMap<>();
requestBody.put("mobile", "13100001111");
requestBody.put("password", "123456");
String requestBodyStr = jsonMapper.writeValueAsString(requestBody);
Map<String, String> responseBody = new HashMap<>();
responseBody.put("result_desc", "登录成功");
String responseBodyStr = jsonMapper.writeValueAsString(responseBody);
// 发送mock请求
String returnContent = mockMvc.perform(MockMvcRequestBuilders.post("/api/login")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.content(requestBodyStr))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().json(responseBodyStr))
.andReturn().getResponse().getContentAsString();
System.out.println(returnContent);
}