【问题标题】:Mockito Testcase ignores annotationsMockito 测试用例忽略了注释
【发布时间】:2011-08-10 10:07:10
【问题描述】:

我有一个 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>

我像这样设置我的编译器和surefire插件:

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

到处都是注释:-)

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

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

/伊娃

【问题讨论】:

  • “从命令行构建”是指 Maven 构建还是其他?

标签: java maven-2 junit mockito


【解决方案1】:

你打过电话吗:

 MockitoAnnotations.initMocks(testClass);

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

【讨论】:

  • 我忘了在问题中发布这个:我的测试类定义上方有@RunWith(MockitoJUnitRunner.class)。按照我的理解,我不应该调用 initMocs。
  • 我阅读了文档,听起来您需要 initMocks() 行才能使其工作;它以粗体字显示“重要!”部分。它说你“可以”使用 Runner;我不确定有或没有 Runner 有什么区别。但你肯定需要这条线。 IMO - @Mock 注释不如手动设置模拟。
  • @Gweebz:不,the runner 替换为:“初始化用 Mock 注释的模拟,因此不需要显式使用 MockitoAnnotations.initMocks(Object)。”
  • 当我在 Eclipse 中运行测试用例时,它运行良好,但当我将它作为我的 Maven 构建的一部分运行时,它就不行了。 @Before 注释也被忽略。这让我相信这个问题可能更多地与 Maven(编译器插件或 surefire 插件)有关,而不是与 Mockito 的关系。 ???
  • 我一直在运行 JUnit3。在surefire插件上将“junitArtifactName”配置参数设置为“org.junit:com.springsource.org.junit”有帮助。这里描述了同样的问题:stackoverflow.com/questions/2182376/…
猜你喜欢
  • 2019-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-08
  • 1970-01-01
  • 1970-01-01
  • 2020-04-29
  • 2020-01-21
相关资源
最近更新 更多