Avinash Nayak | 12 Sep 2008 13:01
Picon
Favicon

Can I mock an enum

I was trying to write testcases for a method which takes an enum as parameter.
I dont want to pass any particular value to it.

This enum also contains a few methods that i want to mock.
Is there any way to do so?

Please let me know if my question is not clear.
I will try to post a sample then.

Thanks,
Avinash

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

    http://xircles.codehaus.org/manage_email

Chris O'Connell | 12 Sep 2008 15:18
Gravatar

Testing a subclass

What is the best way to test a method on a subclass that overrides the
parent method?  What I'm doing in the child version of the method is simply
take the parameters that get passed in and call a couple methods on them and
then just forward on to the superclass version of the method.  Like so:

public void overriddenMethod(Foo thing1, Baz thing2) {

	thing1.setX("X");
	thing2.setY("Y");
	super.overriddenMethod(thing1, thing2);
}

Thanks,
Chris

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

    http://xircles.codehaus.org/manage_email

Geoffrey Wiseman | 12 Sep 2008 17:54
Picon
Gravatar

Re: Can I mock an enum

On Fri, Sep 12, 2008 at 7:01 AM, Avinash Nayak <avi_gaming-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> wrote:
I was trying to write testcases for a method which takes an enum as parameter.
I dont want to pass any particular value to it.

IIRC, Java enum classes are final, so you can't subclass them.  I believe JMock only supports interface-proxy and subclass-proxy mocking, so neither of those will work on an enum.  

Ultimately, I think some on this list would be happy to argue that mocking an Enum may not be the right way to go anyway, although I'm less dogmatic on the subject.  Can I ask what's wrong with passing in a particular Enum value?

  - Geoffrey
--
Geoffrey Wiseman

Steve Freeman | 12 Sep 2008 20:01
Picon

Re: Can I mock an enum

Firstly, I don't believe you can.

Secondly, I would just use one of the enums directly in the test case,  
as an example. If an enum is that complicated, it should defer to  
other objects.

S.

On 12 Sep 2008, at 12:01, Avinash Nayak wrote:
> I was trying to write testcases for a method which takes an enum as  
> parameter.
> I dont want to pass any particular value to it.
>
> This enum also contains a few methods that i want to mock.
> Is there any way to do so?
>
> Please let me know if my question is not clear.
> I will try to post a sample then.

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

Stephen Smith | 12 Sep 2008 21:08
Picon
Gravatar

Re: Can I mock an enum

The Jakarta Commons Lang Enum package is far better than the Java5
implementation, but mocking domain objects or enumerated types seems like a code 
smell to me.

Exactly what behaviour are you trying to assert?

Steve.

---
Stephen Smith, MEng (Wales).
http://www.stephen-smith.co.uk/

Avinash Nayak wrote:
> I was trying to write testcases for a method which takes an enum as parameter.
> I dont want to pass any particular value to it.
> 
> This enum also contains a few methods that i want to mock.
> Is there any way to do so?
> 
> Please let me know if my question is not clear.
> I will try to post a sample then.
> 
> Thanks,
> Avinash
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
> 
>     http://xircles.codehaus.org/manage_email
> 
> 
> 
> ------------------------------------------------------------------------
> 
> 
> No virus found in this incoming message.
> Checked by AVG - http://www.avg.com 
> Version: 8.0.169 / Virus Database: 270.6.21/1669 - Release Date: 9/12/2008 2:18 PM
> 

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

    http://xircles.codehaus.org/manage_email

Steve Freeman | 12 Sep 2008 22:01
Picon

Re: Testing a subclass

I'd say test the whole thing together. If there's behaviour in the  
parent that is complex enough to need special handling, then extract  
another object and use composition.

Sorry for being so ornery.

S.

On 12 Sep 2008, at 14:18, Chris O'Connell wrote:
> What is the best way to test a method on a subclass that overrides the
> parent method?  What I'm doing in the child version of the method is  
> simply
> take the parameters that get passed in and call a couple methods on  
> them and
> then just forward on to the superclass version of the method.  Like  
> so:
>
>
> public void overriddenMethod(Foo thing1, Baz thing2) {
>
> 	thing1.setX("X");
> 	thing2.setY("Y");
> 	super.overriddenMethod(thing1, thing2);
> }

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

Pat Maddox | 13 Sep 2008 00:01
Picon

Re: Can I mock an enum

On Fri, Sep 12, 2008 at 3:08 PM, Stephen Smith
<steve@...> wrote:
> mocking domain objects ... seems like a code smell to me.

Really?  What do you usually mock?

Pat

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

    http://xircles.codehaus.org/manage_email

Avinash | 13 Sep 2008 08:41
Picon
Favicon

Re: Can I mock an enum

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.

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

    http://xircles.codehaus.org/manage_email

Steve Freeman | 13 Sep 2008 09:52
Picon

Re: Re: Can I mock an enum

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

Avinash | 13 Sep 2008 10:19
Picon
Favicon

Re: Can I mock an enum

First of all, the code snippet was just to convey my message.
The method i want to test depends on Strategy.getValue().
Though its a simple Boolean here. it may not be the case also.
So while testing myMethod() I could ensure it uses only getValue method
 out of Strategy class.
And I could have mocked the returnValue and go ahead with the test.

Otherwise, I would have to test the given method by 
 passing all the enum values out of Strategy Enum.
Passing for a single enum value, doesnt say the test has succeeded.
And there might be case that i have 10-12 values in the enum.

Anyway, it was nice to see some good comments... :)

Thanks again.

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

    http://xircles.codehaus.org/manage_email


Gmane