mardi 1 novembre 2016

Play 2.5 jUnit test with WithServer. Why are some mock routes are used?

I have such test:

import static play.test.Helpers.GET;
import static play.test.Helpers.route;

import java.util.concurrent.CompletionStage;
import java.util.concurrent.ExecutionException;

import com.google.common.collect.ImmutableMap;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import play.db.Database;
import play.db.Databases;
import play.db.evolutions.Evolutions;
import play.libs.ws.WS;
import play.libs.ws.WSClient;
import play.libs.ws.WSResponse;
import play.mvc.Http.RequestBuilder;
import play.mvc.Result;
import play.test.WithServer;

/**
 * Created by aieremenko on 10/31/16.
 */
public class OrdersFunctionalTest extends WithServer {
    Database database;

    @Before
    public void startServer() {
        database = Databases.createFrom(
            "default",
            "com.mysql.jdbc.Driver",
            "jdbc:mysql://localhost:3307/test",
            ImmutableMap.of(
                "user", "test",
                "password", "test"
            )
        );

        Evolutions.applyEvolutions(database);

        super.startServer();
    }

    @After
    public void stopServer() {
        super.stopServer();
        Evolutions.cleanupEvolutions(database);
    }


    @Test
    public void testGettingPriorities() throws ExecutionException, InterruptedException {
        final String uri = "/api/orders/priorities";
        WSClient ws = WS.newClient(this.testServer.port());
        CompletionStage<WSResponse> stage = ws.url(uri).get();
        WSResponse response = stage.toCompletableFuture().get();
        String body = response.getBody();
        System.out.println(body);

        RequestBuilder request = new RequestBuilder()
            .method(GET)
            .uri(uri);

        Result result = route(request);
        System.out.println(result.body());
    }
}

But I cannot get a response from my controller neither by http client nor through route helper, because I can see that there are some two fakeRoutes inside the FakeApplication which is provided by WithServer helper, but I need my routes file be parsed and all be integrated.

package controllers.api;

import java.util.UUID;

import com.avaje.ebean.Ebean;
import com.avaje.ebean.PagedList;
import com.avaje.ebean.text.PathProperties;
import com.google.inject.Inject;

import dto.OrderDTO;
import models.customer.Order;
import play.Environment;
import play.Logger;
import play.data.Form;
import play.data.FormFactory;
import play.libs.Json;
import play.mvc.Controller;
import play.mvc.Result;
import play.mvc.Security;
import services.OrderService;
import utils.ResultUtils;

/**
 * Created by aieremenko on 10/24/16.
 */

public class OrdersController extends Controller {

    @Inject
    private FormFactory formFactory;

    @Inject
    private OrderService orderService;

    public Result priorities() {
        return ok(Json.toJson(orderService.getOrderPriorities()));
    }

config/routes

GET    /api/orders/priorities controllers.api.OrdersController.priorities()

Why do this create some fake-routes? What sense of this?

Aucun commentaire:

Enregistrer un commentaire