我要测试的课程在下面的代码部分发布。我正在尝试测试" getSubscriberName"方法。 我编码的测试发布在测试部分的下面。但在运行时测试失败,我接收到下面发布的错误
我在build.gradle中使用的依赖项发布在
下面请让我知道如何更正错误以及我收到错误的原因
码
import microservices_rca
}
}
测试:
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;
}
erroe :
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());
}
}
gradle.build :
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)
答案 0 :(得分:0)
尝试避免间谍,因为它通过调用所有依赖类方法进行实际调用并使您的测试用例闻起来更多,请在下面找到您的问题的解决方案(仍然根据您的代码使用间谍)
@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());
}
}