java – Mockito在使用@Mock时将Null值注入Spring bean?

由于我是Spring Test MVC的新手,我不明白这个问题.我从http://markchensblog.blogspot.in/search/label/Spring开始采用以下代码

变量mockproductService不是从Application Context注入的,它在使用@Mock注释和获取断言错误时包含空值.

我目前遇到的断言错误如下:

java.lang.AssertionError: Model attribute 'Products' expected:<[com.pointel.spring.test.Product@e1b42, com.pointel.spring.test.Product@e1f03]> but was:<[]>
    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)

注意:如果我使用@Autowired而不是@Mock,它可以正常工作.

测试控制器类

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<Product> products = new ArrayList<Product>();
    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<Product> products = new ArrayList<Product>();

    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"));


    }
}

控制器类

@Controller
public class ProductController {

    @Autowired
    private ProductService productService;

    @RequestMapping("/products")
    public String testController(ModelMap model){
        model.addAttribute("Products",productService.findAllProducts());
        return "show_products";
    }
}

WebServletContext mvc-dispatcher-servlet.xml

<bean id="someDependencyMock" class="org.mockito.Mockito" factory-method="mock">
    <constructor-arg value="com.pointel.spring.test.ProductService" />
</bean>
    <context:component-scan base-package="com.pointel.spring.test" />

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" >     
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
</bean>

解决方法:

对于我来说,目前还不清楚Spring和Mockito的组合如何从引用的博客源中获取它应该如预期的那样工作.至少我可以解释你的观察:

>您的测试(this.mockMvc.perform())正在处理由Spring创建的Web应用程序上下文.在该上下文中,ProductController由Spring实例化(context:component-scan).然后,将productService与您在mvc-dispatcher-servlet.xml中创建的Mockito mock一起自动装配为someDependencyMock.
>如果通过@Autowired注入mockproductService,Spring会从其上下文中注入someDependencyMock实例.因此,您的Mockito.when()调用在此实例上正常工作,如前所述,该实例已正确连接到ProductController.
>但是如果你通过@Mock注入mockproductService,Mockito会注入一个新的ProductService实例,而不是Spring上下文,因为它根本不了解Spring.因此,您的Mockito.when()调用不会对Spring自动装配的模拟进行操作,因此someDependencyMock保持未初始化状态.

那么对于我来说,关于博客的原始代码是如何工作的,我还不清楚:

>使用@InjectMocks注释的productController属性将由Mockito初始化,甚至正确连接到测试类中的mockproductService.但Spring对该对象一无所知,也不会在this.mockMvc.perform()调用中使用它.因此,我假设您只使用@Autowired注入mockproductService,即使您在测试类中删除了productController属性和MockitoAnnotations.initMocks()调用,您的测试也会按预期工作.

上一篇:java – 抛出异常后继续


下一篇:java – Mockito:想要但没有被引用