Spring Boot: @TestConfiguration Not Overriding Bean During Integration Test

As documented in the Detecting Test Configuration section of the Spring Boot reference manual, any beans configured in a top-level class annotated with @TestConfiguration will not be picked up via component scanning. So you have to explicitly register your @TestConfiguration class. You can do that either via @Import(MyTestConfiguration.class) or @ContextConfiguration(classes = MyTestConfiguration.class) on your test … Read more

Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=…) with your test

src/test/java packages and src/main/java packages should match. I had same issue where my src/main/java packages were starting with com.comp.example but src/test/java packages were starting with com.sample.example Because of this spring boot application was not able to pickup the configuration of the application, which it picks up from @SpringBootApplication class. So test case should fall under … Read more

Reuse spring application context across junit test classes

Yes, this is perfectly possible. All you have to do is to use the same locations attribute in your test classes: @ContextConfiguration(locations = “classpath:test-context.xml”) Spring caches application contexts by locations attribute so if the same locations appears for the second time, Spring uses the same context rather than creating a new one. I wrote an … Read more

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

Actually, I think you want this: @DirtiesContext(classMode = ClassMode.BEFORE_EACH_TEST_METHOD) http://docs.spring.io/autorepo/docs/spring-framework/4.2.6.RELEASE/javadoc-api/org/springframework/test/annotation/DirtiesContext.html @DirtiesContext may be used as a class-level and method-level annotation within the same class. In such scenarios, the ApplicationContext will be marked as dirty after any such annotated method as well as after the entire class. If the DirtiesContext.ClassMode is set to AFTER_EACH_TEST_METHOD, the context … Read more

Overriding beans in Integration tests

Since Spring Boot 1.4.x there is an option to use @MockBean annotation to fake Spring beans. Reaction on comment: To keep context in cache do not use @DirtiesContext, but use @ContextConfiguration(name = “contextWithFakeBean”) and it will create separate context, while it will keep default context in cache. Spring will keep both (or how many contexts … Read more

Rollback transaction after @Test

Just add @Transactional annotation on top of your test: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {“testContext.xml”}) @Transactional public class StudentSystemTest { By default Spring will start a new transaction surrounding your test method and @Before/@After callbacks, rolling back at the end. It works by default, it’s enough to have some transaction manager in the context. From: 10.3.5.4 Transaction … Read more

JUnit tests pass in Eclipse but fail in Maven Surefire

I had the same problem (JUnit tests failed in Maven Surefire but passed in Eclipse) and managed to solve it by setting forkMode to always in the maven surefire configuration in pom.xml: <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.12</version> <configuration> <forkMode>always</forkMode> </configuration> </plugin> Surefire parameters: http://maven.apache.org/plugins/maven-surefire-plugin/test-mojo.html Edit (January 2014): As Peter Perháč pointed out, the forkMode parameter is … Read more

How to set environment variable or system property in spring tests?

You can initialize the System property in a static initializer: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = “classpath:whereever/context.xml”) public class TestWarSpringContext { static { System.setProperty(“myproperty”, “foo”); } } The static initializer code will be executed before the spring application context is initialized.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)