spring-test

How to re-create database before each test in Spring?

可紊 提交于 2019-11-28 05:16:59
My Spring-Boot-Mvc-Web application has the following database configuration in application.properties file: spring.datasource.url=jdbc:h2:tcp://localhost/~/pdk spring.datasource.username=sa spring.datasource.password= spring.datasource.driver-class-name=org.h2.Driver this is the only config I made. No any other configurations made by me anywhere. Nevertheless the Spring and subsystems are automatically recreate database on each web application run. Database is recreated namely on system run while it contains data after application ends. I was not understanding this defaults and was expecting

Override a single @Configuration class on every spring boot @Test

霸气de小男生 提交于 2019-11-28 04:39:44
On my spring boot application I want to override just one of my @Configuration classes with a test configuration (in particular my @EnableAuthorizationServer @Configuration class), on all of my tests. So far after an overview of spring boot testing features and spring integration testing features no straightforward solution has surfaced: @TestConfiguration : It's for extending, not overriding; @ContextConfiguration(classes=…​) and @SpringApplicationConfiguration(classes =…​) lets me override the whole config, not just the one class; An inner @Configuration class inside a @Test is suggested to

How to access Spring context in jUnit tests annotated with @RunWith and @ContextConfiguration?

好久不见. 提交于 2019-11-28 04:37:56
I have following test class @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"/services-test-config.xml"}) public class MySericeTest { @Autowired MyService service; ... } Is it possible to access services-test-config.xml programmatically in one of such methods? Like: ApplicationContext ctx = somehowGetContext(); Daff Since the tests will be instantiated like a Spring bean too, you just need to implement the ApplicationContextAware interface: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"/services-test-config.xml"}) public class

Spring integration tests with profile

会有一股神秘感。 提交于 2019-11-28 04:24:43
In our Spring web applications, we use the Spring bean profiles to differentiate three scenarios: development, integration, and production. We use them to connect to different databases or set other constants. Using Spring bean profiles works very well for the changing the web app environment. The problem we have is when our integration test code needs change for the environment. In these cases, the integration test loads the application context of the web app. This way we don't have to redefine database connections, constants, etc. (applying the DRY principle). We setup our integration tests

How to integration test auto configuration for a custom Spring Boot style starter library?

女生的网名这么多〃 提交于 2019-11-28 03:58:07
问题 I am writing a library to provide some functionality that is shared between multiple different Spring Boot applications that I work with. I would like to do something similar to the auto-configuration that is provided by the many Spring Boot starter libraries exist. That, or some other simple declarative way to integrate my library with the ApplicationContext of the apps using it. I have found some resources explaining how auto configuration works. I can figure out the above problem. However,

Spring beans redefinition in unit test environment

≡放荡痞女 提交于 2019-11-28 03:04:05
we are using Spring for my application purposes, and Spring Testing framework for unit tests. We have a small problem though: the application code loads a Spring application context from a list of locations (xml files) in the classpath. But when we run our unit tests, we want some of the Spring beans to be mocks instead of full-fledged implementation classes. Moreover, for some unit tests we want some beans to become mocks, while for other unit tests we want other beans to become mocks, as we are testing different layers of the application. All this means I want to redefine specific beans of

How to get @WebMvcTest work with OAuth?

人走茶凉 提交于 2019-11-28 01:32:43
问题 I just had hard time to get my controllers unit tests working because, IMO, what is in Spring doc is not enough if using OAuth. In my case, it's Oauth2 with JWT. I tried to use @WithMockUser , @WithUserDetails and even define my own annotation with @WithSecurityContext and a custom UserSecurityContextFactory but always got anonymous user in the UserSecurityContext when security expression where evaluated, whatever I set the test context to in my factory... I propose the solution I came to

@ActiveProfiles in meta annotation and on test class not working

夙愿已清 提交于 2019-11-28 01:20:05
问题 I created a meta annotation @EmbeddedMongoDBUnitTest that activates two profiles to be used in spring based unit tests. The basic setup works: @Documented @Inherited @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @ActiveProfiles({"embeddedMongoDB", "embeddedMongoDBUnitTest"}) public @interface EmbeddedMongoDBUnitTest { } @RunWith(SpringJUnit4ClassRunner.class) @EmbeddedMongoDBUnitTest @ContextConfiguration(...) public class WorkingTest { ... } Now when trying to activate

How to execute @Sql before a @Before method

喜你入骨 提交于 2019-11-27 19:26:43
I'm trying to combine the follow annotations: org.springframework.test.context.jdbc.Sql and org.junit.Before Like the follow code: @Test @Sql(scripts = "dml-parametro.sql") public void testData(){ Iterable<Parametro> parametros = parametroService.findAll(); List<Parametro> parametrosList = Lists.newArrayList(parametros); Assert.assertThat(parametrosList.size(), Is.is(1)); } @Before public void beforeMethod() { JdbcTestUtils.deleteFromTables(jdbcTemplate, "PARAMETRO"); } The code in the method @Before is running after then the script "dml-parametro.sql" in the @Sql annotation. Is it right to do

Spring Test session scope bean using Junit

喜欢而已 提交于 2019-11-27 18:31:26
I have a session scoped bean which holds user data per http session. I would like to write a Junit test case to test the session scoped bean. I would like to write the test case such that it can prove that the beans are getting created per session. Any pointer as how to write such Junit test case? In order to use request and session scopes in unit test you need to: register these scopes in application context create mock session and request register mock request via RequestContextHolder Something like this (assume that you use Spring TestContext to run your tests): abstractSessionTest.xml :