You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Kiss Izolda <iz...@freemail.hu> on 2010/12/06 14:34:46 UTC
TDD with Tapestry 5.2 how to mock Logger and Messages
Dear Tapestry Users,
we started to use TDD (Test Driven Development) with Tapestry 5.2
in order to observe the rules of TDD we need to mock the following services as well:-org.slf4j.Logger
-org.apache.tapestry5.ioc.Messages
the @ForComponents annotation had no effect in case of this 2 services
is there any way to mock them, to check their invocations?
thanks in advance
Izolda ps: some part of the code @RunWith(JMock.class)
public class MyPageTest extends AbstractTest {
@ForComponents
private Logger logger;
@ForComponents
private Messages messages;
private Mockery context = new JUnit4Mockery();
public void doSetUp() {
logger = context.mock(Logger.class);
messages = context.mock(Messages.class);
}
@Test
public void testUserLoggedIn() {
// TEST FIXTURE
context.checking(new Expectations() {
{
oneOf(logger).debug("user logged in");
oneOf(messages).format("welcome", "Black Smith");
will(returnValue("Hello Black Smith!"));
}
});
// Excercise sut
Document document = tester.renderPage("MyPage");
// TEST ASSERT
assertNotNull(document);
assertSpan("spamWelcome", "Hello Black Smith!", document);
} java.lang.AssertionError: not all expectations were satisfied
expectations:
expected once, never invoked: logger.debug("user logged in"); returns a default value
expected once, never invoked: messages.format("welcome", ["Black Smith"]); returns "Hello Black Smith!"
Re: TDD with Tapestry 5.2 how to mock Logger and Messages
Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
On Mon, 06 Dec 2010 11:34:46 -0200, Kiss Izolda <iz...@freemail.hu> wrote:
> Dear Tapestry Users,
Hi!
> we started to use TDD (Test Driven Development) with Tapestry 5.2
> in order to observe the rules of TDD we need to mock the following
> services as well:-org.slf4j.Logger
> -org.apache.tapestry5.ioc.Messages
> the @ForComponents annotation had no effect in case of this 2 services
They're not services. Their injection is done in a different way from
services. Unfortunately, I can't help you beyond that.
--
Thiago H. de Paula Figueiredo
Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,
and instructor
Owner, Ars Machina Tecnologia da Informação Ltda.
http://www.arsmachina.com.br
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org
Re: TDD with Tapestry 5.2 how to mock Logger and Messages
Posted by Inge Solvoll <in...@gmail.com>.
You can mock services with Testify.
On Mon, Dec 6, 2010 at 7:46 PM, Igor Drobiazko <ig...@gmail.com>wrote:
> I'm wondering why you want to define any expectations on Logger. However,
> when using the PageTester you can't mock services as they are retrieved
> form
> the registry which is created from your modules. You could try to provide
> an
> additional "TestModule" which is used only in that particular test. In this
> module you can try to decorate, advise or override your services for
> testability. But again, I see logs as a noise which I don't want to test
> against.
>
> 2010/12/6 Kiss Izolda <iz...@freemail.hu>
>
> > Hi Igor,
> >
> > We are using Tapestry's built-in testing facilities and have rendered the
> > page (tester.renderPage("MyPage"))
> > We don't necessary need to mock the org.slf4j.Logger, what we need is to
> > unit test whether the page calls the org.slf4j.Logger or not.
> >
> > thank you,
> >
> > Izolda
> >
> > ---------------------------------------------------------------------
> > 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: TDD with Tapestry 5.2 how to mock Logger and Messages
Posted by Igor Drobiazko <ig...@gmail.com>.
I'm wondering why you want to define any expectations on Logger. However,
when using the PageTester you can't mock services as they are retrieved form
the registry which is created from your modules. You could try to provide an
additional "TestModule" which is used only in that particular test. In this
module you can try to decorate, advise or override your services for
testability. But again, I see logs as a noise which I don't want to test
against.
2010/12/6 Kiss Izolda <iz...@freemail.hu>
> Hi Igor,
>
> We are using Tapestry's built-in testing facilities and have rendered the
> page (tester.renderPage("MyPage"))
> We don't necessary need to mock the org.slf4j.Logger, what we need is to
> unit test whether the page calls the org.slf4j.Logger or not.
>
> thank you,
>
> Izolda
>
> ---------------------------------------------------------------------
> 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: TDD with Tapestry 5.2 how to mock Logger and Messages
Posted by Kiss Izolda <iz...@freemail.hu>.
Hi Igor,
We are using Tapestry's built-in testing facilities and have rendered the page (tester.renderPage("MyPage"))
We don't necessary need to mock the org.slf4j.Logger, what we need is to unit test whether the page calls the org.slf4j.Logger or not.
thank you,
Izolda
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org
Re: TDD with Tapestry 5.2 how to mock Logger and Messages
Posted by Igor Drobiazko <ig...@gmail.com>.
You can use Tapestry's built-in testing facilities. Don't mock the service,
but just render you pages. Please read here:
http://tapestry.apache.org/unit-testing-pages-or-components.html
2010/12/6 Kiss Izolda <iz...@freemail.hu>
> Dear Tapestry Users,
>
> we started to use TDD (Test Driven Development) with Tapestry 5.2
> in order to observe the rules of TDD we need to mock the following services
> as well:-org.slf4j.Logger
> -org.apache.tapestry5.ioc.Messages
> the @ForComponents annotation had no effect in case of this 2 services
> is there any way to mock them, to check their invocations?
> thanks in advance
> Izolda ps: some part of the code @RunWith(JMock.class)
> public class MyPageTest extends AbstractTest {
> @ForComponents
> private Logger logger;
> @ForComponents
> private Messages messages;
> private Mockery context = new JUnit4Mockery();
>
> public void doSetUp() {
> logger = context.mock(Logger.class);
> messages = context.mock(Messages.class);
> }
>
> @Test
> public void testUserLoggedIn() {
> // TEST FIXTURE
> context.checking(new Expectations() {
> {
> oneOf(logger).debug("user logged in");
> oneOf(messages).format("welcome", "Black Smith");
> will(returnValue("Hello Black Smith!"));
> }
> });
> // Excercise sut
> Document document = tester.renderPage("MyPage");
> // TEST ASSERT
> assertNotNull(document);
> assertSpan("spamWelcome", "Hello Black Smith!", document);
> } java.lang.AssertionError: not all expectations were satisfied
> expectations:
> expected once, never invoked: logger.debug("user logged in"); returns a
> default value
> expected once, never invoked: messages.format("welcome", ["Black Smith"]);
> returns "Hello Black Smith!"
--
Best regards,
Igor Drobiazko
http://tapestry5.de