2017-05-26 172 views
0

测试是非常非常简单的测试测试在Eclipse中运行良好,但在MVN命令失败

@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 但没有运气。

这里有任何帮助。

+0

你尝试过什么错误提示你做什么? –

+1

Maven永远是对的。你可以显示你的mockito依赖? – davidxxx

+1

您正在使用maven-surefire插件的极端旧版本。此外你为什么要做这么多的配置。按照命名约定使生活更轻松...是的,整个pom文件是一个好主意...... – khmarbaise

回答

0

更新解决的Mockito问题

<dependency> 
     <groupId>org.mockito</groupId> 
     <artifactId>mockito-all</artifactId> 
     <version>1.10.19</version> 
     <scope>test</scope> 
</dependency> 
相关问题