其他分享
首页 > 其他分享> > SprintBoot Junit 5 测试Demo

SprintBoot Junit 5 测试Demo

作者:互联网

/**
 * 使用junit 5 测试项目的接口可用性
 *
 * @author jianghao
 */
@DisplayName("接口联调验收单")
@SpringBootTest
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class AcceptanceTest {

    /**
     * 在每一个 @Test 之前执行一次,使用 ServletRequestAttributes 将头信息参数代入 
     */
    @BeforeEach
    public void addHeader(){
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        
        assert servletRequestAttributes != null;
        MockHttpServletRequest request = (MockHttpServletRequest) servletRequestAttributes.getRequest();
        Object token = API_MAP.get("token");
        if(token != null){
            request.addHeader("Authorization", token.toString());
        }
    }

    @Test
    @Order(0)
    @DisplayName("登录")
    public void login(){
        // 保存验证码信息
        String uuid = IdUtils.simpleUUID();
        String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
        String code = "test";
        redisCache.setCacheObject(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);

        // 登录
        String token = loginService.login("admin", "admin123", code,uuid);
        Assertions.assertNotNull(token);

        // 保存接口信息
        API_MAP.put("token",token);
    }

    @Test
    @Order(1)
    @DisplayName("9001签到")
    public void api9001(){
        MedicineResponse medicineResponse = medicineUserService.sign9001();
        Assertions.assertEquals(SUCCESS_CODE,medicineResponse.getInfcode());
    }


    @DisplayName("9002退签")
    public void api9002(){
        medicineUserService.unsign9002();
    }

    /**
     * 医保接口请求成功状态码
     */
    private static final String SUCCESS_CODE = "0";

    @Autowired
    private MedicineUserService medicineUserService;
    @Autowired
    private SysLoginService loginService;
    @Autowired
    private RedisCache redisCache;
    private static final HashMap<String,Object> API_MAP = new HashMap<>();


}

标签:DisplayName,String,SprintBoot,Demo,void,private,Junit,token,public
来源: https://blog.csdn.net/Aaren_Jiang/article/details/117459092