title: Mockito+ Rest-Assured not mocking in Spring Boot REST MVC application(Integration Testing)
body: <p>I was trying to write test cases for SpringBoot MVC rest application. I am able to run the test cases successfully. But when i tried to mock one of the methods, its not working. 
  The test case still calls the original implementation.</p>
  
  <p>Test class:-</p>
  
  <pre><code> @RunWith(SpringJUnit4ClassRunner.class)
  @SpringApplicationConfiguration(classes = { Initializer.class,
          WebConfig.class })
  @WebAppConfiguration
  public class SampleControllerTest {
  
      @Mock
      CounterUtil counterUtil;
  
      @InjectMocks
      PreProcessor preProcessor
  
      @Autowired
      private WebApplicationContext context;
      private static boolean isSetup = false;
      @Test
      public void sampleTest() {              
          org.mockito.Mockito.when(
                  counterUtil
                          .getCounter()).thenReturn(
                  "2222");
          given().contentType("application/json").when()
          .get("/initialize").then()
          .statusCode(HttpServletResponse.SC_OK);
      }
      @Before
      public void setUp() {
          RestAssured.useRelaxedHTTPSValidation();
  
              counterUtil = (CounterUtil) context
                      .getBean("counterUtil");    
              MockitoAnnotations.initMocks(this);
               RestAssuredMockMvc.mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
  
      }
  }
  </code></pre>
  
  <p>The PreProcessor is a concreate class, with an instance of CounterUtil.</p>
  
  <pre><code>@Component
  public class PreProcessor {
  
      @Autowired
      private CounterUtil counterUtil
  
  
      public void myMethod(){
          counterUtil.getCounter();
      }
  
  }
  </code></pre>
  
  <p>These are the dependencies in pom.xml.</p>
  
  <pre><code>&lt;!-- test dependencies --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;junit&lt;/groupId&gt;
              &lt;artifactId&gt;junit&lt;/artifactId&gt;
              &lt;version&gt;4.11&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;
              &lt;artifactId&gt;jackson-core&lt;/artifactId&gt;
              &lt;version&gt;2.5.3&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;
              &lt;artifactId&gt;jackson-databind&lt;/artifactId&gt;
              &lt;version&gt;2.5.3&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.mockito&lt;/groupId&gt;
              &lt;artifactId&gt;mockito-core&lt;/artifactId&gt;
              &lt;version&gt;1.9.0&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.hamcrest&lt;/groupId&gt;
              &lt;artifactId&gt;hamcrest-core&lt;/artifactId&gt;
              &lt;version&gt;1.3&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;com.jayway.restassured&lt;/groupId&gt;
              &lt;artifactId&gt;spring-mock-mvc&lt;/artifactId&gt;
              &lt;version&gt;2.4.1&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
  </code></pre>
  
  <p>Not getting any error. The execution works fine, but not considering the the mocked implementation.</p>
  
  <p>Any suggestion or pointer is also welcomed.</p>