title: Mockito Injecting Null values into a Spring bean when using @Mock?
body: <p>As I am new to Spring Test MVC I don't understand this problem. I took the code below from  <a href="http://markchensblog.blogspot.in/search/label/Spring" rel="nofollow">http://markchensblog.blogspot.in/search/label/Spring</a></p>
  
  <p>Variable <code>mockproductService</code> is not injected from Application Context and it contains <code>null</code> values while using <code>@Mock</code> annotation and getting assetion error.</p>
  
  <p>The Assertion error I currently encounter is as follows:</p>
  
  <pre><code>java.lang.AssertionError: Model attribute 'Products' expected:&lt;[com.pointel.spring.test.Product@e1b42, com.pointel.spring.test.Product@e1f03]&gt; but was:&lt;[]&gt;
      at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:60)
      at org.springframework.test.util.AssertionErrors.assertEquals(AssertionErrors.java:89)
      at org.springframework.test.web.servlet.result.ModelResultMatchers$2.match(ModelResultMatchers.java:68)
      at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:141)
      at com.pointel.spring.test.ProductControllerTest.testMethod(ProductControllerTest.java:84)
  </code></pre>
  
  <p>Note: If I use <code>@Autowired</code> instead of <code>@Mock</code> it is working fine.</p>
  
  <p>Test Controller class</p>
  
  <pre><code>RunWith(SpringJUnit4ClassRunner.class)
  @WebAppConfiguration
  @ContextConfiguration(locations={"classpath:mvc-dispatcher-servlet.xml"})
  @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class})
  public class ProductControllerTest {
  
      @Autowired
      private WebApplicationContext wac;
  
      private MockMvc mockMvc;
  
     @InjectMocks
      private ProductController productController;
  
      @Mock
      //@Autowired
      private ProductService mockproductService;
  
  
      @Before
      public void setup() {
  
      MockitoAnnotations.initMocks(this);
  
      List&lt;Product&gt; products = new ArrayList&lt;Product&gt;();
      Product product1 = new Product();
      product1.setId(new Long(1));
  
      Product product2 = new Product();
      product2.setId(new Long(2));
  
      products.add(product1);
      products.add(product2);
  
      Mockito.when(mockproductService.findAllProducts()).thenReturn(products);
  
      this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
  
      }
  
      @Test
      public void testMethod() throws Exception {
  
      List&lt;Product&gt; products = new ArrayList&lt;Product&gt;();
  
      Product product1 = new Product();
      product1.setId(new Long(1));
  
      Product product2 = new Product();
      product2.setId(new Long(2));
  
      products.add(product1);
      products.add(product2);
  
      RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/products");
  
      this.mockMvc.perform(requestBuilder).
          andExpect(MockMvcResultMatchers.status().isOk())
          .andExpect(MockMvcResultMatchers.model().attribute("Products", products))
             //.andExpect(MockMvcResultMatchers.model().size(2))
          .andExpect(MockMvcResultMatchers.view().name("show_products"));
  
  
      }
  }
  </code></pre>
  
  <p>Controller class</p>
  
  <pre><code>@Controller
  public class ProductController {
  
      @Autowired
      private ProductService productService;
  
      @RequestMapping("/products")
      public String testController(ModelMap model){
          model.addAttribute("Products",productService.findAllProducts());
          return "show_products";
      }
  }
  </code></pre>
  
  <p>WebServletContext  mvc-dispatcher-servlet.xml</p>
  
  <pre><code>&lt;bean id="someDependencyMock" class="org.mockito.Mockito" factory-method="mock"&gt;
      &lt;constructor-arg value="com.pointel.spring.test.ProductService" /&gt;
  &lt;/bean&gt;
      &lt;context:component-scan base-package="com.pointel.spring.test" /&gt;
  
  &lt;bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" &gt;     
      &lt;property name="prefix" value="/WEB-INF/jsp/" /&gt;
      &lt;property name="suffix" value=".jsp" /&gt;
  &lt;/bean&gt;
  </code></pre>