1

spring boot web integration test, need load test data first. Now I used below way

@ActiveProfiles("test")
@Sql({"/test-schema.sql","/test-user-data.sql"})
public class FooControllerWebIntegrationTest {...}

it's ok, but I found when execute every test method, it will load test data repeatedly. See below:

2015-12-30 15:58:18.398  INFO 4739 --- [           main] o.s.jdbc.datasource.init.ScriptUtils     : Executing SQL script from class path resource [test-schema.sql]
2015-12-30 15:58:18.403  INFO 4739 --- [           main] o.s.jdbc.datasource.init.ScriptUtils     : Executed SQL script from class path resource [test-schema.sql] in 5 ms.
2015-12-30 15:58:18.403  INFO 4739 --- [           main] o.s.jdbc.datasource.init.ScriptUtils     : Executing SQL script from class path resource [test-user-data.sql]
2015-12-30 15:58:18.412  INFO 4739 --- [           main] o.s.jdbc.datasource.init.ScriptUtils     : Executed SQL script from class path resource [test-user-data.sql] in 8 ms.

but I want only load once when test the whole class and even load once when execute mvn package, how could I do to achieve this purpose?

zhuguowei
  • 8,401
  • 16
  • 70
  • 106

3 Answers3

1

What is the problem with loading the schema and data for every test?

If your issue is the schema is recreated for every test, then you can always clean up the schema before the database objects are created.

If your issue is the data created by previous test is not available anymore for the other test, then you have issue with your test because your test should not depends on other tests. Every test should prepare the test data, execute the test and verify the result individually.

Wins
  • 3,420
  • 4
  • 36
  • 70
  • Yeah, in some controller method need to check username is valid,that is it exists in user table. So I want to prepare some test data(e.g. insert into user(name) values('foo')). so I think no need to load this test data every time just once is ok , and maybe take less time to finish whole test process. – zhuguowei Dec 30 '15 at 10:01
  • If what you need is to test if check user name is valid, then you should prepare your data as part of your test, not in the script. The reason is because that test data is only valid for that particular test, and not for other test. – Wins Dec 31 '15 at 06:34
0

If you look at : @Sql annotation documentation

there is a Sql.ExecutionPhase where you can configure when the sql is executed.

Otherwise you have here the same question answered here:

@SQL one time per class

Community
  • 1
  • 1
  • Thanks! but in `Sql.ExecutionPhase` I only found `BEFORE_TEST_METHOD` and `AFTER_TEST_METHOD`, both are method level, without `before_test_class` and `after_test_class`. – zhuguowei Dec 30 '15 at 09:03
0

This sounds like job for database migration tools like Liquibase or Flyway. Spring Boot has integration for both.

luboskrnac
  • 23,973
  • 10
  • 81
  • 92