You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Ernesto Arroyo <ea...@yahoo.es> on 2011/09/04 22:44:05 UTC

How to inject a mock into a tapestry service?

This should be easy, but I can't figure how to do this.

I have a service that has a dependency to a DAO, that I manage using injection:

@Inject
private IOrganizationDAO orgDao;

As far as i know, tapestry uses the construction mechanism to injection, so I have not a setter, tapestry does this through the constructor of the service.

But now I want to do a unit test of this service, so I need to inject manually a mock of the DAO, and I don't know how to do this.
Maybe I have to create a setter for the dependency, but I can't find any example or documentation about this.

private static IPacoService psrv;


@Test(enabled = true)
public void testGetAllOrganizations() {
// psrv.orgDao = new mockDAO();  ===> ????
psrv.getAllOrganizations();
// assert goes here
}

Re: How to inject a mock into a tapestry service?

Posted by Ernesto Arroyo <ea...@yahoo.es>.
In case someone else is in this same problem, thanks to Igor for the
solution:

PD: the test is still without content, only the scheleton, but it works!
I have a lot of work now, I do not manage very well with mocks :o)

package xxxxxxxx.biz;

import static org.easymock.EasyMock.createMock;
import java.util.List;
import org.apache.tapestry5.ioc.test.TestUtils;
import org.testng.AssertJUnit;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import xxxxxxx.dao.IOrganizationDAO;
import xxxxxxx.entities.Organization;


public class PacoServiceTest {

	private static IPacoService psrv;
	private IOrganizationDAO daomock;
	
	@BeforeClass
	public void setUp() {
		psrv = new PacoService();
		daomock = createMock(IOrganizationDAO.class); 
		psrv = TestUtils.create(PacoService.class, "orgDao", daomock); 
	}

	@Test(enabled = true)
	public void testGetAllOrganizations() {
			List<Organization> result = psrv.getAllOrganizations();
			AssertJUnit.assertNull(result); 
		}

	@Test(enabled = false)
	public void testOrganizationPersist() {
		AssertJUnit.fail("Not yet implemented");
	}

	@Test(enabled = false)
	public void testDeleteOrganization() {
		AssertJUnit.fail("Not yet implemented");
	}

}

--
View this message in context: http://tapestry.1045711.n5.nabble.com/How-to-inject-a-mock-into-a-tapestry-service-tp4768327p4771774.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: How to inject a mock into a tapestry service?

Posted by Igor Drobiazko <ig...@gmail.com>.
Check out the TestUtils [1] class. It used to create services for tests.
Here is an example:

IOrganizationDAO da0 = newMock(IOrganizationDAO.class);
IPacoService  service = TestUtils.create(IPacoService.class, "orgDao",
dao);

[1]
https://builds.apache.org/job/tapestry-trunk-freestyle/javadoc/org/apache/tapestry5/ioc/test/TestUtils.html

On Mon, Sep 5, 2011 at 9:09 AM, Ernesto Arroyo <ea...@yahoo.es> wrote:

> Thanks,
>
> Yes it should be easy in "plain Java) but,... As far as I do not provide a
> constructor, Java will provide a default one for me.  Because the default
> constructor will not create the dependency because is a private variable of
> the class I suposse Tapestry is doing an enhancement in the class using
> annotations for that (the class is enhanced in the same way EJBs are
> enhanced I think) to create the needed constructors for injection.
>
> So, If I provide a constructor maybe I "break" the way tapestry is doing
> this enhancement (for example, in Hibernate you can have problems if you
> provide a constructor in some ways)
>
> Of course I am going to do a test and check this, but this are ideas I have
> in mind now.
>
> Thanks a lot.
>
> PD:
> My example code was wrong,
>
> psrv.setOrgDao(mockDao); // orgDao is private in the service, we need a
> setter
>
>
>
> --
> View this message in context:
> http://tapestry.1045711.n5.nabble.com/How-to-inject-a-mock-into-a-tapestry-service-tp4768327p4769454.html
> Sent from the Tapestry - User mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>


-- 
Best regards,

Igor Drobiazko
http://tapestry5.de

Re: How to inject a mock into a tapestry service?

Posted by Ernesto Arroyo <ea...@yahoo.es>.
Thanks,

Yes it should be easy in "plain Java) but,... As far as I do not provide a
constructor, Java will provide a default one for me.  Because the default
constructor will not create the dependency because is a private variable of
the class I suposse Tapestry is doing an enhancement in the class using
annotations for that (the class is enhanced in the same way EJBs are
enhanced I think) to create the needed constructors for injection.

So, If I provide a constructor maybe I "break" the way tapestry is doing
this enhancement (for example, in Hibernate you can have problems if you
provide a constructor in some ways)

Of course I am going to do a test and check this, but this are ideas I have
in mind now. 

Thanks a lot.

PD:
My example code was wrong, 

psrv.setOrgDao(mockDao); // orgDao is private in the service, we need a
setter



--
View this message in context: http://tapestry.1045711.n5.nabble.com/How-to-inject-a-mock-into-a-tapestry-service-tp4768327p4769454.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: How to inject a mock into a tapestry service?

Posted by Steve Eynon <st...@alienfactory.co.uk>.
If you're doing a straight forward unit test, with all class
dependencies mocked out, then this becomes a plain Java question...

You either create a setter for IOrganizationDAO for testing purposes
or start using constructor injection, and pass your mock in when you
construct the service:

public MyService(IOrganizationDAO orgDao) {
    this.orgDao = orgDao;
}

With the latter Tapestry will automatically find, resolve and use any
constructors defined in your service, defaulting to the one with the
most params.

http://tapestry.apache.org/injection-in-detail.html

Steve.

On 5 September 2011 04:44, Ernesto Arroyo <ea...@yahoo.es> wrote:
> This should be easy, but I can't figure how to do this.
>
> I have a service that has a dependency to a DAO, that I manage using injection:
>
> @Inject
> private IOrganizationDAO orgDao;
>
> As far as i know, tapestry uses the construction mechanism to injection, so I have not a setter, tapestry does this through the constructor of the service.
>
> But now I want to do a unit test of this service, so I need to inject manually a mock of the DAO, and I don't know how to do this.
> Maybe I have to create a setter for the dependency, but I can't find any example or documentation about this.
>
> private static IPacoService psrv;
>
>
> @Test(enabled = true)
> public void testGetAllOrganizations() {
> // psrv.orgDao = new mockDAO();  ===> ????
> psrv.getAllOrganizations();
> // assert goes here
> }
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org