Pencils Down

This weblog is about my experiences in software development

Browsing Posts tagged JUnit

Sparse response:

3 Yes,

2 Will be

1 No

Only asked as this is the first place I have worked in many years that does not have JUnit as part of the build process.

Turn off this setting: Project->Properties->Java Compiler->Enable Project Specific Settings

Likely there is something in your specific settings that is off (again likely wrong JDK)

After tossing and turning for some time with the query:

from Change

and getting the error “Change not mapped” when running the associated JUnit test I finally remembered to use the full path:

from com.dantoomeysoftware.domain.Change

Not sure why it can’t find the object. I know there is probably some nice, theoretical explanation, but I don’t care. The first query should work.

This error is the result of trying to mock a class instead of an interface.  In order to mock a class you have to use the class extensions instead.

So, change your code from:

import static org.easymock.EasyMock.*;


import static org.easymock.classextension.EasyMock.*;

I think it’s just historically how EasyMock got developed:

  1. first interfaces like nice Java/Spring/JUnit theory says your supposed to develop,
  2. then normal classes for the working Java programmers of the world.

EasyMock, like other JUnit mock testers, allows you to set expectations for method calls on the mocked object.  However, the expect() method wants to see a return type from the method call.  So, what to do?

A built-in mechanism of EasyMocks is to just “call” the void method in question.  For example a commonly mocked object is the HttpSession:

    session.setAttribute(eq(name), isA(InSession.class));
    //whatever you are really trying to test

Could only be easier if the doc told you this clearly.

This error is a catchall for anything else that might be wrong with your test class.  First make sure any of the below problems are not occurring. 

Your unit test code is running JUnit4.  At least one test method must have the @Test annotation.  Otherwise this error occurs.

If the class you are testing has @Required setters for Spring injections they must be set otherwise JUnit will mask the missing required exception as a no runnable methods exception.

If you are initializing your mock objects outside of the @Begin method you will get this error.

If you are attempting to mock the same class more than once you will get this error.