Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Demonstrate use of @DataJpaTest when multiple datasources are configured #332

Closed
bemace opened this issue Jan 18, 2018 · 7 comments
Closed

Comments

@bemace
Copy link

bemace commented Jan 18, 2018

I've successfully configured two datasources per your example, but it appears additional configuration is needed to make @DataJpaTest's magic work. Currently my attempts fail with below stack trace, and it's not at all clear to a beginner what needs to change to make this work.

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean]: Factory method 'mainEntityManager' threw exception; nested exception is java.lang.IllegalArgumentException: No visible constructors in class org.springframework.boot.test.autoconfigure.jdbc.TestDatabaseAutoConfiguration$EmbeddedDataSourceFactoryBean
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)
	at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
	... 66 more
Caused by: java.lang.IllegalArgumentException: No visible constructors in class org.springframework.boot.test.autoconfigure.jdbc.TestDatabaseAutoConfiguration$EmbeddedDataSourceFactoryBean
	at org.springframework.cglib.proxy.Enhancer.filterConstructors(Enhancer.java:666)
	at org.springframework.cglib.proxy.Enhancer.generateClass(Enhancer.java:567)
	at org.springframework.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)

@odrotbohm
Copy link
Member

I guess you need to bring this up with the Boot team. I am not sure @DataJpaTest is designed to work with multiple DataSources.

@viniciusffj
Copy link

Hi @bemace
Did you find any solution for your problem?
If yes, can you share it? I am facing a similar issue.

@maciejp
Copy link

maciejp commented Feb 27, 2018

@bemace , @viniciusffj did you solve it? I have the same issue with @jdbcTest .

@gubespam
Copy link

gubespam commented May 4, 2018

Found a possible solution here.

Basically you manually configure the H2 database appropriately instead of letting Spring do it automatically.

Create an application.properties file in “src/test/resources” with the following content

# Let Spring autodetect the different SQL Dialects of each datasource
spring.jpa.database=default
# Generate the DB schema in the In-Memory H2 databases based on the JPA Entities
spring.jpa.generate-ddl=true

# H2 In-Memory Database "foo" (used in tests instead of a real PostgreSQL DB)
spring.datasource.url=jdbc:h2:mem:foo;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver

# H2 In-Memory Database "bar" (used in tests instead of a real PostgreSQL DB)
bar.datasource.url=jdbc:h2:mem:bar;DB_CLOSE_ON_EXIT=FALSE
bar.datasource.username=sa
bar.datasource.password=
bar.datasource.driver-class-name=org.h2.Driver

@membersound
Copy link

membersound commented Jun 18, 2019

I'm having the same problem when multiple datasources are configured (and only using @AutoConfigureTestDatabase annotation instead of @DataJpaTest, which uses it internally).

I could also solve it by overriding the application.properties database config with H2 settings manually (and then not using the @AutoConfigureTestDatabase annotation at all).
But I'd also prefer spring to detect all DS and replace them with H2 embedded automatically. That's what the @AutoConfigureTestDatabase is for!

@membersound
Copy link

As I run into that issue again today, sadly there has not been any action on this issue. Could you maybe have a look at it again, and evaluate if it could be fixed or suggested workarounds could be added?

@odrotbohm
Copy link
Member

odrotbohm commented May 11, 2020

This the issue tracker for the examples, not one for the individual projects. As indicated before, this seems to be related to the way @DataJpaTests are designed to work in Boot, i.e. it might make sense to rather reach out to the Boot team.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants