如何测试一个简单的setter方法

时间:2017-09-18 14:42:45

标签: android junit mocking mockito junit4

我要测试的课程在下面的代码部分发布。我正在尝试测试" setSubscriberName"方法。 我编码的测试发布在测试部分的下面。但在运行时测试失败

请让我知道如何正确测试该setter方法

public class ListViewModel {

private String mSubscriberName = null;

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

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("");
}

public void setSubscriberName(String str) {
    String mSubscriberName = null;
    mSubscriberName = str;
}
@Test
public void setSubscriberNameTest() throws Exception {
    ListViewModel spyListView = spy(mListViewModel);

    verify(spyListView).setSubscriberName("abc");
}

}

0 个答案:

没有答案