|
|
|
|
package base;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
|
|
import cn.jlw.web.WebApplication;
|
|
|
|
|
import com.ibeetl.admin.test.util.test.TestEnvUtil;
|
|
|
|
|
import com.ibeetl.jlw.dao.StudentDao;
|
|
|
|
|
import com.ibeetl.jlw.dao.TeacherDao;
|
|
|
|
|
import com.ibeetl.jlw.entity.Student;
|
|
|
|
|
import com.ibeetl.jlw.entity.Teacher;
|
|
|
|
|
import org.junit.Assert;
|
|
|
|
|
import org.junit.jupiter.api.MethodOrderer;
|
|
|
|
|
import org.junit.jupiter.api.TestMethodOrder;
|
|
|
|
|
import org.junit.runner.RunWith;
|
|
|
|
|
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.context.ActiveProfiles;
|
|
|
|
|
import org.springframework.test.context.junit4.SpringRunner;
|
|
|
|
|
import org.springframework.test.web.servlet.MockMvc;
|
|
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <p>
|
|
|
|
|
* 测试基类,个人技术有限,目前没办法完全拆分成单个模块
|
|
|
|
|
* </p>
|
|
|
|
|
*
|
|
|
|
|
* @author mlx
|
|
|
|
|
* @date 2022/9/24
|
|
|
|
|
* @modified
|
|
|
|
|
*/
|
|
|
|
|
@SpringBootTest(classes = {WebApplication.class}, webEnvironment = SpringBootTest.WebEnvironment.MOCK)
|
|
|
|
|
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
|
|
|
|
@RunWith(SpringRunner.class)
|
|
|
|
|
@AutoConfigureMockMvc
|
|
|
|
|
@ActiveProfiles("test")
|
|
|
|
|
//@Sql(scripts = "/sql/clean.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD) // 每个单元测试结束后,清理 DB
|
|
|
|
|
public class BaseTest {
|
|
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
|
StudentDao studentDao;
|
|
|
|
|
@Resource
|
|
|
|
|
TeacherDao teacherDao;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
protected MockMvc mvc;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 随机一个学生登录信息到配置文件中,用于session登录信息
|
|
|
|
|
* 也可以在application-test.properties 修改登录信息
|
|
|
|
|
*
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public void putStudentLoginInfoToEnv() throws Exception {
|
|
|
|
|
// 随机取一个学生的登录信息
|
|
|
|
|
List<Student> studentList = studentDao.execute(
|
|
|
|
|
"select org_id, user_id " +
|
|
|
|
|
"from student " +
|
|
|
|
|
"where student_status = 1 and org_id is not null and user_id is not null " +
|
|
|
|
|
"order by rand() limit 1");
|
|
|
|
|
|
|
|
|
|
Assert.assertTrue("学生表为空,程序中断!", ObjectUtil.isNotEmpty(studentList));
|
|
|
|
|
// 学生ID
|
|
|
|
|
Student student = CollectionUtils.firstElement(studentList);
|
|
|
|
|
// 换学生身份登录系统
|
|
|
|
|
TestEnvUtil.setProperty("user.id", student.getUserId().toString());
|
|
|
|
|
TestEnvUtil.setProperty("user.orgId", student.getOrgId().toString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 随机一个老师登录信息到配置文件中,用于session登录信息
|
|
|
|
|
* 也可以在application-test.properties 修改登录信息
|
|
|
|
|
*
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public void putTeacherLoginInfoToEnv() throws Exception {
|
|
|
|
|
// 随机取一个学生的登录信息
|
|
|
|
|
List<Teacher> teacherList = teacherDao.execute(
|
|
|
|
|
"select org_id, user_id " +
|
|
|
|
|
"from teacher t \n" +
|
|
|
|
|
"where t.teacher_status = 1\t\n" +
|
|
|
|
|
"order by RAND() \n" +
|
|
|
|
|
"limit 1 ");
|
|
|
|
|
|
|
|
|
|
Assert.assertTrue("教师表表为空,程序中断!", ObjectUtil.isNotEmpty(teacherList));
|
|
|
|
|
// 学生ID
|
|
|
|
|
Teacher teacher = CollectionUtils.firstElement(teacherList);
|
|
|
|
|
// 换学生身份登录系统
|
|
|
|
|
TestEnvUtil.setProperty("user.id", teacher.getUserId().toString());
|
|
|
|
|
TestEnvUtil.setProperty("user.orgId", teacher.getOrgId().toString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 清空临时存放的环境变量,清除session登录信息
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public void clearEnvLoginInfo() throws Exception {
|
|
|
|
|
TestEnvUtil.removeProperty("user.id", "user.orgId");
|
|
|
|
|
}
|
|
|
|
|
}
|