Hello, Freakin world!

단위 테스트의 작성 그리고 완성! 본문

Toy Project/URL Mapping 프레임워크 구현하기

단위 테스트의 작성 그리고 완성!

johnna_endure 2020. 4. 2. 17:47

단위 테스트

...
import static org.assertj.core.api.Assertions.assertThat;

public class TestChatRoomController {
    ChatRoomController controller;
    DummyRequestHandler requestHandler;

    @Before
    public void init() {
        controller = new ChatRoomController();
        requestHandler = new DummyRequestHandler();
    }

    @Test
    public void testInvoke_bracedUrlMethod() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        java.lang.reflect.Method method = getMethod(DummyRequestHandler.class, "bracedURLMethod");
        Response response = controller.invoke(requestHandler,
                        method,
                        new Request(Method.POST, "/hello/2/cws"));
        assertThat(response).isEqualToComparingFieldByField(new Response(200));
    }

    @Test
    public void testInvoke_whenPlainURLMethod() {
        java.lang.reflect.Method method =
                getMethod(DummyRequestHandler.class, "plainURLMethod");
        Response actual = controller.invoke(requestHandler,
                method,
                new Request(Method.GET, "/hello"));
        assertThat(actual).isEqualToComparingFieldByField(new Response(200));
    }

    @Test
    public void testInvoke_whenHybridURLMethod() {
        java.lang.reflect.Method method =
                getMethod(DummyRequestHandler.class, "hybridURLMethod");

        Response actual = controller.invoke(requestHandler,
                method,
                new Request(Method.GET, "/hello/23/name"));
        assertThat(actual).isEqualToComparingFieldByField(new Response(200));
    }

    @Test
    public void testInvoke_whenNoArgumentMethod() {
        java.lang.reflect.Method method =
                getMethod(DummyRequestHandler.class, "noArgumentMethod");

        Response actual = controller.invoke(requestHandler,
                method,
                new Request(Method.GET, "/hello"));
        assertThat(actual).isEqualToComparingFieldByField(new Response(200));
    }

    private java.lang.reflect.Method getMethod(Class clazz, String methodName) {
        return Arrays.stream(clazz.getMethods())
                .filter(m -> m.getName().equals(methodName))
                .findFirst().get();
    }
}

 

테스트 데이터를 위한 DummyRequestHandler

 

public class DummyRequestHandler {
    @RequestMapping(method = Method.POST, url = "/hello/{id}/{name}")
    public Response bracedURLMethod(long id, Request request, String name) {
        return new Response(200);
    }

    @RequestMapping(method = Method.GET, url = "/hello")
    public Response plainURLMethod(Request request) {
        return new Response(200);
    }

    @RequestMapping(method = Method.GET, url = "/hello/{id}/name")
    public Response hybridURLMethod(int id, Request request) {
        return new Response(200);
    }

    @RequestMapping(method = Method.GET, url = "/hello")
    public Response noArgumentMethod() {
        return new Response(200);
    }
}

 

전체 코드를 살펴보고 싶다면...

https://github.com/johnna-endure/chatroom-server

 

johnna-endure/chatroom-server

Contribute to johnna-endure/chatroom-server development by creating an account on GitHub.

github.com

 

 

Comments