dimanche 2 août 2020

@Sql in testing for populating the database in Spring

I am trying to test my spring controllers and for that i need to populate my database. I am using h2 database.

@Sql(scripts="day9_springboot/src/test/resources/article.sql")
public class ArticleControllerIT {
    private static RestTemplate restTemplate;

    //we need to mock some data for the database
    @BeforeAll
    public static void init(){
        SpringApplication.run(Application.class);
        restTemplate = new RestTemplate();
        restTemplate.setUriTemplateHandler(new DefaultUriBuilderFactory("http://localhost:8080/articles"));
}
}

This is how i try to populate it. However the script does not seem to work.

insert into article(id,title,author,publication_date) values(1,'Title1','author1','2010-10-10');
insert into article(id,title,author,publication_date) values(2,'Title1','author1','2010-10-10');
insert into article(id,title,author,publication_date) values(3,'Title1','author1','2010-10-10');

This is the script. Any suggestions on how to make this work ?

Aucun commentaire:

Enregistrer un commentaire