在Play Framework中测试Actor但数据库已关闭

时间:2013-01-31 21:59:42

标签: database testing playframework-2.0 akka

我正在使用Play 2.0.4,我正在为使用数据库的演员做一个测试单元。 测试开始很好,但是在给定时刻,与数据库的连接关闭,正在运行的actor失败。

代码:

public class ActorTest extends Helpers {

    private FakeApplication app;
    private ActorSystem actorSystem;
    private ActorRef actorRef;
    private BankAccount account;

    @Before
    public void initTest() {

        Map<String, String> params = new HashMap<String, String>();
        params.put("db.default.driver", "com.mysql.jdbc.Driver");
        params.put("db.default.url", "mysql://root:XXXX@localhost/YYY");
        params.put("ebean.default", "models.*");


        app = fakeApplication(params);
        actorSystem = play.api.libs.concurrent.Akka.system(app.getWrappedApplication());
    }

    @Test
    public void updateAccountTransaction() {
        running(app, new Runnable() {
            @Override
            public void run() {

                account = BankAccount.find.byId(new Long(1));

                actorRef = actorSystem.actorOf(new Props(new UntypedActorFactory() {
                    @Override
                    public UntypedActor create() {
                        return new AccountTaskActor(account);
                    }
                }));

                Calendar fromDate = Calendar.getInstance();
                ....
                ....  
                Calendar toDate = Calendar.getInstance();

                final InputRangeDateMessage param = new InputRangeDateMessage(fromDate, toDate);

                junit.framework.Assert.assertNotNull(account);

                Future<Object> future = Patterns.ask(actorRef, param, 1000000);

                Promise<Object> sdf = Akka.asPromise(future);
                Promise<Result> r2 = sdf.map(new Function<Object, Result>() {
                    @Override
                    public Result apply(Object response) throws Throwable {
                        if (response instanceof ErrorMessage) {
                            ErrorMessage e = (ErrorMessage) response;
                            System.out.println("Error Message " + e.getErrorText());

                            junit.framework.Assert.assertEquals(e.getErrorCode(), -1);

                        } else if (response instanceof BankAccountMessage) {
                            BankAccount a = ((BankAccountMessage) response).getAccount();
                            System.out.println("BankAccount " + a.accountsLastUpdate);
                        }
                        return ok();
                    }
                });

                Result test2;
                test2 = async(r2);

            }
        });
    }
}

1 个答案:

答案 0 :(得分:0)

AFAIK,您必须等待Promise

的结束
...
Result test2 = r2.get();