使用弹簧测试框架模拟服务的预期结果

时间:2014-09-11 07:44:29

标签: java spring web-services spring-mvc spring-test-mvc

我想使用Restful web serviceSpring testing framework编写测试用例。我嘲笑了服务并能够成功运行测试用例。

但是,当服务被模拟时,它返回空响应。所以,我想设置服务的预期输出。

我可以使用不同的模拟框架来实现它,例如MockitoJmockit(在下面的代码中,它与Mockito一起使用)。

但是,除了内部without any addition/external testing frameworks之外,它是否可能Spring testing framework

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import java.util.Arrays;

import static org.hamcrest.Matchers.*;
import static org.hamcrest.Matchers.is;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {TestContext.class, WebAppContext.class})
@WebAppConfiguration
public class TodoControllerTest {

    private MockMvc mockMvc;

    @Autowired
    private TodoService todoServiceMock;

    @Test
    public void findAll_ShouldAddTodoEntriesToModelAndRenderTodoListView() throws Exception {

        Todo first = new TodoBuilder()
                .id(2L)
                .description("Lorem ipsum")
                .title("Bar")
                .build();

    /**
    Need mocking technique from Spring Testing Framework
    */
        when(todoServiceMock.findAll()).thenReturn(Arrays.asList(first));

        mockMvc.perform(get("/"))
                .andExpect(status().isOk())
                .andExpect(view().name("todo/list"))
                .andExpect(forwardedUrl("/WEB-INF/jsp/todo/list.jsp"))
                .andExpect(model().attribute("todos", hasSize(2)))
                .andExpect(model().attribute("todos", hasItem(
                        allOf(
                                hasProperty("id", is(1L)),
                                hasProperty("description", is("Lorem ipsum")),
                                hasProperty("title", is("Foo"))
                        )
                )))
                .andExpect(model().attribute("todos", hasItem(
                        allOf(
                                hasProperty("id", is(2L)),
                                hasProperty("description", is("Lorem ipsum")),
                                hasProperty("title", is("Bar"))
                        )
                )));
    }
}

2 个答案:

答案 0 :(得分:1)

一种可能的解决方案是扩展要测试的类,并覆盖要模拟的方法。然后在单独的配置文件中定义一个bean,它将连接到一个真实对象。接下来在测试类中使用此配置。

这实际上是Mockito间谍的行为。如果我是你,我会坚持下去,因为它提供了更多的灵活性并节省了大量样板代码。

答案 1 :(得分:1)

您没有显示配置的相关部分,但应该完全可以。在TodoService中,您应该根据Spring最佳实践使用 interfaces 注入数据层依赖项,并且可以使用实现这些接口的虚拟/存根类替换这些依赖项(使用Spring配置)提供所需的测试数据。