Mockito Testcase ignores annotations

I have a Spring web application and I want to do unittests for my controllers. I decided not to use Spring to install my tests, but to use Mockito mock objects along with my controllers.

I create and run tests with Maven2 and the surefire plugin. This is from my pom.xml

        <!-- Test -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.framework.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit</groupId>
            <artifactId>com.springsource.org.junit</artifactId>
            <version>4.5.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>1.9.0-rc1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

I configure my compilers and plugins, such as:

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <verbose>true</verbose>
                    <compilerVersion>1.6</compilerVersion>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.4.3</version>
            </plugin>

My test class is as follows:

@RunWith(MockitoJUnitRunner.class)
public class EntityControllerTest {

private EntityController entityController;

private DataEntityType dataEntityType = new DataEntityTypeImpl("TestType");

@Mock
private HttpServletRequest httpServletRequest;

@Mock
private EntityFacade entityFacade;

@Mock
private DataEntityTypeFacade dataEntityTypeFacade;

@Before
public void setUp() {
    entityController = new EntityController(dataEntityTypeFacade, entityFacade);
}

@Test
public void testGetEntityById_IllegalEntityTypeName() {
    String wrong = "WROOONG!!";
    when(dataEntityTypeFacade.getEntityTypeFromTypeName(wrong)).thenReturn(null);
    ModelAndView mav = entityController.getEntityById(wrong, httpServletRequest);
    assertEquals("Wrong view returned in case of error", ".error", mav.getViewName());
}

Annotations around :-)

But when building from the command line, I get a NullPointerException on the line when (dataEntityTypeFacade.getEntityTypeFromTypeName (incorrect)). thenReturn (null); since the dataEntityTypeFacade object is NULL. When I run my test file in Eclipse, everything is fine, and my breadboard objects are created and the method annotated with @Before is called.

, , ???

/Eva

+5
1

:

 MockitoAnnotations.initMocks(testClass);

, : http://docs.mockito.googlecode.com/hg/org/mockito/Mockito.html#9

+5

All Articles