0

I am doing some JUnit tests and I need to execute a SQL script just once, but it seems that it is executing it one time per test.

I am using the next annotations:

-Main:

@SpringBootApplication
@EnableTransactionManagement

-Test Class:

@Sql({ "classpath:insertMessage.sql" })
@Transactional
public class ServiceTest {

What am I missing?

Reg
  • 10,717
  • 6
  • 37
  • 54
Rachel
  • 65
  • 11

1 Answers1

0

If you wish to only influence a single test with the script, just add on the method.

For example

@Sql({ "classpath:insertMessage.sql" })
@Test
public void myExtremlyBadTestMethodNamingConventionTest() {

If you wish to execute a specified test before all your tests for a single test class. This is where it gets tricky -

First off you might consider adding the @SQL to a @BeforeClass, but this is not possible since the @SQL does not support the @BeforeClass (or @Before).

Next, you might discover the @Commit annotation, which means you can commit all the changes made with a Test. (@Transaction's default behavior is to roll back all changes after the test) Now, you might consider, a @Commit single dummy test to execute before all your other tests?

NO Please do not go down this route.

JUnit does not guarantee the order of the execution of tests. (This is by design) Because your tests should not be relying on the result of other tests.

They should be independent of each other. Why? One advantage is, tests can then be executed in parallel. But also, you would never want a new test to break exciting tests, it is just a maintenance nightmare waiting to happen. (#truestory)


Additionally, In the case, you wish to execute a specified script before all your test, you can add an import.sql file to your test's resources. Spring Boot will automatically check for it before your tests and execute it for you. In other words, it will set up your database in a specific state for your all tests.

Reg
  • 10,717
  • 6
  • 37
  • 54
  • I've just changed it and it is being executed just in each test, but I am having a problem with the primary key of the database, which is autoincremented in each test. How can I stop this? I would like it to be 1 on each test. This is the annotation for the id field: @Id @GeneratedValue(strategy = GenerationType.SEQUENCE) @Column(name = "id", updatable = false, nullable = false) – Rachel Sep 03 '18 at 10:22
  • You should just add it hardcode to the test. ex. insert into test_table(id, text_value) values (1, 'test'); – Reg Sep 03 '18 at 10:40
  • 1
    Thank you, that solves it, but i would like it to be auto assigned. Just a complete rollback. Isn't that possible? – Rachel Sep 03 '18 at 11:03
  • Adding @Transaction to your test should result in a role back after the test is done. – Reg Sep 03 '18 at 11:06
  • Yes it does for the registers, but the id is yet incremented. When a test uses my insertMessage.sql, the database table will always have one register, but the problem is that if I do not use a certain id, it will increase by one on each test. I would like the id to be 1 without having to set it. Thanks for all the answers – Rachel Sep 03 '18 at 14:56
  • @Rachel have a look the following, adding this to you insertMessage.sql should solve the problem. https://stackoverflow.com/questions/10065386/resetting-autoincrement-in-h2 – Reg Sep 04 '18 at 07:02