How to mock a POST request with mockk and fuel in kotlin?
I am making some http requests in kotlin with fuel library. I want to test that code using mockk library. I figured out how to mock http requests. Below is the code for that.
val client = mockk<Client>()
every client.executeRequest(any()).statusCode returns 200
every client.executeRequest(any()).responseMessage returns "test"
every client.executeRequest(any()).data returns "abc".toByteArray()
FuelManager.instance.client = client
assertEquals("abc" , testHttpRequest())
I do not like that any()
here. I want to be specific about the http method and the url. I would like to return specific responses based on the url being called and the http method being used.
I figured may be I could do following
val req = Request(Method.POST, "my/test", URL("https://testRequest.com"), timeoutInMillisecond = 3000, timeoutReadInMillisecond = 3000)
every client.executeRequest(req).statusCode returns 200
every client.executeRequest(req).responseMessage returns "OK"
every client.executeRequest(req).data returns "abc".toByteArray()
FuelManager.instance.client = client
But I am getting following error.
io.mockk.MockKException: no answer found for: Client(#1).executeRequest(-->
https://testRequest.com/my/test
"Body : abc"
"Headers : (3)"
Accept-Encoding : compress;q=0.5, gzip;q=1.0
Content-Type : application/json
Authorization : Basic xxx)
What am I missing here?
http kotlin request mockk
add a comment |
I am making some http requests in kotlin with fuel library. I want to test that code using mockk library. I figured out how to mock http requests. Below is the code for that.
val client = mockk<Client>()
every client.executeRequest(any()).statusCode returns 200
every client.executeRequest(any()).responseMessage returns "test"
every client.executeRequest(any()).data returns "abc".toByteArray()
FuelManager.instance.client = client
assertEquals("abc" , testHttpRequest())
I do not like that any()
here. I want to be specific about the http method and the url. I would like to return specific responses based on the url being called and the http method being used.
I figured may be I could do following
val req = Request(Method.POST, "my/test", URL("https://testRequest.com"), timeoutInMillisecond = 3000, timeoutReadInMillisecond = 3000)
every client.executeRequest(req).statusCode returns 200
every client.executeRequest(req).responseMessage returns "OK"
every client.executeRequest(req).data returns "abc".toByteArray()
FuelManager.instance.client = client
But I am getting following error.
io.mockk.MockKException: no answer found for: Client(#1).executeRequest(-->
https://testRequest.com/my/test
"Body : abc"
"Headers : (3)"
Accept-Encoding : compress;q=0.5, gzip;q=1.0
Content-Type : application/json
Authorization : Basic xxx)
What am I missing here?
http kotlin request mockk
I think this kinds of questions are more appropriate to ask to gitter chat or github issues, as they are pretty specific. Anyway this issue is not related to MockK itself, it is about building apropriate request, which may be super hard to achieve. Just debug "Request.equals" method and compare what is included there. Thanks
– oleksiyp
Nov 14 '18 at 17:25
add a comment |
I am making some http requests in kotlin with fuel library. I want to test that code using mockk library. I figured out how to mock http requests. Below is the code for that.
val client = mockk<Client>()
every client.executeRequest(any()).statusCode returns 200
every client.executeRequest(any()).responseMessage returns "test"
every client.executeRequest(any()).data returns "abc".toByteArray()
FuelManager.instance.client = client
assertEquals("abc" , testHttpRequest())
I do not like that any()
here. I want to be specific about the http method and the url. I would like to return specific responses based on the url being called and the http method being used.
I figured may be I could do following
val req = Request(Method.POST, "my/test", URL("https://testRequest.com"), timeoutInMillisecond = 3000, timeoutReadInMillisecond = 3000)
every client.executeRequest(req).statusCode returns 200
every client.executeRequest(req).responseMessage returns "OK"
every client.executeRequest(req).data returns "abc".toByteArray()
FuelManager.instance.client = client
But I am getting following error.
io.mockk.MockKException: no answer found for: Client(#1).executeRequest(-->
https://testRequest.com/my/test
"Body : abc"
"Headers : (3)"
Accept-Encoding : compress;q=0.5, gzip;q=1.0
Content-Type : application/json
Authorization : Basic xxx)
What am I missing here?
http kotlin request mockk
I am making some http requests in kotlin with fuel library. I want to test that code using mockk library. I figured out how to mock http requests. Below is the code for that.
val client = mockk<Client>()
every client.executeRequest(any()).statusCode returns 200
every client.executeRequest(any()).responseMessage returns "test"
every client.executeRequest(any()).data returns "abc".toByteArray()
FuelManager.instance.client = client
assertEquals("abc" , testHttpRequest())
I do not like that any()
here. I want to be specific about the http method and the url. I would like to return specific responses based on the url being called and the http method being used.
I figured may be I could do following
val req = Request(Method.POST, "my/test", URL("https://testRequest.com"), timeoutInMillisecond = 3000, timeoutReadInMillisecond = 3000)
every client.executeRequest(req).statusCode returns 200
every client.executeRequest(req).responseMessage returns "OK"
every client.executeRequest(req).data returns "abc".toByteArray()
FuelManager.instance.client = client
But I am getting following error.
io.mockk.MockKException: no answer found for: Client(#1).executeRequest(-->
https://testRequest.com/my/test
"Body : abc"
"Headers : (3)"
Accept-Encoding : compress;q=0.5, gzip;q=1.0
Content-Type : application/json
Authorization : Basic xxx)
What am I missing here?
http kotlin request mockk
http kotlin request mockk
asked Nov 13 '18 at 12:24
user3745870user3745870
54211
54211
I think this kinds of questions are more appropriate to ask to gitter chat or github issues, as they are pretty specific. Anyway this issue is not related to MockK itself, it is about building apropriate request, which may be super hard to achieve. Just debug "Request.equals" method and compare what is included there. Thanks
– oleksiyp
Nov 14 '18 at 17:25
add a comment |
I think this kinds of questions are more appropriate to ask to gitter chat or github issues, as they are pretty specific. Anyway this issue is not related to MockK itself, it is about building apropriate request, which may be super hard to achieve. Just debug "Request.equals" method and compare what is included there. Thanks
– oleksiyp
Nov 14 '18 at 17:25
I think this kinds of questions are more appropriate to ask to gitter chat or github issues, as they are pretty specific. Anyway this issue is not related to MockK itself, it is about building apropriate request, which may be super hard to achieve. Just debug "Request.equals" method and compare what is included there. Thanks
– oleksiyp
Nov 14 '18 at 17:25
I think this kinds of questions are more appropriate to ask to gitter chat or github issues, as they are pretty specific. Anyway this issue is not related to MockK itself, it is about building apropriate request, which may be super hard to achieve. Just debug "Request.equals" method and compare what is included there. Thanks
– oleksiyp
Nov 14 '18 at 17:25
add a comment |
1 Answer
1
active
oldest
votes
Try to use following:
every client.executeRequest(req) returns <mock object>
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53280953%2fhow-to-mock-a-post-request-with-mockk-and-fuel-in-kotlin%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try to use following:
every client.executeRequest(req) returns <mock object>
add a comment |
Try to use following:
every client.executeRequest(req) returns <mock object>
add a comment |
Try to use following:
every client.executeRequest(req) returns <mock object>
Try to use following:
every client.executeRequest(req) returns <mock object>
answered Nov 15 '18 at 12:08
Vova StelmashchukVova Stelmashchuk
322111
322111
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53280953%2fhow-to-mock-a-post-request-with-mockk-and-fuel-in-kotlin%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I think this kinds of questions are more appropriate to ask to gitter chat or github issues, as they are pretty specific. Anyway this issue is not related to MockK itself, it is about building apropriate request, which may be super hard to achieve. Just debug "Request.equals" method and compare what is included there. Thanks
– oleksiyp
Nov 14 '18 at 17:25