0

我想写一个测试用例来Restful web service使用Spring testing framework. 我模拟了该服务并能够成功运行测试用例。

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

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

without any addition/external testing frameworks但是,除了 internal 之外是否有可能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"))
                        )
                )));
    }
}
4

2 回答 2

1

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

于 2014-09-11T09:40:30.017 回答
1

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

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

于 2014-09-11T09:32:07.090 回答