spring项目测试

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);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值