Re: Re: Can I mock an enum
Steve Freeman <
steve@...>
2008-09-13 07:52:03 GMT
You should be using mocks to test interactions with neighbouring
objects that represent "interesting" behaviour. For simple value
objects which includes simple value objects, I'd just use a real one.
Either the Strategy makes a difference to the behaviour of the unit
tested code, in which case you want to test the effect, or it doesn't,
in which case any of the values will work.
See also
http://www.mockobjects.com/2007/04/test-smell-everything-is-mocked.html
S.
On 13 Sep 2008, at 07:41, Avinash wrote:
> I got what you guys saying.
> Understood the fact that the enums are final
> and hence cant mock them using jmock rather with
> ClasssImposteriser.INSTANCE.
> Still will try to convince what i was trying to achieve.
> I am posting a code snippet. this is quite simple version of my code.
>
> public enum Strategy {
> StrategyTrue(Boolean.TRUE), StrategyFalse(Boolean.FALSE),
> StrategyNone(null);
>
> private Boolean value;
>
> private Strategy(Boolean value) {
> this.value = value;
> }
>
> public Boolean getValue() {
> return value;
> }
> }
>
> public class MyClass {
> public Boolean myMethod(Strategy strategy) {
> return strategy.getValue();
> }
> }
>
> public class MyClassUTEST extends TestCase {
> public void test_myMethod() {
> // Mocked Value;
> final Boolean mockedValue = Boolean.TRUE;
>
> Mockery context = new Mockery();
> context.setImposteriser(ClassImposteriser.INSTANCE);
> final Strategy strategy = context.mock(Strategy.class);
>
> context.checking(new Expectations() {
> {
> oneOf(strategy).getValue();
> will(returnValue(mockedValue));
> }
> });
>
> assertEquals(mockedValue, new MyClass().myMethod(strategy));
> context.assertIsSatisfied();
> }
> }
>
> This way a single testcase can cover all the test scenarios of the
> method.
> Otherwise, i would have to write test for each Enum value.
> I could make it work by changing the enum in the following way.
>
> public enum Strategy {
> StrategyTrue() {
> <at> Override
> public Boolean getValue() {
> return Boolean.TRUE;
> }
> },
> StrategyFalse() {
> <at> Override
> public Boolean getValue() {
> return Boolean.FALSE;
> }
> },
> StrategyNone() {
> <at> Override
> public Boolean getValue() {
> return null;
> }
> };
>
> public abstract Boolean getValue();
> }
>
> But still, i would have appreciated the first attempt.
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