0

I need a clarification about mocking tests. In my particular scenario, I have to test a service, that has the dependency on the connecting module, the connector. Basically what connector does, is that it creates an instance of the service call that has to be made. I will demonstrate on the example.

public DataService(Connector connector) { this.connector = connector; } @Override public ServiceData getWeatherData(String dataId) throws ServiceCommunicatonException { try { return connector.newGetWeatherDataCall().call( WeatherData.builder().withId(dataId).build()); } catch (Exception e) { throw new ServiceCommunicatonException(ERR_MSG); } } 

So the connector.newGetWeatherDataCall() returns the instance of the type WeatherData.

Now, in order to test the Service, I guess I would need to mock the Connector. Mocking the Service is probably pointless, because then I am not really testing it, right?

I tried mocking the Connector with something like this:

@Before public void setUp() { connector = mock(Connector.class); } @Test public void getDataTest() { assertNotNull(service.getData("123")); } 

However, this is obviously wrong, because this gave ma NullPointerException because WeatherDataCall from this line: return

connector.newGetWeatherDataCall().call( WeatherData.builder().withId(dataId).build()); was null. 

So how should this be properly tested/mocked? It would be great with some code examples.

2
  • what is the return type of connector.newGetWeatherDataCall() ? name of the class Commented Nov 15, 2016 at 19:32
  • WeatherData. I have added that in my edit. Commented Nov 15, 2016 at 19:41

2 Answers 2

2
@Test public void getDataTest() { WeatherData getWeatherDataResponse = Mockito.mock(WeatherData.class); when(connector.newGetWeatherDataCall()).thenReturn(getWeatherDataResponse); when(getWeatherDataResponse.call(Matchers.any(Class.class))).thenReturn(new ServiceData()); assertNotNull(service.getData("123")); } 

Explanation:-

  • You got null because you did not set the expected return value. Actually connector.newGetWeatherDataCall() returns null. This is because you did not use Mockito.when() to return your expected results.
  • Second : In your case, a method on this return value is invoked so connector.newGetWeatherDataCall() should return a mock of WeatherData. And now you will set an expectation for weatherData.call(..) which will be ServiceData type.
Sign up to request clarification or add additional context in comments.

5 Comments

Yes, that makes sense, thank you. In addition, is that a sensible test to write for this scenario? I mean, I can't really check the returned values from the service call, because I mock it, and I don't really have an idea on what to test besides of the call result being not null. However, this is also a little funny to test, because you indeed set it to be not null. I am a little bit confused by the concept what exactly is being tested here.
getWeatherData(String dataId) is taking a parameter. So can set some for this dataId I should get this response. Also you can check for null values.
You are unit testing public ServiceData getWeatherData(String dataId) . All its dependencies should be mocked. It's like you are given some job and you are like a manager. If I want to unit test you, I will assume that your dependencies are working fine. Meaning it is known what output they will return for a particular input. Now I will test your correctness and evaluate(which is assert). Summary is assuming that your dependencies are returning results correctly as per their definition, is your current method doing its JOB...
it is difficult(or tricky) for you to understand because there is no business logic in this case.
Can you accept the answer if it was satisfactory and worked for you.
1

You can also set up the mock to throw an exception, which is useful if the catch clause does a bit more work.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.