Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 测试在Eclipse中运行正常,但在mvn命令中失败_Java_Maven_Maven Surefire Plugin - Fatal编程技术网

Java 测试在Eclipse中运行正常,但在mvn命令中失败

Java 测试在Eclipse中运行正常,但在mvn命令中失败,java,maven,maven-surefire-plugin,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).i

这个测试非常简单

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

但是没有运气


这里有任何帮助。

更新Mockito解决了问题

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

org.mockito
莫基托所有
1.10.19
测试

更新Mockito解决了这个问题

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

org.mockito
莫基托所有
1.10.19
测试

您尝试过错误建议您做的事情吗?Maven总是正确的。你能展示你的mockito依赖性吗?你正在使用一个非常旧版本的maven surefire插件。此外,你为什么要做这么多的配置。遵循命名约定使生活更轻松。。。是的,整个pom文件是一个好主意……即使使用mockito verison 3.0.0,我也面临着这个问题。你试过错误建议你做什么吗?Maven总是对的。你能展示你的mockito依赖性吗?你正在使用一个非常旧版本的maven surefire插件。此外,你为什么要做这么多的配置。遵循命名约定使生活更轻松。。。是的,整个pom文件是一个好主意……即使使用mockito verison 3.0.0,我也面临着这个问题。有什么帮助吗