Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to populate database only once before @Test methods in spring test?

Tags:

My next problem testing spring service layer with junit4 is: How to call script that populates database only once before all @Test methods: I want to execute this once before all @Tests:

JdbcTestUtils.executeSqlScript(jdbcTemplate(), new FileSystemResource( "src/main/resources/sql/mysql/javahelp-insert.sql"), false); 

I tried to use @PostConstruct on my GenericServiceTest class(extended by test classes). It turned out that @PostConstruct is called every time before every @Test method. Interesting is that even methods annotated @Autowired of GenericServiceTest are called before every @Test method.

I don't want to populate database before every test class but only once at spring-test startup.

How to execute above method only once before all @Test methods with spring testing framework and junit4?

Thank you!

like image 618
Volodymyr Levytskyi Avatar asked Jul 06 '13 18:07

Volodymyr Levytskyi


People also ask

What will execute the method once before the start of all tests?

The @BeforeMethod annotated method will be executed before each test method will run. The @AfterMethod annotated method will run after the execution of each test method. The @BeforeGroups annotated method run only once for a group before the execution of all test cases belonging to that group.

Which annotation causes that method to be run before each test method?

Explanation. Annotating a public static void method with @BeforeClass causes it to be run once before any of the test methods in the class.

Which annotation can be used to setup data for each test method?

Setup. @Before annotation in JUnit is used on a method containing Java code to run before each test case. i.e it runs before each test execution.

What is @test annotation in spring boot?

The @SpringBootTest annotation tells Spring Boot to look for a main configuration class (one with @SpringBootApplication , for instance) and use that to start a Spring application context. You can run this test in your IDE or on the command line (by running ./mvnw test or ./gradlew test ), and it should pass.


2 Answers

Building on Alfredos answer, this is a way to inject database information without calling the embedded database's default script. For instance, this may be useful when you want to automagically build the DDL for you - at least in tests.

@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration({"/applicationContext.xml"}) public class TestClass {      @Autowired     private ApplicationContext ctx;      private JdbcTemplate template;      @Autowired     public void setDataSource(DataSource dataSource) {        template = new JdbcTemplate(dataSource);     }      private static boolean isInitialized = false;      @Before     public void runOnce() {         if (isInitialized) return;         System.out.println("Initializing database");          String script = "classpath:script.sql";          Resource resource = ctx.getResource(script);         JdbcTestUtils.executeSqlScript(template, resource, true);                     isInitialized = true;     } } 

This way, the runOnce() method is called once and only once for the test run. If you make isInitialized an instance field (non-static), the method will be called before every test. This way you can drop/repopulate the tables, if necessary, before every test run.

Note that this is still a rather quick-and-dirty solution and the sensible way to handle the database is in accordance with Ralph's answer.

like image 68
Mike Adler Avatar answered Oct 02 '22 13:10

Mike Adler


Use Springs Embedded Database Support

<jdbc:embedded-database id="dataSource">     <jdbc:script location="classpath:myScript.sql"/>     <jdbc:script location="classpath:otherScript.sql"/> </jdbc:embedded-database> 

or Springs Initialize Database Support

<jdbc:initialize-database data-source="dataSource">     <jdbc:script location="classpath:myScript.sql"/>     <jdbc:script location="classpath:otherScript.sql"/> </jdbc:initialize-database> 

@See http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/jdbc.html#jdbc-embedded-database-support

like image 33
Ralph Avatar answered Oct 02 '22 12:10

Ralph