模拟:when()需要一个参数,该参数必须是模拟'上的方法调用。

时间:2016-02-07 21:59:04

标签: java spring unit-testing spring-boot mocking

我正在为我的REST-API编写单元测试,并且在实体创建模拟方面存在一些问题。我不知道如何模拟EntityManager。我尝试了下面的例子,但是我收到了一个错误。

我的ControllerTest:

public class MControllerTest {

    private MockMvc mockMvc;

    @InjectMocks A a;
    @InjectMocks B b;
    @InjectMocks AController aController;
    @InjectMocks private AServiceImpl aServiceImpl;

    @Autowired WebApplicationContext webApplicationContext;
    @Autowired private FilterChainProxy springSecurityFilterChain;

    @Autowired
    @InjectMocks
    private EntityManagerFactory entityManagerFactory;


    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
                                 .addFilter(springSecurityFilterChain)
                                 .build();
    }

    @Test
    public void postATest() throws Exception {

        a.setDDDD("XXX");

        EntityManagerFactory entityManagerFactory = webApplicationContext.getBean(EntityManagerFactory.class);
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        when(this.entityManagerFactory.createEntityManager()).thenReturn(entityManager);

        when(aServiceImpl.createEntity(isA(A.class))).thenReturn(a);

        b.setCCCC;
        a.setMovieTranslations(Arrays.asList(b));

        when(aServiceImpl.createEntity(isA(B.class))).thenReturn(a);

        mockMvc.perform(post("/path")
               .andExpect(status().isOk())
               .andReturn().getResponse().getContentAsString();
    }

createEntityMethod:

public Object createEntity(T t) {
    try {
        entityManager.persist(t);
        return t;
    } catch (IllegalArgumentException | EntityExistsException | ...
}

错误日志:

org.mockito.exceptions.misusing.MissingMethodInvocationException: 
when() requires an argument which has to be 'a method call on a mock'.
For example:
    when(mock.getArticles()).thenReturn(articles);

Also, this error might show up because:
1. you stub either of: final/private/equals()/hashCode() methods.
   Those methods *cannot* be stubbed/verified.
   Mocking methods declared on non-public parent classes is not supported.
2. inside when() you don't call method on mock but on some other object.

    at com.x.server.controller.MControllerTest.postATest(MControllerTest.java:121)

当我没有在EntityManager上注入模拟对象时,我通过带有此错误日志的persist方法得到了一个nullpointer异常:

java.lang.NullPointerException: null
    at com.x.server.serviceImpl.AManageServiceImpl.createEntity(AManageServiceImpl.java:45)
    at com.eza.server.controller.MControllerTest.postATest(MControllerTest.java:123)

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

解决它。我刚刚使用entitymanager接口删除了一些代码,它可以工作。

public class MControllerTest {

    private MockMvc mockMvc;

    @InjectMocks A a;
    @InjectMocks B b;
    @InjectMocks AController aController;

    @Autowired WebApplicationContext webApplicationContext;
    @Autowired private FilterChainProxy springSecurityFilterChain;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
                                 .addFilter(springSecurityFilterChain)
                                 .build();
    }

    @Test
    public void postMovieTest() throws Exception {

        a.setDDDD("XXX");
        b.setCCCC;
        a.setMList(Arrays.asList(b));


        mockMvc.perform(post("/path")
            .content(asJsonString(a))
            .contentType(MediaType.APPLICATION_JSON)
            .accept(MediaType.APPLICATION_JSON)
            .andExpect(status().isOk())
            .andReturn().getResponse().getContentAsString();
    }

asJsonString是我自己编写的方法来转换json中的对象

干杯