如何模拟Unirest链接方法?

时间:2019-01-31 12:00:28

标签: java junit powermock powermockito unirest

我有一个像这样的类,它使用Unirest API进行HTTP POST调用-

public class MyClass{
    public MyObject getData(){
    HttpResponse response = Unirest.post(someURL).header("Content-Type", "application/x-www-form-urlencoded")
                        .field("Field1", context.getParameter("value for field 1"))
                        .field("username", context.getParameter("username"))
                        .field("password", context.getParameter("password"))
                        .field("field 3", context.getParameter("field 3 value"))
                        .field("field 4", context.getParameter("field 4 value"))
                        .asJson();
    }
    }

在我的模拟课程中- 我的模型中有一个HttpResponse对象,当调用Unirest HTTP发布时将返回该对象。 我不想通过Unirest到达终点,并想返回我的自定义httpResponse。

HttpResonse<JsonNode> httpResponse; // my mocked response
Mockito.when(Unirest.post(someURL).header("Content-Type", "application/x-www-form-urlencoded")
                            .field("Field1", context.getParameter("value for field 1"))
                            .field("username", context.getParameter("username"))
                            .field("password", context.getParameter("password"))
                            .field("field 3", context.getParameter("field 3 value"))
                            .field("field 4", context.getParameter("field 4 value"))
                            .asJson()).thenReturn(httpResponse); // this is my custom response

我已经嘲笑了所有参数的上下文,但是仍然出现错误

  

org.mockito.exceptions.misusing.WrongTypeOfReturnValue:

     

getParameter()不能返回HttpResponse

     

getParameter()应该返回字符串

是否有一种方法可以更轻松地模拟第三方链接方法或更好的实现示例? 谢谢!

2 个答案:

答案 0 :(得分:1)

如果允许您重构代码,最简单的方法是将静态调用提取到受保护的方法:

public class MyClass{
    public MyObject getData(){
      HttpResponse response = postRequest();
    }

    protected HttpResponse postRequest(){
       return Unirest.post(someURL).header("Content-Type", "application/x-www-form-urlencoded")
                        .field("Field1", context.getParameter("value for field 1"))
                        .field("username", context.getParameter("username"))
                        .field("password", context.getParameter("password"))
                        .field("field 3", context.getParameter("field 3 value"))
                        .field("field 4", context.getParameter("field 4 value"))
                        .asJson();
    }
}

然后在您的测试用例中,监视被测类并模拟受保护的方法:

public void test(){
   MyClass myClass = Mockito.spy(new MyClass());

   HttpResonse<JsonNode> httpResponse; // my mocked response

   doReturn(httpResponse).when(myClass).postRequest();

   ...
}

答案 1 :(得分:0)

我通过分别模拟每个多部分请求摆脱了NPE。例如:

function validateForm() {

    var userId = document.myForm.userName.value;
    var password = document.myForm.passwordOne.value;
    var passwordVerify = document.myForm.passwordTwo.value;
    var fName = document.myForm.firstName.value;
    var lName = document.myForm.lastName.value;
    var emailAdd = document.myForm.email.value;
    var phone = document.myForm.phonenumber.value;

    if (uName(userId)) {
        if (validatePassword(password)) {
            if (passwordCheck(password, passwordVerify)) {
                if (onlyLetters(fName)) {
                    if (onlyLetters(lName)) {
                        if (validateEmail(emailAdd)) {
                            if (phonenumber(phone)) {
                            }
                        }
                    }
                }
            }
        }
    }
    return false;
}


function uName(userId){

    var userId_len = userId.value.length;
    var alphaNumber = /^[\w ]+$/;

    if (userId_len == 0)
    {
        alert("User Id should not be empty");
        userID.focus();
        return false;
    }
    else if (userId.value.match(alphaNumber)) {
        return true;
    }
    else {
        alert("User Id should be alphanumeric");
        userId.focus();
        return false;
    }
    }

此外,您还必须相应地在实际代码中拆分请求。