【问题标题】:Testing a getter method fails测试 getter 方法失败
【发布时间】:2017-09-18 14:18:31
【问题描述】:

我想测试的类发布在下面的代码部分。我正在尝试测试“getSubscriberName”方法。 我编码的测试发布在下面的测试部分。但在运行时测试失败,我收到下面发布的错误

我在 build.gradle 中使用的依赖项发布在下面

请告诉我如何纠正错误以及为什么会出现此错误

代码

public class ListViewModel {

private String mSubscriberName = null;

public ListViewModel(String subscriberName) {
    mSubscriberName = subscriberName;
}

public String getSubscriberName() {
    return mSubscriberName;
}

public void setSubscriberName(String name) {
    mSubscriberName = name;
}

}

}

测试

public class ListViewModelTest {

@Mock
private ListViewModel mListViewModel = null;
@Rule
public MockitoRule mockitoRule = MockitoJUnit.rule();

@Before
public void setUp() throws Exception {
    mListViewModel = new ListViewModel("");
}

@Test
public void getSubscriberName() {
    ListViewModel spyListViewModel = spy(mListViewModel);
    when(spyListViewModel.getSubscriberName()).thenReturn("YXZ");

    String expectedSubscriberName = spyListViewModel.getSubscriberName();
    Assert.assertEquals(expectedSubscriberName, spyListViewModel.getSubscriberName());
}

}

错误

java.lang.AbstractMethodError: org.powermock.api.mockito.internal.mockmaker.PowerMockMaker.isTypeMockable(Ljava/lang/Class;)Lorg/mockito/plugins/MockMaker$TypeMockability;

at org.mockito.internal.util.MockUtil.typeMockabilityOf(MockUtil.java:29)
at org.mockito.internal.util.MockCreationValidator.validateType(MockCreationValidator.java:22)
at org.mockito.internal.creation.MockSettingsImpl.validatedSettings(MockSettingsImpl.java:186)
at org.mockito.internal.creation.MockSettingsImpl.confirm(MockSettingsImpl.java:180)
at org.mockito.internal.MockitoCore.mock(MockitoCore.java:62)
at org.mockito.Mockito.mock(Mockito.java:1729)
at org.mockito.internal.configuration.MockAnnotationProcessor.process(MockAnnotationProcessor.java:33)
at org.mockito.internal.configuration.MockAnnotationProcessor.process(MockAnnotationProcessor.java:16)
at org.mockito.internal.configuration.IndependentAnnotationEngine.createMockFor(IndependentAnnotationEngine.java:38)
at org.mockito.internal.configuration.IndependentAnnotationEngine.process(IndependentAnnotationEngine.java:62)
at org.mockito.internal.configuration.InjectingAnnotationEngine.processIndependentAnnotations(InjectingAnnotationEngine.java:57)
at org.mockito.internal.configuration.InjectingAnnotationEngine.process(InjectingAnnotationEngine.java:41)
at org.mockito.MockitoAnnotations.initMocks(MockitoAnnotations.java:69)
at org.mockito.internal.junit.JUnitRule$1.evaluate(JUnitRule.java:42)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

gradle.build

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'

// required if you want to use Mockito for unit tests
testCompile 'org.mockito:mockito-core:2.7.22'
// required if you want to use Mockito for Android tests
androidTestCompile 'org.mockito:mockito-android:2.7.22'

testCompile 'org.powermock:powermock-api-mockito:1.6.1'
testCompile 'org.powermock:powermock-module-junit4-rule-agent:1.6.1'
testCompile 'org.powermock:powermock-module-junit4-rule:1.6.1'
testCompile 'org.powermock:powermock-module-junit4:1.6.1'

compile 'com.squareup.okhttp3:logging-interceptor:3.4.0'
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.squareup:otto:1.3.8'
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.android.support:recyclerview-v7:25.1.1'
compile 'com.squareup.okhttp3:okhttp:3.9.0'
compile 'com.google.code.gson:gson:2.8.1'
compile 'com.solidfire.code.gson:gson:2.6.2'
compile 'com.google.code.gson:gson-parent:2.8.1'
compile 'com.solidfire.code.gson:gson-parent:2.6.2'
compile 'com.android.support:support-annotations:25.1.1'
compile 'com.squareup.picasso:picasso:2.5.2'
}

【问题讨论】:

  • 可能与您的错误无关,但您正在测试模拟,而不是您的课程。
  • 请您澄清一下..测试模拟是什么意思??
  • 1.您的 mListViewModel 被注释定义为模拟。 2. 你把你的模拟包在一个间谍里面 3. 你定义你的getSubscriberName 函数应该返回“YXZ”然后验证它是expectedSubscriberName

标签: android junit mocking mockito powermock


【解决方案1】:

尽量避免使用 spy,因为它会通过调用所有依赖的类方法来进行实际调用并使您的测试用例闻起来更臭,请在下面找到您问题的解决方案(我仍然根据您的代码使用 spy)

@RunWith(MockitoJUnitRunner.class)
public class ListViewModelTest {
@InjectMocks
private ListViewModel mListViewModel;
@Rule
public MockitoRule mockitoRule = MockitoJUnit.rule();

@Before
public void setUp() throws Exception {
   // mListViewModel = new ListViewModel("");
}

@Test
public void getSubscriberName() {
    ListViewModel spyListViewModel = spy(Mockito.mock(ListViewModel.class));
    when(spyListViewModel.getSubscriberName()).thenReturn("YXZ");
    when(mListViewModel.getSubscriberName()).thenReturn("YXZ");
    String expectedSubscriberName = spyListViewModel.getSubscriberName();
    Assert.assertEquals(expectedSubscriberName, mListViewModel.getSubscriberName());
}

}

【讨论】:

    猜你喜欢
    • 2021-12-15
    • 1970-01-01
    • 2018-07-04
    • 2018-03-08
    • 1970-01-01
    • 1970-01-01
    • 2013-12-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多