![](/img/trans.png)
[英]mvn install is not working in eclipse but works fine from command line
[英]Tests Run fine in Eclipse but failed in mvn command
測試非常非常簡單
@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測試中的步驟操作, 但在Eclipse中通過了該測試,但是在Maven Surefire中失敗了,但是沒有運氣。
這里有任何幫助。
更新Mockito解決了該問題
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.