Mockito Testcase忽略注释



我有一个Spring web应用程序,我想为我的控制器做单元测试。我决定不使用Spring来设置我的测试,而是使用Mockito来模拟对象与我的控制器。

我用Maven2和surefire插件构建和运行测试。这是来自我的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>

我设置我的编译器和确定插件如下:

<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>

我的测试类是这样的:

@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());
}

周围都是注释:-)

但是当从命令行构建时,我在(dataEntityTypeFacade.getEntityTypeFromTypeName(错误)).thenReturn(null)时获得NullPointerException;因为dataEntityTypeFacade对象为空。当我在Eclipse中运行我的测试用例时,一切都很好,我的模拟对象被实例化,用@Before注释的方法被调用。

为什么我的注释在从命令行运行时似乎被忽略了??

/Eva

您打过电话了吗?

 MockitoAnnotations.initMocks(testClass);

在基类或这里提到的测试运行器中:http://docs.mockito.googlecode.com/hg/org/mockito/Mockito.html#9

相关内容

  • 没有找到相关文章

最新更新