java - 测试在 Eclipse 中运行良好,但在 mvn 命令中失败

标签 java maven maven-surefire-plugin

测试是非常非常简单的测试

@RunWith(MockitoJUnitRunner.class)
public class UnitTestMyCode {

  @InjectMocks
  private MyClass resource;

  @Mock
  private DependentBean bean;

  @Test
  public void test1() throws Exception {
    boolean ss = true;
    assertThat(ss).isTrue();
}

错误

org.mockito.exceptions.base.MockitoException: Field 'resource' annotated with @InjectMocks is null.
Please make sure the instance is created *before* MockitoAnnotations.initMocks();
Example of correct usage:
   class SomeTest {
      @InjectMocks private Foo foo = new Foo();

      @Before public void setUp() {
         MockitoAnnotations.initMock(this);

    at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl$1.withBefores(JUnit45AndHigherRunnerImpl.java:27)
    at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:276)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)

Surefire插件配置

   <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.4.2</version>
        <executions>
          <execution>
            <id>default-test</id>
            <phase>test</phase>
            <goals>
              <goal>test</goal>
            </goals>
            <configuration>
              <excludes>
                <exclude>**/*IntegrationTest*.java</exclude>
              </excludes>
              <includes>
                <include>**/*UnitTest*.java</include>
              </includes>
              <skip>false</skip>
            </configuration>
          </execution>
        </executions>
        <configuration>
          <excludes>
            <exclude>**/*IntegrationTest*.java</exclude>
          </excludes>  
          <includes>
                <include>**/*UnitTest*.java</include>
          </includes>            
          <skip>false</skip>
        </configuration>
      </plugin>

我已按照此 JUnit tests pass in Eclipse but fail in Maven Surefire 中的步骤操作 但运气不好。

这里有任何帮助。

最佳答案

更新 Mockito 解决了该问题

<dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-all</artifactId>
        <version>1.10.19</version>
        <scope>test</scope>
</dependency>

关于java - 测试在 Eclipse 中运行良好,但在 mvn 命令中失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44200521/

相关文章:

android - MAVEN:NoClassDeffFoundError

java - maven:无法运行junit5-jupiter-starter-maven?

unit-testing - Maven 不执行任何单元测试

java - 查看 int[][] 是否包含特定值

java - 如何在 VSCode 中重置或更改 SonarLint 的规则?

java - 如何仅重新运行失败的测试? (我用的是junit5+maven+spring boot)

java - 其他包中的@RestController 不起作用

java - 无法在项目上执行目标 org.apache.maven.plugins :maven-surefire-plugin:2. 21.0:test (default-test)

java - SWT下的Swing组件可以加速吗?

java.io.IOException : ipm. 注意。该系统找不到指定的文件