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?
Have a look at the documentation of Spring boot to run your test using Spring Test Framework.
At least you need to use @SpringBootTest
in order to start your application. There are several annotations and helper/mocks classes to ease your tests depending on what part of Spring you want to test (MVC, data, etc...)
@SpringBootTest
@Sql(scripts="day9_springboot/src/test/resources/article.sql")
public class ArticleControllerIT {
@RestTemplate restTemplate;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.