下面是我的build.gradle的相关内容:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
androidTestCompile "org.mockito:mockito-core:1.10.19"
androidTestCompile 'com.google.dexmaker:dexmaker:1.0'
androidTestCompile('com.google.dexmaker:dexmaker-mockito:1.0') {
exclude module: 'hamcrest-core'
exclude module: 'objenesis'
exclude module: 'mockito-core'
}
androidTestCompile 'org.hamcrest:hamcrest-library:1.3'
}当我使用@Mock注释声明模拟时,它是空的。但当我用
context = mock(Context.class);然后我得到一个正确的模拟对象。我在Junit-3 TestCase中使用这个,如果这很重要的话。
为什么注释不起作用?
发布于 2015-01-23 14:56:22
如果使用JUnit 3,则必须在测试中的set方法中使用MockitoAnnotations:
public class ATest extends TestCase {
public void setUp() {
MockitoAnnotations.initMocks(this);
}
// ...
}注解是无法开箱即用的,您必须指示JUnit做一些事情。对于完整的参考,对于JUnit 4,您有其他推荐的选项:
使用JUnit运行程序:
@RunWith(MockitoJUnitRunner.class)
public class ATest {
@Mock Whatever w;
// ...
}或者使用JUnit规则:
public class ATest {
@Rule MockitoRule mockitoRule = MockitoJUnit.rule();
@Mock Whatever w;
// ...
}https://stackoverflow.com/questions/28109204
复制相似问题