dimanche 24 mars 2019

How to mock a class which uses an external service?

I am new with mockito and unit testing and I am trying to build some tests for my class which converts, based on the currency, an amount to euro. What should I set in mockito in order to simulate a call to an external service?

All the times I run the test, it returns me a null value (as actual one) when running the method .execute. I tried to run the application and it works as it should. Therefore I guess I am making something wrong in this test case.

This is a part of code for my test class:

@Mock
private ConversionRepo conversionRepo;

@Test
public void convertToEuroFromGBP()
{
  ConversionRequest conversionRequest = new ConversionRequest(TEN, "GBP");
  assertNoEquals(TEN, conversionRepo.execute(conversionRequest));
}

This is a part of code for my test class:

@Mock
private ConversionRepo conversionRepo;

@Test
public void convertToEuroFromGBP()
{
  ConversionRequest conversionRequest = new ConversionRequest(TEN, "GBP");
  assertNotEquals(TEN, conversionRepo.execute(conversionRequest));
}

This is my ConversionRepo class:

public interface ConversionRepo
{
  BigDecimal execute(ConversionRequest conversionRequest);
}

Do you have any ideas regarding what I am doing wrong? Thanks!

Aucun commentaire:

Enregistrer un commentaire