1

Below is my test code.

import org.junit.Test;
import org.mockito.Mockito;

public class classA {

    @Mock
    EncSercice encService;

    @InjectMocks
    ServiceClass service;
    @BeforeEach
    public void setUp() {
      MockitoAnnotations.initMocks(this);
    }


    @Test
    public void testEncryptRegisterTransactionPayload() {
        
        ValidRequest request = new ValidRequest();

        try {
            
            Mockito.when(encService.callFn(Mockito.anyString(), Mockito.any(String[].class)))
                    .thenReturn("Stubbed encrypted message");

            service.sendPayload(request);
            Mockito.verify(encService).callFn(Mockito.anyString(), Mockito.any(String[].class));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Here I'm trying to mock/stub the encService.callFn method. But when i run it, It's actually calling method. I can see the logs which I have mentioned in callFn method. Even i have tried using doReturn().when(), Still it's calling the actual method. How can I solve this?

EncService class:

class EncSercice implements EncSerciceImp {
    public String callFn(final String req, final String[] keys) throws Exception {
    byte[] output = // calling a private method and passing req and keys as args....
    return new String(output);
}
}

// ServiceClass  code 
public void sendPayload(final ValidRequest  req) {

    // some code imp..
    EncSercice encService = new EncSercice();
    String str = encService.callFn(req, keys);
}

note: callFn will return a dynamic string.

  • 1
    Don’t forget to annotate your test class, depending On whether you are using Junit4 or 5 - see : https://stackoverflow.com/a/49655834/681444 – racraman May 20 '23 at 06:47
  • @racraman I have added `@ExtendWith(MockitoExtension.class)` and also `@BeforeEach public void setUp() { MockitoAnnotations.initMocks(this); }` still the actual function is getting called. – raj basilio May 20 '23 at 07:42
  • 1
    Are you using JUnit 4 or JUnit 5? You shouldn't need to do both of those things, since the MockitoExtension actually calls initMocks. – Dawood ibn Kareem May 20 '23 at 07:46
  • And can you show us the `EncSercice` class? There might be something about it that stops it from being stubbed, for example, if it's final. – Dawood ibn Kareem May 20 '23 at 07:49
  • I have updated the question and provided `EncService`. I use `spring-boot-starter-test`. I think it has `junit 5x`. Spring verion 2.7.8. – raj basilio May 20 '23 at 08:29
  • 1
    How does this even work? you provide 2 parameters for the method callFn() in test, but this method needs only one – Feel free May 20 '23 at 09:54
  • In addition to the above question about parameters, must also ask how `ServiceClass` is calling `EncSercice` - is it calling `callFn` directly, or only via `execute` ? If the latter, then even if Mockito was set up correctly, then your `when` would never have any effect (since the mock object does not have the actual `execute` code), so you would need to add a`when` for the `execute` call. – racraman May 20 '23 at 10:12
  • You still haven't shown the annotation you are using. Please edit your question with your actual code. Also, please add the code for ServiceClass. And answer the question @Feelfree asked. – tgdavies May 21 '23 at 00:31
  • Feelfree, I have edited the question. I had to mask some code. For service code, I'm just using @Service annotation. – raj basilio May 21 '23 at 06:40
  • 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 create a `new` instance in your method → the Mockito mock is never used (definitely a dupe) – knittl May 23 '23 at 05:19
  • yes, I was creating `new` instance of EncService. I have used `@Autowired` for `EncService` on service class, then the test case passed. – raj basilio May 27 '23 at 12:14

2 Answers2

1

The class being tested, ServiceClass, should have EncsService as a field, so that mockito could inject it as a dependency.

public class ServiceClass {
   private EncSercice encService;
   public SeviceClass(EncSercice enc) 
   {
      this.encService = enc;
   }

   public void sendPayload(final ValidRequest  req) {

    // some code imp...
    String str = encService.call(req, keys);
0

On my service class I have created object instance with new keyword. Instead of that, When I have used

@Autowired
private EncService encService;

Then the test case passed.