custom Matcher necessary?

View: New views
12 Messages — Rating Filter:   Alert me  

custom Matcher necessary?

by Jeroen Verhagen :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi all,

I'm using jMock 2.4.0 to junit test my code.

In the following test example:

  one(message).setContent(with(aNonNull(StringBuffer.class)));

Is it somehow possible to capture and inspect the value that is passed
to the method setContent()? Or do I have to write a custom Matcher to
do that?

--

regards,

Jeroen

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: custom Matcher necessary?

by Steve Freeman-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

First, why do you want to do that?

Second, if you want to capture an argument, use an action not a matcher.

S.

On 4 Jun 2008, at 10:27, Jeroen Verhagen wrote:

> Hi all,
>
> I'm using jMock 2.4.0 to junit test my code.
>
> In the following test example:
>
>  one(message).setContent(with(aNonNull(StringBuffer.class)));
>
> Is it somehow possible to capture and inspect the value that is passed
> to the method setContent()? Or do I have to write a custom Matcher to
> do that?



Steve Freeman
Winner of the Agile Alliance Gordon Pask award 2006

http://www.m3p.co.uk

M3P Limited.
Registered office. 2 Church Street, Burnham, Bucks, SL1 7HZ.
Company registered in England & Wales. Number 03689627



---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: custom Matcher necessary?

by Jeroen Verhagen :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Steve,

On Wed, Jun 4, 2008 at 11:44 AM, Steve Freeman <steve@...> wrote:
> First, why do you want to do that?

I want to check the content and assert that it meets my expectations.

> Second, if you want to capture an argument, use an action not a matcher.

Thanks, I will look into actions. I hadn't considered them, but then
I'm new to testing using jMock.

--

regards,

Jeroen

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: custom Matcher necessary?

by Steve Freeman-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 4 Jun 2008, at 12:34, Jeroen Verhagen wrote:
> On Wed, Jun 4, 2008 at 11:44 AM, Steve Freeman <steve@...>  
> wrote:
>> First, why do you want to do that?
>
> I want to check the content and assert that it meets my expectations.

 > one(message).setContent(with(aNonNull(StringBuffer.class)));

This doesn't feel like the right thing to do. Can you give us more  
context about the test?

>> Second, if you want to capture an argument, use an action not a  
>> matcher.
>
> Thanks, I will look into actions. I hadn't considered them, but then
> I'm new to testing using jMock.

In that case, see above. You shouldn't need to write actions often.

S.


Steve Freeman
Winner of the Agile Alliance Gordon Pask award 2006

http://www.m3p.co.uk

M3P Limited.
Registered office. 2 Church Street, Burnham, Bucks, SL1 7HZ.
Company registered in England & Wales. Number 03689627



---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: custom Matcher necessary?

by Geoffrey Wiseman :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Wed, Jun 4, 2008 at 7:34 AM, Jeroen Verhagen <jeroenverhagen@...> wrote:
Hi Steve,

On Wed, Jun 4, 2008 at 11:44 AM, Steve Freeman <steve@...> wrote:
> First, why do you want to do that?

I want to check the content and assert that it meets my expectations.

In a way that you couldn't accomplish with something like an equality matcher?  There's also a HasToString matcher, which might work well for a StringBuffer.

  - Geoffrey
--
Geoffrey Wiseman

RE: custom Matcher necessary?

by Uladzimir Liashkevich :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

public abstract class StoreMatcher<T> extends BaseMatcher<T> {
    protected abstract void store(T value);

    @SuppressWarnings({ "unchecked" })
    public boolean matches(Object value) {
        store((T)value);
        return true;
    }

    public void describeTo(Description description) {
        description.appendText("stores value");
    }
}


-----Original Message-----
From: Jeroen Verhagen [mailto:jeroenverhagen@...]
Sent: Wednesday, June 04, 2008 12:28 PM
To: user@...
Subject: [jmock-user] custom Matcher necessary?

Hi all,

I'm using jMock 2.4.0 to junit test my code.

In the following test example:

  one(message).setContent(with(aNonNull(StringBuffer.class)));

Is it somehow possible to capture and inspect the value that is passed
to the method setContent()? Or do I have to write a custom Matcher to
do that?

--

regards,

Jeroen

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: custom Matcher necessary?

by Steve Freeman-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I suggest you don't use matchers for this kind of activity, they  
should be stateless and there are no guarantees as to when or how  
often they will be called. Use an Action if you really want to do this.

S.

On 5 Jun 2008, at 08:14, Uladzimir Liashkevich wrote:

> public abstract class StoreMatcher<T> extends BaseMatcher<T> {
>    protected abstract void store(T value);
>
>    @SuppressWarnings({ "unchecked" })
>    public boolean matches(Object value) {
>        store((T)value);
>        return true;
>    }
>
>    public void describeTo(Description description) {
>        description.appendText("stores value");
>    }
> }
>
>
> -----Original Message-----
> From: Jeroen Verhagen [mailto:jeroenverhagen@...]
> Sent: Wednesday, June 04, 2008 12:28 PM
> To: user@...
> Subject: [jmock-user] custom Matcher necessary?
>
> Hi all,
>
> I'm using jMock 2.4.0 to junit test my code.
>
> In the following test example:
>
>  one(message).setContent(with(aNonNull(StringBuffer.class)));
>
> Is it somehow possible to capture and inspect the value that is passed
> to the method setContent()? Or do I have to write a custom Matcher to
> do that?
>
> --
>
> regards,
>
> Jeroen
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>

Steve Freeman
Winner of the Agile Alliance Gordon Pask award 2006

http://www.m3p.co.uk

M3P Limited.
Registered office. 2 Church Street, Burnham, Bucks, SL1 7HZ.
Company registered in England & Wales. Number 03689627



---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



RE: custom Matcher necessary?

by Uladzimir Liashkevich :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

You are right. Using CustomAction is more preferable and side-effect
free (actually I used this approach most of the time).
StoreMatcher should be used with care and it depends on JMock
implementation.


-----Original Message-----
From: Steve Freeman [mailto:steve@...]
Sent: Thursday, June 05, 2008 11:51 AM
To: user@...
Subject: Re: [jmock-user] custom Matcher necessary?

I suggest you don't use matchers for this kind of activity, they  
should be stateless and there are no guarantees as to when or how  
often they will be called. Use an Action if you really want to do this.

S.

On 5 Jun 2008, at 08:14, Uladzimir Liashkevich wrote:

> public abstract class StoreMatcher<T> extends BaseMatcher<T> {
>    protected abstract void store(T value);
>
>    @SuppressWarnings({ "unchecked" })
>    public boolean matches(Object value) {
>        store((T)value);
>        return true;
>    }
>
>    public void describeTo(Description description) {
>        description.appendText("stores value");
>    }
> }
>
>
> -----Original Message-----
> From: Jeroen Verhagen [mailto:jeroenverhagen@...]
> Sent: Wednesday, June 04, 2008 12:28 PM
> To: user@...
> Subject: [jmock-user] custom Matcher necessary?
>
> Hi all,
>
> I'm using jMock 2.4.0 to junit test my code.
>
> In the following test example:
>
>  one(message).setContent(with(aNonNull(StringBuffer.class)));
>
> Is it somehow possible to capture and inspect the value that is passed
> to the method setContent()? Or do I have to write a custom Matcher to
> do that?
>
> --
>
> regards,
>
> Jeroen
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>

Steve Freeman
Winner of the Agile Alliance Gordon Pask award 2006

http://www.m3p.co.uk

M3P Limited.
Registered office. 2 Church Street, Burnham, Bucks, SL1 7HZ.
Company registered in England & Wales. Number 03689627



---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: custom Matcher necessary?

by Jeroen Verhagen :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Wed, Jun 4, 2008 at 4:12 PM, Steve Freeman <steve@...> wrote:
> On 4 Jun 2008, at 12:34, Jeroen Verhagen wrote:
>> one(message).setContent(with(aNonNull(StringBuffer.class)));
>
> This doesn't feel like the right thing to do. Can you give us more context
> about the test?

message is mocked object in the method I want to unit test. The main
point is to test whether the setContent() method is called with a
StringBuffer *not* containing a particular string ('PMXAF') because
the unit tested method is supposed to remove that content.

No ideally I would like not to write custom code and do something like this:

  one(message).setContent(with(not(stringContains("PMXAF"))));

Ofcourse I have to deal with a StringBuffer not a String so I cannot
do this, no can I use hasToString Matcher like Geoffrey suggested?


--

regards,

Jeroen

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: custom Matcher necessary?

by Steve Freeman-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I think what you want is:

one
(message).setContent(with(hasToString(not(containsString("PMFAX")))));

A bit messy, but no new code required.

S.

On 5 Jun 2008, at 16:03, Jeroen Verhagen wrote:

> On Wed, Jun 4, 2008 at 4:12 PM, Steve Freeman <steve@...> wrote:
>> On 4 Jun 2008, at 12:34, Jeroen Verhagen wrote:
>>> one(message).setContent(with(aNonNull(StringBuffer.class)));
>>
>> This doesn't feel like the right thing to do. Can you give us more  
>> context
>> about the test?
>
> message is mocked object in the method I want to unit test. The main
> point is to test whether the setContent() method is called with a
> StringBuffer *not* containing a particular string ('PMXAF') because
> the unit tested method is supposed to remove that content.
>
> No ideally I would like not to write custom code and do something  
> like this:
>
>  one(message).setContent(with(not(stringContains("PMXAF"))));
>
> Ofcourse I have to deal with a StringBuffer not a String so I cannot
> do this, no can I use hasToString Matcher like Geoffrey suggested?



Steve Freeman
Winner of the Agile Alliance Gordon Pask award 2006

http://www.m3p.co.uk

M3P Limited.
Registered office. 2 Church Street, Burnham, Bucks, SL1 7HZ.
Company registered in England & Wales. Number 03689627



---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: custom Matcher necessary?

by Jeroen Verhagen :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Thu, Jun 5, 2008 at 5:23 PM, Steve Freeman <steve@...> wrote:
> I think what you want is:
>
> one
> (message).setContent(with(hasToString(not(containsString("PMFAX")))));

I don't mind, it's get it done. However I get the following error:
"The method containsString(String) is undefined for the type new
Expectations(){}"

I'm using jMock 2.4.0 with Hamcrest version 1.1 libraries. What could
be causing this?


--

Thanks and regards,

Jeroen

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: custom Matcher necessary?

by Steve Freeman-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

It's a static import from Matchers in Hamcrest. you might want to set  
your IDE preferences for preferred static imports.

S.

On 5 Jun 2008, at 16:52, Jeroen Verhagen wrote:

> On Thu, Jun 5, 2008 at 5:23 PM, Steve Freeman <steve@...> wrote:
>> I think what you want is:
>>
>> one
>> (message
>> ).setContent(with(hasToString(not(containsString("PMFAX")))));
>
> I don't mind, it's get it done. However I get the following error:
> "The method containsString(String) is undefined for the type new
> Expectations(){}"
>
> I'm using jMock 2.4.0 with Hamcrest version 1.1 libraries. What could
> be causing this?
>

Steve Freeman
Winner of the Agile Alliance Gordon Pask award 2006

http://www.m3p.co.uk

M3P Limited.
Registered office. 2 Church Street, Burnham, Bucks, SL1 7HZ.
Company registered in England & Wales. Number 03689627



---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email