Java Intellij不';不能将Mockito类识别为测试类
我使用IntellJ Ultimate,在运行Mockito测试类时遇到问题 如果我使用这样的类Java Intellij不';不能将Mockito类识别为测试类,java,junit,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.jun
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类的任何内容
我的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>
4.0.0
1.
2.
1.0-快照
org.apache.maven.plugins
maven编译器插件
1.8
1.8
org.junit.jupiter
JUnitJupiter api
释放
1.
2.
1.0-快照
测试
朱尼特
朱尼特
释放
org.mockito
莫基托所有
1.9.5
MockitoJUnitRunner
是
摆脱它后,您可以使用@Before
-注释方法初始化模拟:
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
}
MockitoJUnitRunner
摆脱它后,您可以使用@Before
-注释方法初始化模拟:
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
}