question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to mockito a new object in a method?

See original GitHub issue

Hey,

How to mockito a new object in a method? for example: `

    public void add(String msg){
           User user = new User();
           boolean isFlag = user.send(msg);
           if(isFlag){
                  .....
           }else{
                  ....
          }
  }

`

How to mock the send method executed by the user object? Similar to “whenNew” method in powermock, is there the same function in mockito?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
afreetgocommented, Jul 16, 2021

It would be better to have a method like powermocker’s “whenNew”.

1reaction
TimvdLippecommented, Feb 3, 2022

You can now mock object construction with the inline mock maker and Mockito.mockConstruction. For more information, see https://javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/Mockito.html#49

Read more comments on GitHub >

github_iconTop Results From Across the Web

Test class with a new() call in it with Mockito - Stack Overflow
You can create spies of real objects. When you use the spy then the real methods are called (unless a method was stubbed)....
Read more >
Creating Mocks and Spies in Mockito with Code Examples
#1) Mock creation with Code ... Mockito gives several overloaded versions of Mockito. Mocks method and allows creating mocks for dependencies.
Read more >
how to mock the new object creation in the method
I want to test the method using the mockito (not using annotations); my method looks like this: public void methodName(){ //do some logic....
Read more >
How to Unit Test Classes Which Create New Objects - DZone
A simple method to write test cases for classes that use new keywords in their method with the help of Mockito and minimum...
Read more >
Mockito's Mock Methods - Baeldung
In this tutorial, we'll illustrate the various uses of the standard static mock methods of the Mockito API.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found