Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mockito Testcase ignores annotations

I have a Spring web application and I want to make unittests for my controllers. I have decided not to use Spring to setup my tests but to use Mockito mock objects in conjunction with my controllers.

I build 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 setup my compiler and surefire plugins like this:

<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 looks like this:

@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 all around :-)

But when building from the commandline i get a NullPointerException in the line when(dataEntityTypeFacade.getEntityTypeFromTypeName(wrong)).thenReturn(null); as the dataEntityTypeFacade object is null. When I run my testcase in Eclipse all is well and my mock objects are instantiated and the method annotated with the @Before is called.

Why are my annotations seemingly ignored when running from the command line???

/Eva

like image 374
Eva Troels Avatar asked Aug 10 '11 10:08

Eva Troels


People also ask

Why is JUnit test ignored?

Sometimes you may require not to execute a method/code or Test Case because coding is not done fully. For that particular test, JUnit provides @Ignore annotation to skip the test. Ignore all test methods using @Ignore annotation.

How do you ignore test cases in testing?

In such cases, annotation @Test(enabled = false) helps to disable this test case. If a test method is annotated with @Test(enabled = false), then the test case that is not ready to test is bypassed. Now, let's see @Test(enabled = false) in action.

How do you ignore a test case in JUnit?

The @Ignore annotation helps in this scenario. A test method annotated with @Ignore will not be executed. If a test class is annotated with @Ignore, then none of its test methods will be executed.

How do you ignore test cases in JUnit 4?

Overview. In this post, we will learn how to ignore or disable test methods or classes while running test cases. To ignore a test in JUnit you can either comment a method or delete the @Test annotation, but the test runner will not report such a test.


1 Answers

Have you called:

 MockitoAnnotations.initMocks(testClass);

in the base class or a test runner as mention here: http://docs.mockito.googlecode.com/hg/org/mockito/Mockito.html#9

like image 129
Andreas Köberle Avatar answered Oct 10 '22 18:10

Andreas Köberle