|
View:
New views
4 Messages
—
Rating Filter:
Alert me
|
|
|
aNonNull unexpected behaviourI've probably misinterpreted something, but here goes....
Using the State pattern (see e.g. http://exciton.cs.rice.edu/JavaResources/DesignPatterns/StatePat.htm) I want to test the logic in my states. So I have two interfaces: State and Context. Interface Context defines the method setState(State newState) and interface State defines some event handling methods. The interfaces are then implemented by the classes I want to test. My test methods look something like (using version 2.4.0): @Test public void testOnEvent() { // Class under test StateOne stateOne = new StateOne(mockContext); context.checking(new Expectations() { { // We should transit to StateTwo one(mockContext).setState(with(aNonNull(StateTwo.class))); } }); stateOne.onEvent(new SomeEvent()); context.assertIsSatisfied(); } To my delight, the test succeeds. My joy was somewhat dimmed when I noted that I can put any state I like as argument to aNonNull(), as long as it implements State. They all fullfill the expectation. This was not what I'd expect. I hoped that "with(aNonNull(StateTwo.class))" would ensure that I go to the right state, not just that the argument has the correct interface. I assume that I've got something wrong, but hope for any pointers to how I should do my tests to ensure that I really go to StateTwo. Cheers, /JMK |
|
|
Re: aNonNull unexpected behaviourTry
one(context).setState(with(instanceOf(StateTwo.class)); which should be more precise. Look at the static methods on the hamcrest Matchers class for more options. Give all the collaboration going on, I'd make a point of writing some integration tests to make sure this all hangs together. S. On 4 Jun 2008, at 12:04, Jan Kodet wrote: > > I've probably misinterpreted something, but here goes.... > > Using the State pattern (see e.g. > http://exciton.cs.rice.edu/JavaResources/DesignPatterns/ > StatePat.htm) I want > to test the logic in my states. So I have two interfaces: State and > Context. > Interface Context defines the method setState(State newState) and > interface > State defines some event handling methods. The interfaces are then > implemented by the classes I want to test. > > My test methods look something like (using version 2.4.0): > > @Test > public void testOnEvent() { > // Class under test > StateOne stateOne = new StateOne(mockContext); > > context.checking(new Expectations() { > { > // We should transit to StateTwo > > one(mockContext).setState(with(aNonNull(StateTwo.class))); > } > }); > > stateOne.onEvent(new SomeEvent()); > context.assertIsSatisfied(); > } > > > To my delight, the test succeeds. My joy was somewhat dimmed when I > noted > that I can put any state I like as argument to aNonNull(), as long > as it > implements State. They all fullfill the expectation. > > This was not what I'd expect. I hoped that > "with(aNonNull(StateTwo.class))" > would ensure that I go to the right state, not just that the > argument has > the correct interface. > > I assume that I've got something wrong, but hope for any pointers to > how I > should do my tests to ensure that I really go to StateTwo. > 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: aNonNull unexpected behaviourWell, it works, but since instanceOf returns Object, I have to cast it back to what I expected:
one(context).setState((StateTwo)with(instanceOf(StateTwo.class)); This lacks the normal beauty of JMock, so my gut feeling tells me that there is something here that I've missed. /JMK
|
|
|
Re: aNonNull unexpected behaviourI fear that's the best we can do given the mess that is Java generics.
One kludge might be to write a little helper method in the test class that wraps up the cast. one(context).setState(withA(StateTwo.class)); S. On 5 Jun 2008, at 08:32, Jan Kodet wrote: > Well, it works, but since instanceOf returns Object, I have to cast > it back > to what I expected: > > one(context).setState((StateTwo)with(instanceOf(StateTwo.class)); > > This lacks the normal beauty of JMock, so my gut feeling tells me > that there > is something here that I've missed. > > /JMK 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 |
| Free Forum Powered by Nabble | Forum Help |