|
View:
New views
5 Messages
—
Rating Filter:
Alert me
|
|
|
how to test property of argument passed to method?What is the proper way to test a property of an argument passed into a method of a mock, under JMock 2.5? Let's say that my CUT would normally be called like: myObject.myMethod(arg); and I'd like to verify that "arg" is of a particular type, and that it has a particular property set. I'm trying to do this: one(mock).myMethod(with(hasProperty("prop", eq("value")))); but hasProperty() returns Object, while myMethod() is expecting type Argument.
I *can* do the following, which works: one(mock).myMethod(with(any(Argument.class))); I've been googling this problem for 2 days now, so any help would be greatly appreciated, thanks.
|
|
|
Re: how to test property of argument passed to method?Unfortunately, Java's generics are crap, so while the following doesn't compile:
one(mock).myMethod(with(hasProperty("prop", eq("value")))); The following does: Matcher<Bob> propEqualToValue = hasProperty("prop", equalTo("value"); one(mock).myMethod(with(propEqualToValue)); Maybe they'll make generics work one day, but until then we'll have to use awkward workarounds like that. --Nat 2008/10/2 Limbic System <limbicsystem@...>: > What is the proper way to test a property of an argument passed into a > method of a mock, under JMock 2.5? > Let's say that my CUT would normally be called like: > myObject.myMethod(arg); > and I'd like to verify that "arg" is of a particular type, and that it has a > particular property set. I'm trying to do this: > one(mock).myMethod(with(hasProperty("prop", eq("value")))); > but hasProperty() returns Object, while myMethod() is expecting type > Argument. > I *can* do the following, which works: > one(mock).myMethod(with(any(Argument.class))); > I've been googling this problem for 2 days now, so any help would be greatly > appreciated, thanks. > --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|
|
Re: how to test property of argument passed to method?Nat,
Thanks for your speedy reply. Too bad the generics don't allow that syntax, but at least now I can stop banging my head against the desk. :-) L On Thu, Oct 2, 2008 at 1:37 AM, Nat Pryce <nat.pryce@...> wrote: Unfortunately, Java's generics are crap, so while the following doesn't compile: |
|
|
Re: how to test property of argument passed to method?It sure seems like something could be done in the API to make it simpler, but I'm not sure what. So let me think out loud here.
What we really want is the ability to explicitly specify the type parameter like in C++ where you could say with< Bob >( hasProperty(...) ) Java does have explicit type parameters for methods, but it only works if qualified with this for an instance method or the class name for static methods (See <http://www.angelikalanger.com/GenericsFAQ/FAQSections/TechnicalDetails.html#What%20explicit%20type%20argument%20specification?>). You would have to do something like this: this.< Bob >with( hasProperty(...) ) It seems you could accomplish the same thing just using casting since with is going to return null any way: one(mock).myMethod( (Bob)with(hasProperty("prop", equalTo("value") ) ); I have said it before, but I think the generic parameter on Matcher was a bad idea and adds no value. At the very least there should have been non-generic matchers and generic ones are subinterface of that. hasProperty should return the non-generic Matcher while some other method could return a generic one. Then you can overload differently based on generic or non-generic. Perhaps it might be good to have a with method that takes a class parameter (Note I used ... to allow multiple Matchers that are anded together): T with( Class<T> clazz, Matcher< ? >... matcher ) This is essentially the same as: (T)with( allOf( is( clazz ), allOf( matcher ) ) ) ); Which could then be used as one(mock).myMethod( with(Bob.class, hasProperty("prop", equalTo("value") ) );
On Thu, Oct 2, 2008 at 3:54 PM, Limbic System <limbicsystem@...> wrote:
-- Dale King |
|
|
Re: how to test property of argument passed to method?On Thu, Oct 2, 2008 at 5:38 PM, Dale King <dalewking@...> wrote:
This is very close to what I was originally trying to do, and quite readable.
L |
| Free Forum Powered by Nabble | Forum Help |