java接口自动化-post请求获取不到cookie问题解决

本文详细描述了在Java接口自动化测试中遇到的一个问题:POST请求后无法获取Cookie。通过分析测试代码,发现在LoginTest类中虽然响应包含Cookie,但未正确存储到Cookiestore。经过检查,发现Cookiestore对象未实例化,修复此问题后,测试正常执行,数据库成功添加数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.问题描述

在做自动化测试时,有两个测试方法,loginTrue方法和addUser方法,原则上只有loginTrue方法执行成功获取到cookie,并存储到TestConfig的静态对象cookiestore里面;下次从TestConfig拿到这个cookie请求addUser。
代码如下:
LoginTest类:

package com.course.cases;

import com.course.config.TestConfig;
import com.course.model.InterfaceName;
import com.course.model.LoginCase;
import com.course.utils.ConfigFile;
import com.course.utils.MybatisUtils;
import lombok.extern.log4j.Log4j2;
import org.apache.http.client.CookieStore;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.ibatis.session.SqlSession;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import java.io.IOException;
import java.net.URISyntaxException;

@Log4j2
public class LoginTest {
    private CookieStore cookieStore;
    @BeforeTest(groups = "loginTrue",description = "测试准备工作")
    public void beforeTest(){
        TestConfig.addUserUrl = ConfigFile.getUrl(InterfaceName.ADD_USER);
        TestConfig.getUserInfoUrl = ConfigFile.getUrl(InterfaceName.GET_USER_INFO);
        TestConfig.getUserListUrl = ConfigFile.getUrl(InterfaceName.GET_USER_LIST);
        TestConfig.loginUrl = ConfigFile.getUrl(InterfaceName.LOGIN);
        TestConfig.updateUserInfoUrl = ConfigFile.getUrl(InterfaceName.UPDATE_USER_INFO);

        TestConfig.client = HttpClients.custom().setDefaultCookieStore(TestConfig.cookieStore).build();

    }
    @Test(groups = "loginTrue",description = "用户登录成功接口测试")
    public void loginTrue() throws URISyntaxException {
        SqlSession sqlSession = MybatisUtils.openSession();
        LoginCase loginCase = sqlSession.selectOne("loginCase",1);
        System.out.println(loginCase);
        System.out.println(TestConfig.loginUrl);

        log.info("********开始验证loginTrue api*********");
        //发请求,获取结果
        String result = getResult(loginCase,TestConfig.loginUrl);
        log.info("请求http:localhost/v1/login的结果是------>" + result);
        //验证返回结果
        Assert.assertEquals(loginCase.getExpected(),result);
        log.info("********loginTrue api 验证通过*********");


    }
    //getResult这个是具体发送请求的方法
    private String getResult(LoginCase loginCase, String loginUrl) throws URISyntaxException {
        //1.创建URLBuilder对象方法
        URIBuilder uriBuilder = new URIBuilder(loginUrl);
        uriBuilder.setParameter("userName",loginCase.getUserName()).setParameter("password",loginCase.getPassword());

        //2.创建post方法
        HttpPost post = new HttpPost(uriBuilder.build());

        //3.获得client对象,并设置cookiestore
        TestConfig.client = HttpClients.custom().setDefaultCookieStore(cookieStore).build();

        //4.设置请求头
        post.setHeader("content-type","application/json");
        //5.设置cookie,已在3设置
        //6.请求api,获得响应结果
        CloseableHttpResponse response = null;
        String res = null;

        try {
            response = TestConfig.client.execute(post);
            res = EntityUtils.toString(response.getEntity(),"utf-8
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值