I'm trying to execute a set of JUnit4 tests classes of a Spring Boot Application that consists of multiple web services and has a database configured.
Is convenient that the context is cleared after each test, so I included a @DirtiesContext annotation on each test class, because default behaviour of this annotation is set at AFTER_CLASS.
The problem I'm getting is that the first test class works well but then the following ones always fail.
So I created 2 simple JUnit classes to try to address the problem. Both are equals and test method is empty so always should return success:
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import urlshortener2014.goldenbrown.Application;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
@IntegrationTest("server.port=0")
@DirtiesContext
public class ApplicationTests {
@Value("${local.server.port}")
private int port = 0;
@Test
public void testAlwaysOk() throws Exception {
}
}
I have executed both test classes (ApplicationTests and SameApplicationTests) within eclipse and through "gradle test" and in both cases the second and following tests classes fails, after context being cleaned.
I suspect that the problem is related with the database of the application, that is not being recreated correctly, because output trace points to database related errors multiple times. But I'm not sure how or why this is happening and how to fix it.
Here is a Gist with "gradle test" outputs (normal output, --info output and --debug output): https://gist.github.com/jgbarcos/c8b34c5c292ca1fabc1d
Here is the build.gradle (made to only test the 2 simple classes) that is being used:
eclipse {
project {
name = "UrlShortener2014.goldenBrown"
}
}
dependencies {
compile project(":common")
// Provides java script libraries for static content
compile("org.webjars:bootstrap:3.0.3")
compile("org.webjars:jquery:2.0.3-1")
compile 'org.apache.httpcomponents:httpclient:4.3.6'
compile 'nl.bitwalker:UserAgentUtils:1.2.4'
compile 'org.springframework:spring-context'
compile 'org.springframework:spring-context-support'
compile 'net.sf.ehcache:ehcache:2.7.4'
compile("org.springframework.boot:spring-boot-starter-web:1.2.0.RELEASE")
compile 'org.springframework:spring-test:4.1.4.RELEASE'
testCompile 'junit:junit:4.10'
}
// Used for @DirtiesContext problem
test{
scanForTestClasses = false
// This should get only "ApplicationTests.class" and "SameApplicationTests.class"
include "**/*ApplicationTests.class"
}
Here is the GitHub branch that I created to reproduce the problem withing the team project folder (goldenBrown): https://github.com/jgbarcos/UrlShortener2014/tree/debug_branch/goldenBrown
(Note: project depends on another project called common in another folder "/common" instead of "/goldenBrown", this could be a bit tricky)
Hope this helps to understand the problem, thanks in advance.