简体   繁体   English

在测试模式下运行时,Play框架中的Mongo DB中没有数据

[英]No data in Mongo DB in Play framework when running in Test mode

I have play framework application v2.2 that use Mongo Database (using play-jongo ). 我有一个使用Mongo数据库(使用play-jongo )的播放框架应用程序v2.2。 I have some testing unit class in /test folder. 我在/ test文件夹中有一些测试单元类。 However if I run the unit class using test command or run directly from eclipse, there is no data found at all from Mongo DB, but if I run the application normally, I can see the data. 但是,如果我使用test命令运行单元类或直接从eclipse运行,则从Mongo DB根本找不到数据,但是如果我正常运行应用程序,则可以看到数据。 I have used Helpers.faceApplication() method but still no data at all. 我已经使用了Helpers.faceApplication()方法,但仍然没有任何数据。

Here is the unit test code: 这是单元测试代码:

@Test
public void test1()  {
    Helpers.running(Helpers.fakeApplication(), new Runnable() {

        @Override
        public void run() {

            //MyUser is the mongo entity
            MyUser myUser = MyUser.findById("123");
            if (myUser != null) {
                Logger.info("User ID: " + myUser.id);
            } else {
                Logger.info("User is NULL");  //it always get here
            }
        }
    });

}

The myUser is always returned null if I run the test unit. 如果我运行测试单元,则myUser始终返回null。

I have feeling that the Helpers.fakeApplication doesn't read the /conf/applicaton.conf so it doesn't connect to mongo db. 我觉得Helpers.fakeApplication无法读取/conf/applicaton.conf因此无法连接到mongo db。

Anyone know how to connect the play to mongo db when running in test unit? 在测试单元中运行时,有人知道如何将Play连接到mongo db吗?

Just some information that might be helpful. 只是一些可能有用的信息。 When you create the fakeApplication you are also able to provide a database to test with: 创建fakeApplication时,还可以提供一个数据库进行测试:

running(fakeApplication(inMemoryDatabase("test")), new Runnable() {
        public void run() {
            // some assertions here
        }
    });

This will of course use the H2 in-memory-database. 当然,这将使用H2内存数据库。 I am not that familiar with Mongo but with regular SQL databases I would try something like this: 我对Mongo不太熟悉,但是对于常规SQL数据库,我会尝试这样的操作:

final Map<String, String> postgres = new HashMap<String, String>();
postgres.put("db.default.driver", "org.postgresql.Driver");
postgres.put("db.default.url", "jdbc:postgresql://localhost/myDataBase");
postgres.put("db.default.user", "postgres");
postgres.put("db.default.password", "password");

running(fakeApplication(postgres), new Runnable() {
        public void run() {
            // some assertions here
        }
});

I hope this gives you some clue on how to proceed with Mongo. 我希望这会给您一些有关继续进行Mongo的线索。

It turned out that I'm using old play-jongo version (v0.4) that doesn't support loading existing mongo database in test mode. 原来,我使用的是旧的play-jongo版本(v0.4),该版本不支持在测试模式下加载现有的mongo数据库。 So my solution is to update the play-jongo to v0.5 (for play v2.1.x) by modifying the play-jongo dependency in file /project/Build.scala : 所以我的解决方案是通过修改文件/project/Build.scala的play-jongo依赖项将play-jongo更新到v0.5(对于p​​lay v2.1.x):

val appDependencies = Seq(
    ...
    "uk.co.panaxiom" %% "play-jongo" % "0.5.0-jongo0.4"
)

For another play version, please refer to the play-jongo's readme.md . 有关其他播放版本,请参阅play-jongo的readme.md

Next, in /conf/application.conf I added this line: 接下来,在/conf/application.conf我添加了以下行:

playjongo.test-uri="mongodb://127.0.0.1:27017/mydb"

So this is the reason I need to update the play-jongo because the old version doesn't support playjongo.test-uri configuration. 因此,这就是我需要更新play-jongo的原因,因为旧版本不支持playjongo.test-uri配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM