2017-04-26 30 views
0

我使用IntellJ Ultimate並且遇到了運行Mockito測試類的問題。Intellij不承認Mockito類作爲測試類

如果我使用一個類一樣,

import org.junit.jupiter.api.Test; 

class FactoryTest { 
    @Test 
    void createEntry() { 
    } 

} 

我可以直接在「運行」這一類。

如果我有這樣的:

import static org.mockito.Mockito.when; 

import org.junit.Assert; 
import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.mockito.InjectMocks; 
import org.mockito.Mock; 
import org.mockito.runners.MockitoJUnitRunner; 

// @RunWith attaches a runner with the test class to initialize the test data 
@RunWith(MockitoJUnitRunner.class) 
public class MathApplicationTester { 

    //@InjectMocks annotation is used to create and inject the mock object 
    @InjectMocks 
    MathApplication mathApplication = new MathApplication(); 

    //@Mock annotation is used to create the mock object to be injected 
    @Mock 
    CalculatorService calcService; 

    @Test 
    public void testAdd(){ 
     //add the behavior of calc service to add two numbers 
     when(calcService.add(10.0,20.0)).thenReturn(30.00); 

     //test the add functionality 
     Assert.assertEquals(mathApplication.add(10.0, 20.0),30.0,0); 
    } 
} 

我要創建之前,另一個類,並運行它,因爲該教程中還有什麼寫這將啓動測試的exlucisv類似乎不正確我http://www.vogella.com/tutorials/Mockito/article.html#testing-with-mock-objects

public class TestRunner { 
    public static void main(String[] args) { 
     Result result = JUnitCore.runClasses(MathApplicationTester.class); 

     for (Failure failure : result.getFailures()) { 
      System.out.println(failure.toString()); 
     } 

     System.out.println(result.wasSuccessful()); 
    } 
} 

我POM:

<?xml version="1.0" encoding="UTF-8"?> 
<project xmlns="http://maven.apache.org/POM/4.0.0" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 

    <groupId>1</groupId> 
    <artifactId>2</artifactId> 
    <version>1.0-SNAPSHOT</version> 
    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <configuration> 
        <source>1.8</source> 
        <target>1.8</target> 
       </configuration> 
      </plugin> 
     </plugins> 
    </build> 
    <dependencies> 
     <dependency> 
      <groupId>org.junit.jupiter</groupId> 
      <artifactId>junit-jupiter-api</artifactId> 
      <version>RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>1</groupId> 
      <artifactId>2</artifactId> 
      <version>1.0-SNAPSHOT</version> 
      <scope>test</scope> 
     </dependency> 
     <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <version>RELEASE</version> 
     </dependency> 
     <!-- https://mvnrepository.com/artifact/org.mockito/mockito-all --> 
     <dependency> 
      <groupId>org.mockito</groupId> 
      <artifactId>mockito-all</artifactId> 
      <version>1.9.5</version> 
     </dependency> 

    </dependencies> 

</project> 

回答

1

MockitoJUnitRunneralready deprecated

擺脫它後,你可以用一個@Before -annotated方法來初始化您的嘲弄:

@Before 
public void setup() { 
    MockitoAnnotations.initMocks(this); 
}