Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mock controllers / rest endpoints for unit testing in Play Framework 2.x [Java]

We are developing a project with Java on Play Framework 2.x and have some rest endpoints. Also we have some test cases for them like as follows:

    @Test
    public void testLogout() throws Exception {
        FakeRequest request = new FakeRequest("GET", "/product/api/v1/logout");

        Result result = route(request);

        assertThat(status(result)).isEqualTo(OK);
        assertThat(contentType(result)).isEqualTo("application/json");
        assertThat(contentAsString(result)).contains("result");
    }

On the other hand, we have some methods [like register()] which can not test in production database.

What is the correct way to test the methods which affect the prod database? I think mocking but I am not sure that and I don't know how to do. If mocking is a good choice, is there any working example?

Please give me some advice about this issue.

like image 566
Oguz Ozkeroglu Avatar asked May 04 '15 13:05

Oguz Ozkeroglu


1 Answers

I think the correct way is not to test against production database.

I divide the tests in 2 groups, unit tests and integration tests. Unit tests are commonly known, and in integration tests I test everything that is outside the application itself (for example, the database) and the conections between them.

I run the unit tests using a mock in memory database when needed and integration tests against a database with same structure as the production one but not the same database.

I hope my approach will help you.

like image 57
hiamex Avatar answered Oct 15 '22 18:10

hiamex