“Invalid use of argument matchers” but I use matchers only
I wish to test the following getRights method:
public GetProductRp getRights(String aaId, String bbId, String ccId) {
GetProductRp rp = (GetProductRp) webServiceTemplate.marshalSendAndReceive(createRq(aaId, bbId, ccId));
return rp;
}
private GetProductRq createRq(String aaId, String bbId, String ccId) {
GetProductRq rq = new GetProductRq();
GetProductRqBody body = new GetProductRqBody();
body.setaaId(aaId);
body.setbbId(bbId);
body.setccId(ccId);
rq.setBody(body);
return rq;
}
This is my test class:
@RunWith(SpringRunner.class)
@SpringBootTest()
public class ClassTest {
@Autowired
private Class rightClass;
@MockBean
private WebServiceTemplate webServiceTemplate;
@Test
public void getRightsTest() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
GetProductRp response = Helper.createProductRp("xx", "yy");
Method mCreateRq = rightClass.class.getDeclaredMethod("createRq", String.class, String.class, String.class);
mCreateRq.setAccessible(true);
GetProductRq request = (GetProductRq) mCreateRq.invoke(rightClass, "12345678", "12345678", "1111");
Mockito.when(webServiceTemplate.marshalSendAndReceive(request)).thenReturn(response);
Mockito.when(rightClass.getRights(Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).thenReturn(response);
Assert.assertNotNull(response);
}
I receive the error provided in the short description above altough I only use Matchers (Mockito.anyString())....
Any idea?
unit-testing mockito
add a comment |
I wish to test the following getRights method:
public GetProductRp getRights(String aaId, String bbId, String ccId) {
GetProductRp rp = (GetProductRp) webServiceTemplate.marshalSendAndReceive(createRq(aaId, bbId, ccId));
return rp;
}
private GetProductRq createRq(String aaId, String bbId, String ccId) {
GetProductRq rq = new GetProductRq();
GetProductRqBody body = new GetProductRqBody();
body.setaaId(aaId);
body.setbbId(bbId);
body.setccId(ccId);
rq.setBody(body);
return rq;
}
This is my test class:
@RunWith(SpringRunner.class)
@SpringBootTest()
public class ClassTest {
@Autowired
private Class rightClass;
@MockBean
private WebServiceTemplate webServiceTemplate;
@Test
public void getRightsTest() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
GetProductRp response = Helper.createProductRp("xx", "yy");
Method mCreateRq = rightClass.class.getDeclaredMethod("createRq", String.class, String.class, String.class);
mCreateRq.setAccessible(true);
GetProductRq request = (GetProductRq) mCreateRq.invoke(rightClass, "12345678", "12345678", "1111");
Mockito.when(webServiceTemplate.marshalSendAndReceive(request)).thenReturn(response);
Mockito.when(rightClass.getRights(Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).thenReturn(response);
Assert.assertNotNull(response);
}
I receive the error provided in the short description above altough I only use Matchers (Mockito.anyString())....
Any idea?
unit-testing mockito
add a comment |
I wish to test the following getRights method:
public GetProductRp getRights(String aaId, String bbId, String ccId) {
GetProductRp rp = (GetProductRp) webServiceTemplate.marshalSendAndReceive(createRq(aaId, bbId, ccId));
return rp;
}
private GetProductRq createRq(String aaId, String bbId, String ccId) {
GetProductRq rq = new GetProductRq();
GetProductRqBody body = new GetProductRqBody();
body.setaaId(aaId);
body.setbbId(bbId);
body.setccId(ccId);
rq.setBody(body);
return rq;
}
This is my test class:
@RunWith(SpringRunner.class)
@SpringBootTest()
public class ClassTest {
@Autowired
private Class rightClass;
@MockBean
private WebServiceTemplate webServiceTemplate;
@Test
public void getRightsTest() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
GetProductRp response = Helper.createProductRp("xx", "yy");
Method mCreateRq = rightClass.class.getDeclaredMethod("createRq", String.class, String.class, String.class);
mCreateRq.setAccessible(true);
GetProductRq request = (GetProductRq) mCreateRq.invoke(rightClass, "12345678", "12345678", "1111");
Mockito.when(webServiceTemplate.marshalSendAndReceive(request)).thenReturn(response);
Mockito.when(rightClass.getRights(Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).thenReturn(response);
Assert.assertNotNull(response);
}
I receive the error provided in the short description above altough I only use Matchers (Mockito.anyString())....
Any idea?
unit-testing mockito
I wish to test the following getRights method:
public GetProductRp getRights(String aaId, String bbId, String ccId) {
GetProductRp rp = (GetProductRp) webServiceTemplate.marshalSendAndReceive(createRq(aaId, bbId, ccId));
return rp;
}
private GetProductRq createRq(String aaId, String bbId, String ccId) {
GetProductRq rq = new GetProductRq();
GetProductRqBody body = new GetProductRqBody();
body.setaaId(aaId);
body.setbbId(bbId);
body.setccId(ccId);
rq.setBody(body);
return rq;
}
This is my test class:
@RunWith(SpringRunner.class)
@SpringBootTest()
public class ClassTest {
@Autowired
private Class rightClass;
@MockBean
private WebServiceTemplate webServiceTemplate;
@Test
public void getRightsTest() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
GetProductRp response = Helper.createProductRp("xx", "yy");
Method mCreateRq = rightClass.class.getDeclaredMethod("createRq", String.class, String.class, String.class);
mCreateRq.setAccessible(true);
GetProductRq request = (GetProductRq) mCreateRq.invoke(rightClass, "12345678", "12345678", "1111");
Mockito.when(webServiceTemplate.marshalSendAndReceive(request)).thenReturn(response);
Mockito.when(rightClass.getRights(Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).thenReturn(response);
Assert.assertNotNull(response);
}
I receive the error provided in the short description above altough I only use Matchers (Mockito.anyString())....
Any idea?
unit-testing mockito
unit-testing mockito
asked Nov 20 '18 at 11:42
dorcsi
368
368
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
The issue here could be that you are putting @Autowired
on private Class rightClass;
and you are trying to mock the method of it. If you want to mock the method then you should put @MockBean
annotation as :
@MockBean
private Class rightClass;
But I do not want to mock that class, I wish to use the original one. By the way, I have other similar classes to test, and all of them are working fine. The only difference is that in those classes there is only one input parameter whish has to be mocked..
– dorcsi
Nov 20 '18 at 12:48
If you don't wish to mock it then why are you writing this Mockito.when(rightClass.getRights(Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).thenReturn(response); ?
– codeLover
Nov 20 '18 at 13:36
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%2f53392273%2finvalid-use-of-argument-matchers-but-i-use-matchers-only%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
The issue here could be that you are putting @Autowired
on private Class rightClass;
and you are trying to mock the method of it. If you want to mock the method then you should put @MockBean
annotation as :
@MockBean
private Class rightClass;
But I do not want to mock that class, I wish to use the original one. By the way, I have other similar classes to test, and all of them are working fine. The only difference is that in those classes there is only one input parameter whish has to be mocked..
– dorcsi
Nov 20 '18 at 12:48
If you don't wish to mock it then why are you writing this Mockito.when(rightClass.getRights(Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).thenReturn(response); ?
– codeLover
Nov 20 '18 at 13:36
add a comment |
The issue here could be that you are putting @Autowired
on private Class rightClass;
and you are trying to mock the method of it. If you want to mock the method then you should put @MockBean
annotation as :
@MockBean
private Class rightClass;
But I do not want to mock that class, I wish to use the original one. By the way, I have other similar classes to test, and all of them are working fine. The only difference is that in those classes there is only one input parameter whish has to be mocked..
– dorcsi
Nov 20 '18 at 12:48
If you don't wish to mock it then why are you writing this Mockito.when(rightClass.getRights(Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).thenReturn(response); ?
– codeLover
Nov 20 '18 at 13:36
add a comment |
The issue here could be that you are putting @Autowired
on private Class rightClass;
and you are trying to mock the method of it. If you want to mock the method then you should put @MockBean
annotation as :
@MockBean
private Class rightClass;
The issue here could be that you are putting @Autowired
on private Class rightClass;
and you are trying to mock the method of it. If you want to mock the method then you should put @MockBean
annotation as :
@MockBean
private Class rightClass;
answered Nov 20 '18 at 12:00
codeLover
2,2591520
2,2591520
But I do not want to mock that class, I wish to use the original one. By the way, I have other similar classes to test, and all of them are working fine. The only difference is that in those classes there is only one input parameter whish has to be mocked..
– dorcsi
Nov 20 '18 at 12:48
If you don't wish to mock it then why are you writing this Mockito.when(rightClass.getRights(Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).thenReturn(response); ?
– codeLover
Nov 20 '18 at 13:36
add a comment |
But I do not want to mock that class, I wish to use the original one. By the way, I have other similar classes to test, and all of them are working fine. The only difference is that in those classes there is only one input parameter whish has to be mocked..
– dorcsi
Nov 20 '18 at 12:48
If you don't wish to mock it then why are you writing this Mockito.when(rightClass.getRights(Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).thenReturn(response); ?
– codeLover
Nov 20 '18 at 13:36
But I do not want to mock that class, I wish to use the original one. By the way, I have other similar classes to test, and all of them are working fine. The only difference is that in those classes there is only one input parameter whish has to be mocked..
– dorcsi
Nov 20 '18 at 12:48
But I do not want to mock that class, I wish to use the original one. By the way, I have other similar classes to test, and all of them are working fine. The only difference is that in those classes there is only one input parameter whish has to be mocked..
– dorcsi
Nov 20 '18 at 12:48
If you don't wish to mock it then why are you writing this Mockito.when(rightClass.getRights(Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).thenReturn(response); ?
– codeLover
Nov 20 '18 at 13:36
If you don't wish to mock it then why are you writing this Mockito.when(rightClass.getRights(Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).thenReturn(response); ?
– codeLover
Nov 20 '18 at 13:36
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53392273%2finvalid-use-of-argument-matchers-but-i-use-matchers-only%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