0

I am trying to work with p6spy in Spring Boot tests. I have a test class annotated with

@RunWith(SpringRunner.class)
@SpringBootTest

My gradle.build looks like this

dependencies {
 compile('org.springframework.boot:spring-boot-starter-data-jpa')
 runtime('com.h2database:h2')

 testCompile 'p6spy:p6spy:3.0.0'
 testCompile('org.springframework.boot:spring-boot-starter-test')
}

As for the application itself (which runs fine), I added the new datasource to the test-application-context.

spring:
application:
    name: persistence
datasource:
    url: jdbc:p6spy:h2:mem:persistence;DB_CLOSE_ON_EXIT=FALSE
    username: sa
    password:
    driver-class-name: com.p6spy.engine.spy.P6SpyDriver
jpa:
database: H2

But when I run my test I get this error

java.lang.IllegalStateException: Cannot load driver class: com.p6spy.engine.spy.P6SpyDriver

To me this looks like my dependencies are not loaded. At first, I was using the @DataJpaTest annotation, but this one ignored even my new test-application-context.

Any help appreciated.

EDIT: I got it working bei adding the p6spy dependency manually to the test using IntelliJ. Now I'm sure that my classpath is wrong, but I don't know how to fix it to make it work in Gradle.

sorencito
  • 2,517
  • 20
  • 21
  • Please compare your project with https://github.com/p6spy/p6spy-it-spring-boot This sample works as expected. – Simon Martinelli Oct 09 '17 at 19:22
  • @simas_ch Thanks for the hint. I actually used this helper, but it doesn't use tests with p6spy (or maybe I'm blind? ) – sorencito Oct 09 '17 at 19:33
  • Why don't you use testCompile('com.h2database:h2') instead of runtime? – Justinas Jakavonis Oct 10 '17 at 07:54
  • Try these properties: @RunWith(SpringRunner.class) @DataJpaTest @AutoConfigureTestDatabase(replace = Replace.ANY) @SpringBootTest(classes = YourApplication.class) – Justinas Jakavonis Oct 10 '17 at 07:57
  • @Justas Your idea to use the application context of my application inside the SpringBootTest is cool, I never thought about it. Unfortunately, it does not work ootb. I tried all your suggestions, but in particular '@DataJpaTest' seems to override my custom datasource. I'll play with '@SpringBootTest' now and see if I can make it work. – sorencito Oct 10 '17 at 08:12
  • I use this config with test applications.properties which contains in-memory config like yours and it works well. What error do you get? – Justinas Jakavonis Oct 10 '17 at 08:29
  • Do you test endpoints or just database layer? – Justinas Jakavonis Oct 10 '17 at 08:31
  • @Justas I only test the database. Actually, I want to replace the driver in order to use p6spy. This is what I have the application.yml for - and I think it is behaving correctly. I also suppose that if I changed some H2 properties it would also work. The problem seems to be loading the dependencies, which gradle or Springboot doesn't seem to get right. – sorencito Oct 10 '17 at 08:41
  • Does it work with H2 without P6spy? – Justinas Jakavonis Oct 10 '17 at 08:45
  • "java.lang.IllegalStateException: Cannot load driver class" - did you installed the lib sucessfully? – Justinas Jakavonis Oct 10 '17 at 08:53
  • @Justas Yes, it works without p6spy. I think what you mean with installation is exactly my problem ;-) I try to get it onto the classpath for my test by using Gradle's dependency management (see code snippet) – sorencito Oct 10 '17 at 09:08
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/156336/discussion-between-sorencito-and-justas). – sorencito Oct 10 '17 at 09:15
  • try to set the scope to compile instead of testCompile of p6spy – Simon Martinelli Oct 10 '17 at 14:37
  • @simas_ch Did that already. Actually I tried nearly all possible permutations of scopes. Doesn't help... – sorencito Oct 10 '17 at 15:04
  • @Justas, simas_ch, Thank you guys for bothering - it's a bug in IntelliJ. I don't know how I managed not to notice (I used both console and IDE), but it works now. Well, it works on the console, not in IntelliJ - what annoys me most is not that I spent the entire day on this, but that the solution is my original post! – sorencito Oct 10 '17 at 18:42

1 Answers1

0

The problem is located in my version of IntelliJ. I will file a bug report.

If anyone should have this problem, I added the missing dependencies manually in the project settings. Then it works also from the IDE.

sorencito
  • 2,517
  • 20
  • 21