java - Intellij 不将 Mockito 类识别为测试类

标签 java junit mockito

我使用 IntellJ Ultimate,但运行 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>

最佳答案

MockitoJUnitRunneralready deprecated

摆脱它之后,您可以使用 @Before 带注释的方法初始化您的模拟:

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

关于java - Intellij 不将 Mockito 类识别为测试类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43635999/

相关文章:

Java Scanner 分隔符和 System.in

java - hibernate "is not mapped"异常

java - 如何模拟具有功能接口(interface)参数的方法?

java - 使用 Mockito 测试 CompletableFuture.supplyAsync

java - 压缩并保存位图/缩略图

java - 使用java将两个数组转换为json字符串对象

java - 如何编写 JUnit 测试来比较 2 个对象列表的相等性?

java - PatternSyntaxException : while using String. java中的ReplaceAll函数?

java - 使用 Junit 和 Mock put 请求进行测试

java - 安卓单元测试 : mocking System. currentTimeMillis()