Java MockitoJUnitRunner 已弃用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41909538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MockitoJUnitRunner is deprecated
提问by Albert Hendriks
I'm trying to make a unit test with @InjectMocks
and @Mock
.
我正在尝试使用@InjectMocks
and进行单元测试@Mock
。
@RunWith(MockitoJUnitRunner.class)
public class ProblemDefinitionTest {
@InjectMocks
ProblemDefinition problemDefinition;
@Mock
Matrix matrixMock;
@Test
public void sanityCheck() {
Assert.assertNotNull(problemDefinition);
Assert.assertNotNull(matrixMock);
}
}
When I don't include the @RunWith
annotation, the test fails. But
当我不包含@RunWith
注释时,测试失败。但
The type MockitoJUnitRunner is deprecated
不推荐使用 MockitoJUnitRunner 类型
I'm using Mockito 2.6.9. How should I go about this?
我正在使用 Mockito 2.6.9。我应该怎么做?
采纳答案by Nicolas Filotto
org.mockito.runners.MockitoJUnitRunner
is now indeed deprecated, you are supposed to use org.mockito.junit.MockitoJUnitRunner
instead. As you can see only the package name has changed, the simple name of the class is still MockitoJUnitRunner
.
org.mockito.runners.MockitoJUnitRunner
现在确实已弃用,您应该org.mockito.junit.MockitoJUnitRunner
改用。如您所见,只有包名发生了变化,类的简单名称仍然是MockitoJUnitRunner
.
Excerpt from the javadoc of org.mockito.runners.MockitoJUnitRunner
:
摘自 javadoc 的org.mockito.runners.MockitoJUnitRunner
:
Moved to
MockitoJUnitRunner
, this class will be removed with Mockito 3
移至
MockitoJUnitRunner
,该类将随 Mockito 3 移除
回答by Hiccup
You can try this, if you do not want to use @RunWith(MockitoJUnitRunner.class)
你可以试试这个,如果你不想使用 @RunWith(MockitoJUnitRunner.class)
@BeforeClass
public void setup() {
MockitoAnnotations.initMocks(this);
}
回答by MateuszW90
You can try this:
你可以试试这个:
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
}
Because you add @Before
annotation, Your mock objects can be new and recorded many times, and in all test you can give objects new properties. But, if you want one time record behavior for mock object please add @BeforeCLass
因为你添加了@Before
注解,你的模拟对象可以是新的并且可以被多次记录,并且在所有的测试中你都可以赋予对象新的属性。但是,如果您想一次性记录模拟对象的行为,请添加@BeforeCLass
回答by git pull origin
There is also a @Rule
option:
还有一个@Rule
选项:
@Rule
public MockitoRule rule = MockitoJUnit.rule();
Or in Kotlin:
或者在 Kotlin 中:
@get:Rule
var rule = MockitoJUnit.rule()
回答by Akshay Chopra
You can try importing the following:
您可以尝试导入以下内容:
import org.mockito.runners.MockitoJUnitRunner;
Also, if you are using Eclipse, just press Ctrl + Shift + Oand it will auto import it.
此外,如果您使用的是Eclipse,只需按Ctrl + Shift + O,它就会自动导入它。