You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
3.7 KiB
Java

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