Mocking methods of local scope objects with Mockito2.7
We are using Maven, Jersey, Mockito 2 in our project. The Mockito 2.7.5 with PowerMockito 1.7.4 dependency is causing some issues: Unable to Mock method of local scope variable. Here is my sample code:
import com.fasterxml.jackson.databind.ObjectMapper;
public Class Sample{
public String method1(String input){
ObjectMapper mapper = new ObjectMapper();
InputDO inputDO = mapper.readValue(input, InputDO.class);
}
}
Inside Test Class
@Test
public void testMethod(){
ObjectMapper mapper = Mockito.mock(ObjectMapper.class);
InputDO = inputDO = Mockito.mock(InputDO.class);
doReturn(inputDO).when(mapper).readValue(anyString(), eq(InputDO.class));
Sample s = Mockito.mock(Ssample.class);
s.method1(anyString());
assertNotNull(s);
}
The Test is failing in mapper.raedValue(). Please help me in mocking the above steps.
java jersey mockito
add a comment |
We are using Maven, Jersey, Mockito 2 in our project. The Mockito 2.7.5 with PowerMockito 1.7.4 dependency is causing some issues: Unable to Mock method of local scope variable. Here is my sample code:
import com.fasterxml.jackson.databind.ObjectMapper;
public Class Sample{
public String method1(String input){
ObjectMapper mapper = new ObjectMapper();
InputDO inputDO = mapper.readValue(input, InputDO.class);
}
}
Inside Test Class
@Test
public void testMethod(){
ObjectMapper mapper = Mockito.mock(ObjectMapper.class);
InputDO = inputDO = Mockito.mock(InputDO.class);
doReturn(inputDO).when(mapper).readValue(anyString(), eq(InputDO.class));
Sample s = Mockito.mock(Ssample.class);
s.method1(anyString());
assertNotNull(s);
}
The Test is failing in mapper.raedValue(). Please help me in mocking the above steps.
java jersey mockito
can you share your Test class as well?
– GauravRai1512
Nov 16 '18 at 11:11
also the exact version of mockito and powermockito
– Maciej Kowalski
Nov 16 '18 at 11:13
add a comment |
We are using Maven, Jersey, Mockito 2 in our project. The Mockito 2.7.5 with PowerMockito 1.7.4 dependency is causing some issues: Unable to Mock method of local scope variable. Here is my sample code:
import com.fasterxml.jackson.databind.ObjectMapper;
public Class Sample{
public String method1(String input){
ObjectMapper mapper = new ObjectMapper();
InputDO inputDO = mapper.readValue(input, InputDO.class);
}
}
Inside Test Class
@Test
public void testMethod(){
ObjectMapper mapper = Mockito.mock(ObjectMapper.class);
InputDO = inputDO = Mockito.mock(InputDO.class);
doReturn(inputDO).when(mapper).readValue(anyString(), eq(InputDO.class));
Sample s = Mockito.mock(Ssample.class);
s.method1(anyString());
assertNotNull(s);
}
The Test is failing in mapper.raedValue(). Please help me in mocking the above steps.
java jersey mockito
We are using Maven, Jersey, Mockito 2 in our project. The Mockito 2.7.5 with PowerMockito 1.7.4 dependency is causing some issues: Unable to Mock method of local scope variable. Here is my sample code:
import com.fasterxml.jackson.databind.ObjectMapper;
public Class Sample{
public String method1(String input){
ObjectMapper mapper = new ObjectMapper();
InputDO inputDO = mapper.readValue(input, InputDO.class);
}
}
Inside Test Class
@Test
public void testMethod(){
ObjectMapper mapper = Mockito.mock(ObjectMapper.class);
InputDO = inputDO = Mockito.mock(InputDO.class);
doReturn(inputDO).when(mapper).readValue(anyString(), eq(InputDO.class));
Sample s = Mockito.mock(Ssample.class);
s.method1(anyString());
assertNotNull(s);
}
The Test is failing in mapper.raedValue(). Please help me in mocking the above steps.
java jersey mockito
java jersey mockito
edited Nov 16 '18 at 11:32
Harsha D G
asked Nov 16 '18 at 11:06
Harsha D GHarsha D G
34
34
can you share your Test class as well?
– GauravRai1512
Nov 16 '18 at 11:11
also the exact version of mockito and powermockito
– Maciej Kowalski
Nov 16 '18 at 11:13
add a comment |
can you share your Test class as well?
– GauravRai1512
Nov 16 '18 at 11:11
also the exact version of mockito and powermockito
– Maciej Kowalski
Nov 16 '18 at 11:13
can you share your Test class as well?
– GauravRai1512
Nov 16 '18 at 11:11
can you share your Test class as well?
– GauravRai1512
Nov 16 '18 at 11:11
also the exact version of mockito and powermockito
– Maciej Kowalski
Nov 16 '18 at 11:13
also the exact version of mockito and powermockito
– Maciej Kowalski
Nov 16 '18 at 11:13
add a comment |
2 Answers
2
active
oldest
votes
Make sure you:
1) Annotate test class with:
@RunWith(PowerMockRunner.class)
@PrepareForTest(ObjectMapper.class)
2) Add this as the first lines to you test method:
ObjectMapper mapper = Mockito.mock(ObjectMapper.class);
PowerMockito.whenNew(ObjectMapper.class).withNoArguments().thenReturn(mapper);
If I add powemock-api-mockito 1.7.4 as dependency then all tests are failing. Might be because I'm using Mockito 2.7.
– Harsha D G
Nov 16 '18 at 12:20
for 2.7 you need powermock 1.7.0 -> github.com/powermock/powermock/wiki/Mockito
– Maciej Kowalski
Nov 16 '18 at 13:07
add a comment |
Test Fail because you create an Mock for ObjectMapper in Test Class but here Every time new ObjectMapper was creating with new keyword. So better will be
`class Test{
ObjectMapper ObjectMapperMock = new ObjectMapper();
ObjectMapper spymapper=spy(ObjectMapperMock);
doReturn(result).when(spymapper.readValue());
}
`
ObjectMapper is a local variable to testMethod() and I can't change the Sample Class
– Harsha D G
Nov 16 '18 at 11:39
Then Instead of using Mock use the Spy
– Rohit Kumar
Nov 16 '18 at 12:35
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%2f53336612%2fmocking-methods-of-local-scope-objects-with-mockito2-7%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Make sure you:
1) Annotate test class with:
@RunWith(PowerMockRunner.class)
@PrepareForTest(ObjectMapper.class)
2) Add this as the first lines to you test method:
ObjectMapper mapper = Mockito.mock(ObjectMapper.class);
PowerMockito.whenNew(ObjectMapper.class).withNoArguments().thenReturn(mapper);
If I add powemock-api-mockito 1.7.4 as dependency then all tests are failing. Might be because I'm using Mockito 2.7.
– Harsha D G
Nov 16 '18 at 12:20
for 2.7 you need powermock 1.7.0 -> github.com/powermock/powermock/wiki/Mockito
– Maciej Kowalski
Nov 16 '18 at 13:07
add a comment |
Make sure you:
1) Annotate test class with:
@RunWith(PowerMockRunner.class)
@PrepareForTest(ObjectMapper.class)
2) Add this as the first lines to you test method:
ObjectMapper mapper = Mockito.mock(ObjectMapper.class);
PowerMockito.whenNew(ObjectMapper.class).withNoArguments().thenReturn(mapper);
If I add powemock-api-mockito 1.7.4 as dependency then all tests are failing. Might be because I'm using Mockito 2.7.
– Harsha D G
Nov 16 '18 at 12:20
for 2.7 you need powermock 1.7.0 -> github.com/powermock/powermock/wiki/Mockito
– Maciej Kowalski
Nov 16 '18 at 13:07
add a comment |
Make sure you:
1) Annotate test class with:
@RunWith(PowerMockRunner.class)
@PrepareForTest(ObjectMapper.class)
2) Add this as the first lines to you test method:
ObjectMapper mapper = Mockito.mock(ObjectMapper.class);
PowerMockito.whenNew(ObjectMapper.class).withNoArguments().thenReturn(mapper);
Make sure you:
1) Annotate test class with:
@RunWith(PowerMockRunner.class)
@PrepareForTest(ObjectMapper.class)
2) Add this as the first lines to you test method:
ObjectMapper mapper = Mockito.mock(ObjectMapper.class);
PowerMockito.whenNew(ObjectMapper.class).withNoArguments().thenReturn(mapper);
answered Nov 16 '18 at 11:42
Maciej KowalskiMaciej Kowalski
13.1k72142
13.1k72142
If I add powemock-api-mockito 1.7.4 as dependency then all tests are failing. Might be because I'm using Mockito 2.7.
– Harsha D G
Nov 16 '18 at 12:20
for 2.7 you need powermock 1.7.0 -> github.com/powermock/powermock/wiki/Mockito
– Maciej Kowalski
Nov 16 '18 at 13:07
add a comment |
If I add powemock-api-mockito 1.7.4 as dependency then all tests are failing. Might be because I'm using Mockito 2.7.
– Harsha D G
Nov 16 '18 at 12:20
for 2.7 you need powermock 1.7.0 -> github.com/powermock/powermock/wiki/Mockito
– Maciej Kowalski
Nov 16 '18 at 13:07
If I add powemock-api-mockito 1.7.4 as dependency then all tests are failing. Might be because I'm using Mockito 2.7.
– Harsha D G
Nov 16 '18 at 12:20
If I add powemock-api-mockito 1.7.4 as dependency then all tests are failing. Might be because I'm using Mockito 2.7.
– Harsha D G
Nov 16 '18 at 12:20
for 2.7 you need powermock 1.7.0 -> github.com/powermock/powermock/wiki/Mockito
– Maciej Kowalski
Nov 16 '18 at 13:07
for 2.7 you need powermock 1.7.0 -> github.com/powermock/powermock/wiki/Mockito
– Maciej Kowalski
Nov 16 '18 at 13:07
add a comment |
Test Fail because you create an Mock for ObjectMapper in Test Class but here Every time new ObjectMapper was creating with new keyword. So better will be
`class Test{
ObjectMapper ObjectMapperMock = new ObjectMapper();
ObjectMapper spymapper=spy(ObjectMapperMock);
doReturn(result).when(spymapper.readValue());
}
`
ObjectMapper is a local variable to testMethod() and I can't change the Sample Class
– Harsha D G
Nov 16 '18 at 11:39
Then Instead of using Mock use the Spy
– Rohit Kumar
Nov 16 '18 at 12:35
add a comment |
Test Fail because you create an Mock for ObjectMapper in Test Class but here Every time new ObjectMapper was creating with new keyword. So better will be
`class Test{
ObjectMapper ObjectMapperMock = new ObjectMapper();
ObjectMapper spymapper=spy(ObjectMapperMock);
doReturn(result).when(spymapper.readValue());
}
`
ObjectMapper is a local variable to testMethod() and I can't change the Sample Class
– Harsha D G
Nov 16 '18 at 11:39
Then Instead of using Mock use the Spy
– Rohit Kumar
Nov 16 '18 at 12:35
add a comment |
Test Fail because you create an Mock for ObjectMapper in Test Class but here Every time new ObjectMapper was creating with new keyword. So better will be
`class Test{
ObjectMapper ObjectMapperMock = new ObjectMapper();
ObjectMapper spymapper=spy(ObjectMapperMock);
doReturn(result).when(spymapper.readValue());
}
`
Test Fail because you create an Mock for ObjectMapper in Test Class but here Every time new ObjectMapper was creating with new keyword. So better will be
`class Test{
ObjectMapper ObjectMapperMock = new ObjectMapper();
ObjectMapper spymapper=spy(ObjectMapperMock);
doReturn(result).when(spymapper.readValue());
}
`
edited Nov 16 '18 at 12:40
answered Nov 16 '18 at 11:32
Rohit Kumar Rohit Kumar
12
12
ObjectMapper is a local variable to testMethod() and I can't change the Sample Class
– Harsha D G
Nov 16 '18 at 11:39
Then Instead of using Mock use the Spy
– Rohit Kumar
Nov 16 '18 at 12:35
add a comment |
ObjectMapper is a local variable to testMethod() and I can't change the Sample Class
– Harsha D G
Nov 16 '18 at 11:39
Then Instead of using Mock use the Spy
– Rohit Kumar
Nov 16 '18 at 12:35
ObjectMapper is a local variable to testMethod() and I can't change the Sample Class
– Harsha D G
Nov 16 '18 at 11:39
ObjectMapper is a local variable to testMethod() and I can't change the Sample Class
– Harsha D G
Nov 16 '18 at 11:39
Then Instead of using Mock use the Spy
– Rohit Kumar
Nov 16 '18 at 12:35
Then Instead of using Mock use the Spy
– Rohit Kumar
Nov 16 '18 at 12:35
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%2f53336612%2fmocking-methods-of-local-scope-objects-with-mockito2-7%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
can you share your Test class as well?
– GauravRai1512
Nov 16 '18 at 11:11
also the exact version of mockito and powermockito
– Maciej Kowalski
Nov 16 '18 at 11:13