Ale*_*nko 8 java testing android mocking mockito
我已经插入了需要依赖
testCompile 'org.mockito:mockito-core:1.10.19'
Run Code Online (Sandbox Code Playgroud)
然后我把我的测试代码放到/src/test/java/目录中
然后我尝试启动这样的测试
import org.junit.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
public class PresenterActivityAcceptNotAcceptTest {
@Test
public void emailValidator_CorrectEmailSimple_ReturnsTrue() {
boolean dd = true;
assertThat(dd, is(true));
}
Run Code Online (Sandbox Code Playgroud)
它工作正常,但如果我添加任何与Mocklib相关的女巫
例如 @RunWith
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
@RunWith(MockitoJUnitRunner.class)
public class PresenterActivityAcceptNotAcceptTest {
@Test
public void emailValidator_CorrectEmailSimple_ReturnsTrue() {
boolean dd = true;
assertThat(dd, is(true));
}
Run Code Online (Sandbox Code Playgroud)
我有这样的错误
Error:Execution failed for task ':Application:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
Error:(10, 10) error: cannot find symbol class MockitoJUnitRunner
Error:(5, 27) error: package org.mockito.runners does not exist
/home/aleksey/Downloads/NTZ/FittingRoom/Application/src/test/java/com/fittingroom/newtimezone/presenters/PresenterActivityAcceptNotAcceptTest.java
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
如果我伪造某事,请随意询问
提前致谢!
Pie*_*e C 11
正如Baeldung 的文章中所述,从 Mockito 版本开始2.2.20,软件包MockitoJUnitRunner发生了变化。所以改变:
import org.mockito.runners.MockitoJUnitRunner;
Run Code Online (Sandbox Code Playgroud)
到 :
import org.mockito.junit.MockitoJUnitRunner;
Run Code Online (Sandbox Code Playgroud)
像往常一样,您必须将mockito-core库导入到您的build.gradle:
dependencies {
testImplementation 'org.mockito:mockito-core:4.2.0'
}
Run Code Online (Sandbox Code Playgroud)
作为旁注,Matchers如果您使用它们,您还需要更改导入。例如 :
从 :
import static org.mockito.Matchers.any;
Run Code Online (Sandbox Code Playgroud)
到 :
import static org.mockito.Mockito.any;
Run Code Online (Sandbox Code Playgroud)
看起来 Gradle 没有完成它的工作。手动添加罐子可能会解决问题。 如何下载和安装 jar 到这里。
并下载 mockito 使用此链接
https://mvnrepository.com/artifact/org.mockito/mockito-core/1.10.19