MockitoJUnitRunner 已弃用
MockitoJUnitRunner is deprecated
我正在尝试使用 @InjectMocks
和 @Mock
进行单元测试。
@RunWith(MockitoJUnitRunner.class)
public class ProblemDefinitionTest {
@InjectMocks
ProblemDefinition problemDefinition;
@Mock
Matrix matrixMock;
@Test
public void sanityCheck() {
Assert.assertNotNull(problemDefinition);
Assert.assertNotNull(matrixMock);
}
}
当我不包含 @RunWith
注释时,测试失败。但是
The type MockitoJUnitRunner is deprecated
我正在使用 Mockito 2.6.9。我该怎么办?
org.mockito.runners.MockitoJUnitRunner
is now indeed deprecated, you are supposed to use org.mockito.junit.MockitoJUnitRunner
代替。正如您所看到的,只有包名称发生了变化,class 的简单名称仍然是 MockitoJUnitRunner
.
摘自 org.mockito.runners.MockitoJUnitRunner
的 javadoc:
Moved to MockitoJUnitRunner
, this class will be removed with
Mockito 3
你可以试试这个:
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
}
因为你添加了@Before
注解,你的模拟对象可以是新的,可以多次记录,并且在所有测试中你可以给对象新的属性。但是,如果你想一次性记录模拟对象的行为,请添加 @BeforeCLass
还有一个@Rule
选项:
@Rule
public MockitoRule rule = MockitoJUnit.rule();
或者在 Kotlin 中:
@get:Rule
var rule = MockitoJUnit.rule()
您可以尝试导入以下内容:
import org.mockito.runners.MockitoJUnitRunner;
此外,如果您使用的是 Eclipse,只需按 Ctrl + Shift + O它会自动导入它。
在我的案例中,当我将依赖项更新到最新版本时,我设法解决了这个问题:
def mockito_version = '2.28.2'
// For local unit tests on your development machine
testImplementation "org.mockito:mockito-core:$mockito_version"
// For instrumentation tests on Android devices and emulators
androidTestImplementation "org.mockito:mockito-android:$mockito_version"
然后我通过替换命令更改了导入 (Mac: cmd+Shift+R Windows: Ctrl+Shift+R)
来自
import org.mockito.runners.MockitoJUnitRunner;
至
import org.mockito.junit.MockitoJUnitRunner;
我正在尝试使用 @InjectMocks
和 @Mock
进行单元测试。
@RunWith(MockitoJUnitRunner.class)
public class ProblemDefinitionTest {
@InjectMocks
ProblemDefinition problemDefinition;
@Mock
Matrix matrixMock;
@Test
public void sanityCheck() {
Assert.assertNotNull(problemDefinition);
Assert.assertNotNull(matrixMock);
}
}
当我不包含 @RunWith
注释时,测试失败。但是
The type MockitoJUnitRunner is deprecated
我正在使用 Mockito 2.6.9。我该怎么办?
org.mockito.runners.MockitoJUnitRunner
is now indeed deprecated, you are supposed to use org.mockito.junit.MockitoJUnitRunner
代替。正如您所看到的,只有包名称发生了变化,class 的简单名称仍然是 MockitoJUnitRunner
.
摘自 org.mockito.runners.MockitoJUnitRunner
的 javadoc:
Moved to
MockitoJUnitRunner
, this class will be removed with Mockito 3
你可以试试这个:
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
}
因为你添加了@Before
注解,你的模拟对象可以是新的,可以多次记录,并且在所有测试中你可以给对象新的属性。但是,如果你想一次性记录模拟对象的行为,请添加 @BeforeCLass
还有一个@Rule
选项:
@Rule
public MockitoRule rule = MockitoJUnit.rule();
或者在 Kotlin 中:
@get:Rule
var rule = MockitoJUnit.rule()
您可以尝试导入以下内容:
import org.mockito.runners.MockitoJUnitRunner;
此外,如果您使用的是 Eclipse,只需按 Ctrl + Shift + O它会自动导入它。
在我的案例中,当我将依赖项更新到最新版本时,我设法解决了这个问题:
def mockito_version = '2.28.2'
// For local unit tests on your development machine
testImplementation "org.mockito:mockito-core:$mockito_version"
// For instrumentation tests on Android devices and emulators
androidTestImplementation "org.mockito:mockito-android:$mockito_version"
然后我通过替换命令更改了导入 (Mac: cmd+Shift+R Windows: Ctrl+Shift+R) 来自
import org.mockito.runners.MockitoJUnitRunner;
至
import org.mockito.junit.MockitoJUnitRunner;