开发者

Play Framework automatically deletes all records in the database when you run unit Test

开发者 https://www.devze.com 2023-03-17 06:16 出处:网络
Whenever I run Automated Tests on开发者_运维技巧 my site, all the tables get cleaned to 0 rows. Is that by design? How do I prevent them?

Whenever I run Automated Tests on开发者_运维技巧 my site, all the tables get cleaned to 0 rows. Is that by design? How do I prevent them?

Update: Found the real 'culprit' %test.jpa.ddl=create


That is by design. Unit tests should not have any external dependencies on some pre-existing state such as persisted data. If you need data for testing purposes, you need to set that up in your @Before setup method. For example:

@Before
public void setUp() {
    // The following loads test data from the YAML file
    Fixtures.loadModels("test-data/users.yml");
}

@Test
public void someTest() {
    assertEquals(5, User.count()); // 5 User records exist due to @Before method
}

You should take a look at your conf/application.conf file and notice that you have a line that reads:

%test.db=mem

This is the default setting - which says that when the application is run in test mode, use an in-memory database. If you want your tests to work on persisted data (not recommended), you can change the test mode db settings. See Play test documentation for details.


I had the same problem until I updated application.conf file:

%test.jpa.ddl=none

https://playframework.com/documentation/1.2.4/configuration#jpa

0

精彩评论

暂无评论...
验证码 换一张
取 消