You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Matthew Broadhead <ma...@nbmlaw.co.uk.INVALID> on 2019/02/13 14:04:43 UTC

using different datasource during junit tests

i am trying to test some business logic that depends heavily on the 
results of calls to entitymanager.  i would like to keep the 
persistence.xml exactly as it is in the main project but basically load 
in a different database underneath.  is there a way to initialise the 
entitymanager and point it to a development database for the duration of 
the tests?

Re: using different datasource during junit tests

Posted by Mark Struberg <st...@yahoo.de.INVALID>.
You can provide a map to javax.persistence.Persistence#createEntityManagerFactory(String pu, Map properties);
That way you can define the Driver, jdbc user + pwc, url, etc

That way you don't need anything in your persistence.xml.

If you use CDI you can create a producer for it or use the one from DeltaSpike.
Or you use TomEE where you can do the same. You can pass a property map to DeltaSpike CdiCtrl boot(Map):
https://github.com/apache/deltaspike/blob/master/deltaspike/cdictrl/api/src/main/java/org/apache/deltaspike/cdise/api/CdiContainer.java#L58

LieGrue,
strub

> Am 13.02.2019 um 17:10 schrieb Francesco Chicchiriccò <il...@apache.org>:
> 
> On 13/02/19 15:04, Matthew Broadhead wrote:
>> i am trying to test some business logic that depends heavily on the results of calls to entitymanager.  i would like to keep the persistence.xml exactly as it is in the main project but basically load in a different database underneath.  is there a way to initialise the entitymanager and point it to a development database for the duration of the tests?
> 
> Hi Matthew,
> I normally do as you require above, but with some Spring ORM support, not sure it fits with your scenario; if so, I can point you to some working samples.
> 
> Regards.
> 
> -- 
> Francesco Chicchiriccò
> 
> Tirasa - Open Source Excellence
> http://www.tirasa.net/
> 
> Member at The Apache Software Foundation
> Syncope, Cocoon, Olingo, CXF, OpenJPA, PonyMail
> http://home.apache.org/~ilgrosso/
> 


Re: using different datasource during junit tests

Posted by Matthew Broadhead <ma...@nbmlaw.co.uk.INVALID>.
thanks Francesco, i don't use spring currently so it may not work in my case

i often consider diving into spring when i see
a) how many positions require it in the job market
b) how many web tutorials mention it

On 13/02/2019 17:10, Francesco Chicchiriccò wrote:
> On 13/02/19 15:04, Matthew Broadhead wrote:
>> i am trying to test some business logic that depends heavily on the 
>> results of calls to entitymanager. i would like to keep the 
>> persistence.xml exactly as it is in the main project but basically 
>> load in a different database underneath.  is there a way to 
>> initialise the entitymanager and point it to a development database 
>> for the duration of the tests?
>
> Hi Matthew,
> I normally do as you require above, but with some Spring ORM support, 
> not sure it fits with your scenario; if so, I can point you to some 
> working samples.
>
> Regards.
>


Re: using different datasource during junit tests

Posted by Francesco Chicchiriccò <il...@apache.org>.
On 13/02/19 15:04, Matthew Broadhead wrote:
> i am trying to test some business logic that depends heavily on the 
> results of calls to entitymanager.  i would like to keep the 
> persistence.xml exactly as it is in the main project but basically 
> load in a different database underneath.  is there a way to initialise 
> the entitymanager and point it to a development database for the 
> duration of the tests?

Hi Matthew,
I normally do as you require above, but with some Spring ORM support, 
not sure it fits with your scenario; if so, I can point you to some 
working samples.

Regards.

-- 
Francesco Chicchiriccò

Tirasa - Open Source Excellence
http://www.tirasa.net/

Member at The Apache Software Foundation
Syncope, Cocoon, Olingo, CXF, OpenJPA, PonyMail
http://home.apache.org/~ilgrosso/