0

I'm really lost with my error, I saw a lot of documentation and not able to find my error.

I'm testing a method service, which has a class and method inside:

@Service
public class myServiceImpl implements myService {
    public void myMethod(MyObject myObject) throws Exception {
         Object2 object2 = new Object2(myObject) // just some mapping and properties
         MyClass myClass = new MyClass();
         myClass.doStuff(object2); // this is a void method
    }
}

so my test is like this:

@RunWith(PowerMockRunner.class)
@PowerMockIgnore({"javax.managenemt.*"}
public class MyServiceImplTest{
   @InjectMocks
   private MyServiceImpl myService;

  @Test
  public void myMethodTest() throws Exception {
    MyClass myClass = mock(MyClass.class);
    doNothing().when(myClass).doStuff(any());

    MyObject myObject = //get from mock file, it works
    myService.myMethod(myObject);
  }
}

the test crashes because it the doStuff method is invoked, so my mock is not working.

What I'm doing wrong?

cucuru
  • 3,456
  • 8
  • 40
  • 74
  • Does this answer your question? [Why are my mocked methods not called when executing a unit test?](https://stackoverflow.com/questions/74027324/why-are-my-mocked-methods-not-called-when-executing-a-unit-test) – you are doing `new MyClass()` in your service which means that your `myClass` mock is never used by your class under test – knittl May 15 '23 at 20:18

1 Answers1

0

The possible reason is using powermockito. PowerMockito is used to mock static methods, final classes, and other advanced scenarios. Seems that in your case it`s not necessary. The test with mockito should look like this:

    @ExtendWith(MockitoExtension.class)
    public class MyServiceImplTest {

    @Mock
    private MyClass myClass;

    private MyServiceImpl myService;

    @BeforeEach
    public void setUp() {
        myService = new MyServiceImpl(myClass);
    }

    @Test
    public void myMethodTest() throws Exception {
        MyObject myObject = // get from mock file, it works

        Mockito.lenient().doNothing().when(myClass).doStuff(any());

        myService.myMethod(myObject);

        // Add assertions or verifications if needed
    }
}
Feel free
  • 758
  • 5
  • 15
  • thanks but it still doesnt work – cucuru May 15 '23 at 05:55
  • Can you provide your exception? And what dependency you are using for testing? – Feel free May 15 '23 at 06:00
  • the exception is a nullpointerexception, it happens because the doStuff does something like object.getProperty().getValue(), so the doNothing is not working. I'm using Junit 5.5.2 and mockito 3.1.0 – cucuru May 15 '23 at 06:07
  • Can you try my updated code? Maybe it`s better to inject mock by constructor – Feel free May 15 '23 at 06:14
  • i already did, in fact it was one of my tries yesterday, I also updated the service with @requiredArgsconstructor, then the test works but it does not the application. – cucuru May 15 '23 at 06:34
  • Does doStuff() method has any dependency inside? Is nullPointer there? Can you put a breakpoint inside this method? Maybe we need to mock some methods there – Feel free May 15 '23 at 11:23
  • as I'm doing a "doNothing()", is it important what is inside the method?? I mean, the doNothing should just skip this, no? – cucuru May 16 '23 at 08:51
  • If the doStuff method in your MyClass implementation performs operations like object.getProperty().getValue(), then calling Mockito.lenient().doNothing().when(myClass).doStuff(any()) won't prevent those operations from being executed. The doNothing() stubbing only affects the behavior of the doStuff method itself, not the methods called within it. – Feel free May 16 '23 at 08:56
  • I think you need to use one more when() to mock this object.getProperty().getValue() or You can use Mockito.spy() to create a partial mock of myClass and mock the specific methods (getProperty(), getValue()) that need to return desired values without executing their real logic. – Feel free May 16 '23 at 08:57
  • The mock is never used by the CUT. See the reason(s) explained here: https://stackoverflow.com/questions/74027324/why-are-my-mocked-methods-not-called-when-executing-a-unit-test – knittl May 28 '23 at 06:56