Mocking methods of local scope objects with Mockito2.7












0















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.










share|improve this question

























  • 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
















0















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.










share|improve this question

























  • 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














0












0








0








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.










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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



















  • 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












2 Answers
2






active

oldest

votes


















0














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);





share|improve this answer
























  • 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



















0














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());
}


`






share|improve this answer


























  • 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














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
});


}
});














draft saved

draft discarded


















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









0














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);





share|improve this answer
























  • 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
















0














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);





share|improve this answer
























  • 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














0












0








0







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);





share|improve this answer













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);






share|improve this answer












share|improve this answer



share|improve this answer










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



















  • 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













0














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());
}


`






share|improve this answer


























  • 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


















0














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());
}


`






share|improve this answer


























  • 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
















0












0








0







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());
}


`






share|improve this answer















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());
}


`







share|improve this answer














share|improve this answer



share|improve this answer








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





















  • 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




















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Bressuire

Vorschmack

Quarantine