package com.tandogan.geostuff.opencagedata;

import com.tandogan.geostuff.opencagedata.entity.EntityComponentScanMarker;
import com.tandogan.geostuff.opencagedata.entity.GeocodeResponse;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
import org.springframework.core.env.Environment;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import org.springframework.web.client.RestOperations;

import static org.junit.Assert.assertNull;

@ComponentScan(basePackageClasses = {EntityComponentScanMarker.class, RepositoryComponentScanMarker.class})
@PropertySources({
        @PropertySource("classpath:/opencagedata-default.properties"),
        @PropertySource(value = "file:${HOME}/.config/opencagedata", ignoreResourceNotFound = true)
})
@RunWith(MockitoJUnitRunner.class)
public class GeocodeRepositoryImplTest
{
    @Autowired
    private Environment env;

    @Mock
    RestOperations restTemplate;

    @InjectMocks
    GeocodeRepositoryImpl testable;

    @Before
    public void setUp()
    {
        MockitoAnnotations.initMocks(this);

        testable.setApiKey(env.getProperty("${API_KEY}"));
        testable.setUrlBase("http://api.opencagedata.com/geocode/v1/json");
    }

    @Test
    public void testFoo()
    {
        GeocodeResponse result = testable.query("foo");
        assertNull(result);
    }
}

@@ -396,32 +396,89 @@
 itoJUnitRunner;%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A
 import org.sprin
@@ -790,192 +790,228 @@
 ork.
-test.context.ContextConfiguration;%0Aimport org.springframework.test.context.junit4.SpringJUnit4ClassRunner;%0Aimport org.springframework.test.context.support.AnnotationConfigContextLoader
+http.HttpMethod;%0Aimport org.springframework.http.MediaType;%0Aimport org.springframework.test.web.client.MockRestServiceServer;%0Aimport org.springframework.web.client.RestTemplate;%0A%0Aimport static org.junit.Assert.assertNull
 ;%0Aim
@@ -1007,32 +1007,39 @@
 ertNull;%0Aimport 
+static 
 org.springframew
@@ -1034,32 +1034,37 @@
 springframework.
+test.
 web.client.RestO
@@ -1062,66 +1062,141 @@
 ent.
-RestOperations;%0A%0Aimport static org.junit.Assert.assertNull
+match.MockRestRequestMatchers.method;%0Aimport static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess
 ;%0A%0A@
@@ -1571,16 +1571,116 @@
 lTest%0A%7B%0A
+    private static final Logger LOGGER = LoggerFactory.getLogger(GeocodeRepositoryImplTest.class);%0A%0A
     @Aut
@@ -1738,18 +1738,16 @@
 Rest
-Operations
+Template
  res
@@ -1808,24 +1808,63 @@
  testable;%0A%0A
+    MockRestServiceServer mockServer;%0A%0A
     @Before%0A
@@ -1950,56 +1950,112 @@
     
-testable.setApiKey(env.getProperty(%22$%7BAPI_KEY%7D%22)
+mockServer = MockRestServiceServer.createServer(restTemplate);%0A%0A        testable.setApiKey(%22foobar-4711%22
 );%0A 
@@ -2170,32 +2170,281 @@
 testFoo()%0A    %7B%0A
+        String data = %22%7B %5C%22id%5C%22 : %5C%2242%5C%22, %5C%22name%5C%22 : %5C%22Holiday Inn%5C%22%7D%22;%0A        // TODO the mockServer should actually respond to any urls%0A        mockServer.expect(method(HttpMethod.GET)).andRespond(withSuccess(data, MediaType.APPLICATION_JSON));%0A%0A
         GeocodeR

Use MockRestServiceServer