You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Jake Vang <va...@googlemail.com> on 2010/06/21 06:54:00 UTC

unit test the service layer (aka data access layer)

hi, i've been following this code here at
http://struts.apache.org/2.0.14/docs/struts-2-spring-2-jpa-ajax.html.
what i want to know is how to unit test the service layer (or data
access object layer). in the case of the link, it would be the
PersonServiceImpl class.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: unit test the service layer (aka data access layer)

Posted by Jake Vang <va...@googlemail.com>.
James,

thank you! that second link at the spring site was precisely what i
needed. i have one more question, my spring.xml file refers to a
properties file, how do i annotate to make the reference work?

On Mon, Jun 21, 2010 at 12:31 PM, James Cook <Ja...@wecomm.com> wrote:
> You would still have you build your own EntityManager, like if you were doing it for Tomcat eg.
>
> http://najfiles.blogspot.com/2010/01/jpa-cat-spring.html
>
> Then check out:
>
> http://static.springsource.org/spring/docs/2.5.x/reference/testing.html
>
> Particularly the bit where it talks about extending AbstractTransactionalJUnit4SpringContextTests for your unit test and the like
>
> Cheers
>
> James
>
>
> -----Original Message-----
> From: Jake Vang [mailto:vangjake@googlemail.com]
> Sent: 21 June 2010 17:27
> To: Struts Users Mailing List
> Subject: Re: unit test the service layer (aka data access layer)
>
> james,
>
> would setting up the unit test in that way load up the service class
> as if it would in in the J2EE container?
>
> the reason why i asked is because in the tutorial, as far as i
> understand, they have wired in using Spring a transaction manager.
> that means in my service object, when i call update/delete/create, i
> don't have to get a EntityTransaction and do .begin() and .commit()
> before and after i call the EntityManager's merge/remove/persist
> methods, respectively.
>
> i tried unit testing naively, by constructing an EntityManagerFactory
> from the Persistence class (defining yet another
> META-INF/persistence.xml file), but my CRUD operations (minus read)
> never persists changes to the database (because it is in a J2SE
> environment without a transaction manager? as opposed to being in a
> J2EE environment with a transaction manager? well, that's what some
> sites suggests anyways).
>
>
> On Mon, Jun 21, 2010 at 5:02 AM, James Cook <Ja...@wecomm.com> wrote:
>> Get involved with:
>>
>> Junit 4.4
>> Spring-test libs
>>
>> And started your service test class like such:
>>
>> @RunWith(SpringJUnit4ClassRunner.class)
>> @ContextConfiguration(locations = "classpath:/testConfigXML.xml")
>> public class MyServiceImplTest extends AbstractJUnit4SpringContextTests
>> {
>>
>>    @Autowired(required = true)
>>    private MyService myService = null;
>>
>> testConfigXML.xml will contain your *test* config..
>>
>> Then do the do!
>>
>>
>> -----Original Message-----
>> From: Jake Vang [mailto:vangjake@googlemail.com]
>> Sent: 21 June 2010 05:54
>> To: Struts Users Mailing List
>> Subject: unit test the service layer (aka data access layer)
>>
>> hi, i've been following this code here at
>> http://struts.apache.org/2.0.14/docs/struts-2-spring-2-jpa-ajax.html.
>> what i want to know is how to unit test the service layer (or data
>> access object layer). in the case of the link, it would be the
>> PersonServiceImpl class.
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> For additional commands, e-mail: user-help@struts.apache.org
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> For additional commands, e-mail: user-help@struts.apache.org
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


RE: unit test the service layer (aka data access layer)

Posted by James Cook <Ja...@wecomm.com>.
You would still have you build your own EntityManager, like if you were doing it for Tomcat eg.

http://najfiles.blogspot.com/2010/01/jpa-cat-spring.html

Then check out:

http://static.springsource.org/spring/docs/2.5.x/reference/testing.html

Particularly the bit where it talks about extending AbstractTransactionalJUnit4SpringContextTests for your unit test and the like

Cheers

James


-----Original Message-----
From: Jake Vang [mailto:vangjake@googlemail.com] 
Sent: 21 June 2010 17:27
To: Struts Users Mailing List
Subject: Re: unit test the service layer (aka data access layer)

james,

would setting up the unit test in that way load up the service class
as if it would in in the J2EE container?

the reason why i asked is because in the tutorial, as far as i
understand, they have wired in using Spring a transaction manager.
that means in my service object, when i call update/delete/create, i
don't have to get a EntityTransaction and do .begin() and .commit()
before and after i call the EntityManager's merge/remove/persist
methods, respectively.

i tried unit testing naively, by constructing an EntityManagerFactory
from the Persistence class (defining yet another
META-INF/persistence.xml file), but my CRUD operations (minus read)
never persists changes to the database (because it is in a J2SE
environment without a transaction manager? as opposed to being in a
J2EE environment with a transaction manager? well, that's what some
sites suggests anyways).


On Mon, Jun 21, 2010 at 5:02 AM, James Cook <Ja...@wecomm.com> wrote:
> Get involved with:
>
> Junit 4.4
> Spring-test libs
>
> And started your service test class like such:
>
> @RunWith(SpringJUnit4ClassRunner.class)
> @ContextConfiguration(locations = "classpath:/testConfigXML.xml")
> public class MyServiceImplTest extends AbstractJUnit4SpringContextTests
> {
>
>    @Autowired(required = true)
>    private MyService myService = null;
>
> testConfigXML.xml will contain your *test* config..
>
> Then do the do!
>
>
> -----Original Message-----
> From: Jake Vang [mailto:vangjake@googlemail.com]
> Sent: 21 June 2010 05:54
> To: Struts Users Mailing List
> Subject: unit test the service layer (aka data access layer)
>
> hi, i've been following this code here at
> http://struts.apache.org/2.0.14/docs/struts-2-spring-2-jpa-ajax.html.
> what i want to know is how to unit test the service layer (or data
> access object layer). in the case of the link, it would be the
> PersonServiceImpl class.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: unit test the service layer (aka data access layer)

Posted by Jake Vang <va...@googlemail.com>.
james,

would setting up the unit test in that way load up the service class
as if it would in in the J2EE container?

the reason why i asked is because in the tutorial, as far as i
understand, they have wired in using Spring a transaction manager.
that means in my service object, when i call update/delete/create, i
don't have to get a EntityTransaction and do .begin() and .commit()
before and after i call the EntityManager's merge/remove/persist
methods, respectively.

i tried unit testing naively, by constructing an EntityManagerFactory
from the Persistence class (defining yet another
META-INF/persistence.xml file), but my CRUD operations (minus read)
never persists changes to the database (because it is in a J2SE
environment without a transaction manager? as opposed to being in a
J2EE environment with a transaction manager? well, that's what some
sites suggests anyways).


On Mon, Jun 21, 2010 at 5:02 AM, James Cook <Ja...@wecomm.com> wrote:
> Get involved with:
>
> Junit 4.4
> Spring-test libs
>
> And started your service test class like such:
>
> @RunWith(SpringJUnit4ClassRunner.class)
> @ContextConfiguration(locations = "classpath:/testConfigXML.xml")
> public class MyServiceImplTest extends AbstractJUnit4SpringContextTests
> {
>
>    @Autowired(required = true)
>    private MyService myService = null;
>
> testConfigXML.xml will contain your *test* config..
>
> Then do the do!
>
>
> -----Original Message-----
> From: Jake Vang [mailto:vangjake@googlemail.com]
> Sent: 21 June 2010 05:54
> To: Struts Users Mailing List
> Subject: unit test the service layer (aka data access layer)
>
> hi, i've been following this code here at
> http://struts.apache.org/2.0.14/docs/struts-2-spring-2-jpa-ajax.html.
> what i want to know is how to unit test the service layer (or data
> access object layer). in the case of the link, it would be the
> PersonServiceImpl class.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


RE: unit test the service layer (aka data access layer)

Posted by James Cook <Ja...@wecomm.com>.
Get involved with:

Junit 4.4
Spring-test libs

And started your service test class like such:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:/testConfigXML.xml")
public class MyServiceImplTest extends AbstractJUnit4SpringContextTests
{

    @Autowired(required = true)
    private MyService myService = null;

testConfigXML.xml will contain your *test* config..

Then do the do!


-----Original Message-----
From: Jake Vang [mailto:vangjake@googlemail.com] 
Sent: 21 June 2010 05:54
To: Struts Users Mailing List
Subject: unit test the service layer (aka data access layer)

hi, i've been following this code here at
http://struts.apache.org/2.0.14/docs/struts-2-spring-2-jpa-ajax.html.
what i want to know is how to unit test the service layer (or data
access object layer). in the case of the link, it would be the
PersonServiceImpl class.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org