YW. Junit测试案例-签名公章

gradle + SpringBoot + JPA + Junit4


注入Service,主测代码逻辑(登录信息一般为后台录入,Junit拿不到)

package com.server.service;

import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;

import com.lib.db.sso.entity.SysUserSign;
import com.lib.vo.SysUserSignVO;
import com.server.ServerApplication;
import com.server.repository.SysUserSignRepository;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = ServerApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("dev")        //配置启动文件
public class UserSignServiceTest {
	
	@Autowired
	private UserSignService userSignService;
	
	@Autowired
	private SysUserSignRepository sysUserSignRepository;
	
	@Test
	public void testsave() {                                                                                                                                                                                                                                                                                      
//		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();            
//        SysUserInfo userInfo = (SysUserInfo) authentication.getPrincipal();
//        String id = userInfo.getId();
		SysUserSignVO sysUserSignVO = new SysUserSignVO();
		String userId = "13018097503";
		String signUrl = "/oss/cctp/static/image/23e4a0820ae109f921a8ae4ae1c664a4.jpg";
		int signType = 0;
		sysUserSignVO.setUserId(userId);
		sysUserSignVO.setSignUrl(signUrl);
		sysUserSignVO.setSignType(signType);
		SysUserSign sysUserSign = SysUserSignVO.loadFrom(sysUserSignVO,SysUserSign.class);
		System.out.println("================");
		System.out.println("================");
		sysUserSignRepository.save(sysUserSign);
		System.out.println("================");
		System.out.println("================");
		System.out.println("================");
	}

	@Test
	public void testupdate() {                                                                                                                               
		SysUserSignVO sysUserSignVO = new SysUserSignVO();
		String userId = "13018097503";
		String signUrl = "/oss/cctp/static/image/fb5fcce57806dba4856aa01dc073c5d0.jpg";
		int signType = 0;
		sysUserSignVO.setUserId(userId);
		sysUserSignVO.setSignUrl(signUrl);
		sysUserSignVO.setSignType(signType);
		sysUserSignRepository.signdel(userId);
		System.out.println("================");
		System.out.println("================");
		SysUserSign sysUserSign = SysUserSignVO.loadFrom(sysUserSignVO,SysUserSign.class);
		sysUserSignRepository.save(sysUserSign);
		System.out.println("================");
		System.out.println("================");
		System.out.println("================");
	}
}

写入接口调用路径及入参,测Controller接口

package com.server.controller;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.WebApplicationContext;

@SpringBootTest
@RunWith(SpringRunner.class)
@ActiveProfiles("dev")
public class UserControllerTest {

	@Autowired
    private WebApplicationContext webApplicationContext;
    private MockMvc mockMvc;
    
    @Before
    public void setUp() throws Exception{
        mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
    }
    
	@Test
	public void testunboundMiniProgramOwn() throws Exception {
		String programType = "1";
		ResultActions perform = mockMvc.perform(MockMvcRequestBuilders.post("/api/member/user/unbound/miniProgram/own")
				.param("programType", programType));
		perform.andExpect(MockMvcResultMatchers.status().is(200));
		
	}
	
	@Test
	public void retrievePassword() throws Exception {
		String username = "13018097503";
		String password00 = "XXXXxxxx00";
		String password01 = "XXXXxxxx00";
		String varkey = "744661";
		ResultActions perform = mockMvc.perform(MockMvcRequestBuilders.get("/api/retrievePassword")
				.param("username",username,"password00",password00,
			    		"password01",password01,"varkey",varkey));
		System.out.println("================");
		System.out.println("================");
		System.out.println("================");
		perform.andExpect(MockMvcResultMatchers.status().is(200));
		
	}
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值