Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unit test a method with enum parameters?

I'm using junit and EasyMock to do unit testing on a project I'm working on. However, I've run into a problem. I have a good handful of methods that have a parameter that is an enumeration.

I ran into the java.lang.NullPointerException when attempting to mock the enum, and it seems enums just cannot be mocked. More information I found on it here:

http://download.oracle.com/javase/tutorial/java/javaOO/classvars.html

Is there any good way to unit test this method without mocking the enum??

Thanks!

EDIT: Péter Török was right! I was completely looking over the fact that I could just plug in something for the enum. For example:

public void methodName(String description, Location buildingLocation) {

where Location is my enum, I can call the method as:

methodName("here is my description", Location.DENVER);

like image 250
Jay Avatar asked Dec 10 '22 09:12

Jay


1 Answers

What does your enum contain that you need to mock it? Why can't you just simply use the available values themselves?

Since enums are (supposed to be) stateless and immutable, they should be readily available for unit testing, you should have no problems in instantiating them, they should hold no (mutable) global state and should have no external dependencies which make them hard to use in unit tests.

Failing any of the above would be a sign of a design problem to me rather than a unit testing problem.

like image 62
Péter Török Avatar answered Dec 11 '22 23:12

Péter Török