0

Restful web serviceを使用するためのテストケースを書きたいと思いSpring testing frameworkます。サービスをモックして、テストケースを正常に実行できました。

ただし、サービスが嘲笑されるため、空の応答が返されます。そのため、サービスからの期待される出力を設定したいと思います。

Mockitoまたはのようなさまざまなモッキングフレームワークを使用してそれを実現でき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

構成の関連部分は表示されていませんが、完全に可能である必要があります。Spring のベスト プラクティスに従って、インターフェイスTodoServiceを使用してデータ レイヤーの依存関係を注入する必要があります。これらの依存関係は、(Spring 構成を使用して) 必要なテスト データを提供するインターフェイスを実装するダミー/スタブ クラスに置き換えることができます。

于 2014-09-11T09:40:30.017 に答える