spring-test

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

白昼怎懂夜的黑 提交于 2019-11-27 04:17:17
问题 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

JUnit tests pass in Eclipse but fail in Maven Surefire

让人想犯罪 __ 提交于 2019-11-27 02:45:32
I have written some JUnit tests using JUnit 4 and spring-test libraries. When I run the tests inside Eclipse then run fine and pass. But when I run them using Maven (during the build process), they fail giving a spring related error. I am not sure what is causing the problem, JUnit, Surefire or Spring. Here is my test code, spring configuration and the exception that I get from Maven: PersonServiceTest.java package com.xyz.person.test; import static com.xyz.person.util.FjUtil.toFjList; import static junit.framework.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import java

Disable security for unit tests with spring boot

青春壹個敷衍的年華 提交于 2019-11-27 01:57:12
I'm trying to create a simple spring boot web project with security. I can launch the application fine and the security is working fine. However, I have some components that I want to test without security (or test at all -- I cant get the test working at all). I get an exception indicating that it can't find an ObjectPostProcessor and thus can't bring up the container. Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.security.config.annotation.ObjectPostProcessor] found for dependency 14:01:50.937 [main] ERROR o.s.boot

How to flush data into db inside active spring transaction?

a 夏天 提交于 2019-11-27 01:38:29
问题 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

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

泪湿孤枕 提交于 2019-11-27 00:41:37
问题 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

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

五迷三道 提交于 2019-11-27 00:33:18
问题 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(); 回答1: Since the tests will be instantiated like a Spring bean too, you just need to implement the ApplicationContextAware interface: @RunWith

Spring integration tests with profile

那年仲夏 提交于 2019-11-27 00:23:29
问题 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

Failed to load ApplicationContext caused by ArrayIndexOutOfBoundsException in ClassReader

大憨熊 提交于 2019-11-26 23:29:32
问题 When I am running junit test class the below exception arises? How can i resolve this? Failed to load ApplicationContext java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:157) at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109) at org.springframework.test.context.support

Spring Test session scope bean using Junit

时间秒杀一切 提交于 2019-11-26 22:41:57
问题 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? 回答1: 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

Rollback transaction after @Test

耗尽温柔 提交于 2019-11-26 21:55:02
First of all, I've found a lot of threads on StackOverflow about this, but none of them really helped me, so sorry to ask possibly duplicate question. I'm running JUnit tests using spring-test, my code looks like this @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {}) public class StudentSystemTest { @Autowired private StudentSystem studentSystem; @Before public void initTest() { // set up the database, create basic structure for testing } @Test public void test1() { } ... } My problem is that I want my tests to NOT influence other tests. So I'd like to create