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));
}
}