Home > Software engineering >  SpringBoot Mockito: when..thenReturn giving an exception
SpringBoot Mockito: when..thenReturn giving an exception

Time:11-04

So, currently, I'm testing on a Service class

This is my ConvertService.java

@Service
public class ConvertService {

  private final NetworkClient networkClient; //NetworkClient is a Service too

  private final ConvertUtility convertUtility; 

  public ConvertService(Network networkClient) {
    convertUtility = ConvertFactory.of("dev", "F");
    this.networkClient = networkClient

  }


  public Response convert(Request request) {

    User user = networkClient.getData(request.getId()); //User is POJO class
    Context context = convertUtility.transform(request.getToken()) //getToken returns a String
    //Context is a normal Java 

  }

}

This is my ConvertServiceTest.java

@SpringBootTest
@RunWith(MockitoJunitRunner.class)
class ConvertServiceTest {

  @MockBean
  private NetworkClient networkClient;

  @Mock
  ConvertUtility convertUtility;

  private ConvertService convertService;

  @BeforeEach
  void setUp() {
    convertService = new ConvertService(networkClient);
  }

  private mockMethod() {
    Request request = Request(1000);
    User user = new User("user1");
    Context context = new Context();

    when(networkClient.getData(anyLong())).thenReturn(user);
    when(convertUtility.transform(any(String.class)).thenReturn(context);
     
     Response response = convertService.convert(request);  //it throws me an exception here
    }
  }

convertService.convert(request); throws an exception

pointing inside convertUtility.transform(request.getToken())

I'm not sure why it's processing everything from transform method, when I wrote
when(convertUtility.transform(any(String.class)).thenReturn(context);

Can anyone please help?

EDIT: ConvertUtility is a read-only library

CodePudding user response:

Inside your public constructor, you're using a static factory method to get an instance of the ConvertUtility. You'd have to mock the static ConvertUtility.of() method to work with a mock during your test.

While Mockito is able to mock static methods, I'd recommend refactoring (if possible) your class design and accepting an instance of ConvertUtility as part of the public constructor:

@Service
public class ConvertService {

  private final NetworkClient networkClient; //NetworkClient is a Service too
  private final ConvertUtility convertUtility;

  public ConvertService(Network networkClient, ConvertUtility convertUtility) {
    this.convertUtility = convertUtility
    this.networkClient = networkClient

  }
}

With this change, you can easily mock the collaborators of your ConvertService when writing unit tests:

@ExtendWith(MockitoExtension.class)
class ConvertServiceTest {

   @Mock
   private NetworkClient networkClient;

   @Mock
   private ConvertUtility convertUtility;

   @InjectMocks
   private ConvertService convertService;

   @Test // make sure it's from org.junit.jupiter.api
   void yourTest() {


   }
}
  • Related