Playframework2控制器测试[Java]

时间:2013-06-06 15:54:52

标签: playframework-2.0 playframework-2.1

当我在游戏2.1中执行控制器测试时遇到一些奇怪的问题。

    package controllers;

import static org.fest.assertions.Assertions.assertThat;
import static play.mvc.Http.Status.OK;
import static play.test.Helpers.callAction;
import static play.test.Helpers.charset;
import static play.test.Helpers.contentType;
import static play.test.Helpers.status;

import models.Section;
import models.Topic;

import org.junit.Test;

import play.mvc.Result;

public class TopicsTest extends BaseControllerTest{
    @Test
    public void testCallList() {
        Section s = new Section();
        s.name = "Music section";
        s.id = 1L;
        s.description = "section about music";
        s.save();
        System.out.println(Section.find.where().eq("name", "Music section").findUnique().id); // output 1
        Result result = callAction(controllers.routes.ref.Topics.list(1));
        assertThat(status(result)).isEqualTo(OK);
    }
}

控制器代码非常简单,适用于开发人员。模式很好。 检查一下:

public static Result list(Long id) {
    return ok(list.render(Section.find.byId(id)));
}

错误堆栈跟踪:

java.lang.RuntimeException: java.lang.NoSuchMethodError: models.Section.getName()Ljava/lang/String;
    at play.libs.F$Promise$6.apply(F.java:401)
    at scala.concurrent.Future$$anonfun$map$1.apply(Future.scala:253)
    at scala.concurrent.Future$$anonfun$map$1.apply(Future.scala:249)
    at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:29)
    at akka.dispatch.BatchingExecutor$Batch$$anonfun$run$1.liftedTree1$1(BatchingExecutor.scala:67)
    at akka.dispatch.BatchingExecutor$Batch$$anonfun$run$1.processBatch$1(BatchingExecutor.scala:66)
    at akka.dispatch.BatchingExecutor$Batch$$anonfun$run$1.apply$mcV$sp(BatchingExecutor.scala:82)
    at akka.dispatch.BatchingExecutor$Batch$$anonfun$run$1.apply(BatchingExecutor.scala:59)
    at akka.dispatch.BatchingExecutor$Batch$$anonfun$run$1.apply(BatchingExecutor.scala:59)
    at scala.concurrent.BlockContext$.withBlockContext(BlockContext.scala:72)
    at akka.dispatch.BatchingExecutor$Batch.run(BatchingExecutor.scala:58)
    at akka.dispatch.TaskInvocation.run(AbstractDispatcher.scala:137)
    at scala.concurrent.forkjoin.ForkJoinTask$AdaptedRunnableAction.exec(ForkJoinTask.java:1441)
    at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:262)
    at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:975)
    at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1478)
    at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:104)
Caused by: java.lang.NoSuchMethodError: models.Section.getName()Ljava/lang/String;
    at views.html.topics.list$.apply(list.template.scala:32)
    at views.html.topics.list$.render(list.template.scala:62)
    at views.html.topics.list.render(list.template.scala)
    at controllers.Topics.list(Topics.java:24)
    at controllers.ref.ReverseTopics$$anonfun$list$3.apply(routes_reverseRouting.scala:761)
    at controllers.ref.ReverseTopics$$anonfun$list$3.apply(routes_reverseRouting.scala:761)
    at play.core.Router$HandlerInvoker$$anon$6$$anon$2.invocation(Router.scala:141)
    at play.core.j.JavaAction$$anon$1.call(JavaAction.scala:31)
    at play.core.j.JavaAction$$anon$2.apply(JavaAction.scala:74)
    at play.core.j.JavaAction$$anon$2.apply(JavaAction.scala:73)
    at play.libs.F$Promise$PromiseActor.onReceive(F.java:420)
    at akka.actor.UntypedActor$$anonfun$receive$1.applyOrElse(UntypedActor.scala:159)
    at akka.actor.ActorCell.receiveMessage(ActorCell.scala:425)
    at akka.actor.ActorCell.invoke(ActorCell.scala:386)
    at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:230)
    at akka.dispatch.Mailbox.run(Mailbox.scala:212)
    at akka.dispatch.ForkJoinExecutorConfigurator$MailboxExecutionTask.exec(AbstractDispatcher.scala:502)
    ... 4 more

请帮我找到解决方案,或假冒这个测试。

1 个答案:

答案 0 :(得分:1)

Play似乎无法访问模型类的字段(看起来它不会生成getter和setter)。然后,相当明显的解决方案是明确地编写自己的getter(和setter),这样无论如何Play都可以找到它们。