spring-test

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

筅森魡賤 提交于 2019-11-28 18:20:56
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

Overriding beans in Integration tests

你。 提交于 2019-11-28 17:31:12
For my Spring-Boot app I provide a RestTemplate though a @Configuration file so I can add sensible defaults(ex Timeouts). For my integration tests I would like to mock the RestTemplate as I dont want to connect to external services - I know what responses to expect. I tried providing a different implementation in the integration-test package in the hope that the latter will override the real implementation , but checking the logs it`s the other way around : the real implementation overrides the test one. How can I make sure the one from the TestConfig is the one used? This is my config file :

Are Spring's MockMvc used for unit testing or integration testing?

坚强是说给别人听的谎言 提交于 2019-11-28 16:53:26
问题 Spring has 2 setups for the MockMvc: Standalone setup WebApplicationContext setup In general what kind of testing is MockMvc used for? Unit or Integration? or Both? Am i right in saying that using the standalone setup (running outside the Spring's application context) allows you to write unit tests and with the WebApplicationContext setup you can write integration tests? 回答1: Both forms are actually integration tests since you are testing the integration of your code with the Spring

Configure specific in memory database for testing purpose in Spring

感情迁移 提交于 2019-11-28 16:21:30
How do I configure my Spring Boot application so that when I run unit tests it will use in-memory database such as H2/HSQL but when I run Spring Boot application it will use production database [Postgre/MySQL] ? Spring profiles can be used for this. This would be a specific way: Have environment specific properties files: application.properties : spring.profiles.active: dev application-dev.properties spring.jpa.database: MYSQL spring.jpa.hibernate.ddl-auto: update spring.datasource.url: jdbc:mysql://localhost:3306/dbname spring.datasource.username: username spring.datasource.password: password

How to run JUnit SpringJUnit4ClassRunner with Parametrized?

微笑、不失礼 提交于 2019-11-28 15:57:09
The following code is invalid due to duplicate @RunWith annotation: @RunWith(SpringJUnit4ClassRunner.class) @RunWith(Parameterized.class) @SpringApplicationConfiguration(classes = {ApplicationConfigTest.class}) public class ServiceTest { } But how can I use these two annotations in conjunction? There are at least 2 options to do that: Following http://www.blog.project13.pl/index.php/coding/1077/runwith-junit4-with-both-springjunit4classrunner-and-parameterized/ Your test needs to look something like this: @RunWith(Parameterized.class) @ContextConfiguration(classes = {ApplicationConfigTest

Could not load TestContextBootstrapper - Spring Unit testing

烂漫一生 提交于 2019-11-28 11:54:49
I have to execute Unit test on one of my Dao classes using Spring. Here is my unit test: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"classpath:app-config.xml"}) @ActiveProfiles("local") public class HouseDaoTest { @Autowired HouseDataDao houseDataDao; @Test public void saveTest(){ HouseData data = new HouseData(); Address add = new Address(); // Truncating for sake of simplicity houseDataDao.save(data); } } And My bean configuration files: app-config.xml: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi=

Java annotations - code simplifications

半世苍凉 提交于 2019-11-28 09:24:40
问题 I am looking for a way to simplify the following code. @WebAppConfiguration @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = { // My configuration classes }) public class MyServiceTest { @Autowired private MyService service; @Test public void myTest() { Assert.assertTrue(service != null); } } I have many configuration classes and I don't want to put them into each test class. So I got the idea to create my own annotation: @WebAppConfiguration @RunWith

Reload or refresh a Spring application context inside a test method?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-28 08:30:27
I need to change the Spring profiles that are active in my applicationContext within a single method of my test class, and to do so I need to run one line of code before refreshing the contest because I am using a ProfileResolver. I have tried the following: @WebAppConfiguration @ContextConfiguration(locations = {"/web/WEB-INF/spring.xml"}) @ActiveProfiles(resolver = BaseActiveProfilesResolverTest.class) public class ControllerTest extends AbstractTestNGSpringContextTests { @Test public void test() throws Exception { codeToSetActiveProfiles(...); ((ConfigurableApplicationContext)this

How to flush data into db inside active spring transaction?

主宰稳场 提交于 2019-11-28 07:38:17
I want to test hibernate session's save() method using spring testing framework. @Test method is : @Test @Transactional public void testSave() { User expected = createUser(); getGenericDao().currentSession().save(expected); User actual = getUser(generatedId); assertUsersEqual(expected,actual); } I want to flush user into database. I want my user to be in database after this method getGenericDao().currentSession().save(expected); Then I want to go to database using spring data framework and fetch this saved user by next line: User actual = getUser(generatedId); I tried to use hibernate flush

Fails to load Spring application context in tests with SpringJUnit4ClassRunner

╄→尐↘猪︶ㄣ 提交于 2019-11-28 06:09:27
问题 I can't figure out why Spring can't load the application context in a test class defined as follows: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"/springMVC-config/mainController-servlet.xml"}) public class DiagnosticsControllerTest { @Mock DiagnosticsService diagnosticsService; private DiagnosticsController diagnosticsController; private MockMvc mockMvc; @Before public void setUp() { MockitoAnnotations.initMocks(this); diagnosticsController = new