using different datasource during junit tests

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

using different datasource during junit tests

Matthew Broadhead-2
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?
Reply | Threaded
Open this post in threaded view
|

Re: using different datasource during junit tests

ilgrosso
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/

Reply | Threaded
Open this post in threaded view
|

Re: using different datasource during junit tests

Mark Struberg-3
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ò <[hidden email]>:
>
> 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/
>

Reply | Threaded
Open this post in threaded view
|

Re: using different datasource during junit tests

Matthew Broadhead-2
In reply to this post by ilgrosso
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.
>