【发布时间】:2016-03-15 06:14:42
【问题描述】:
我刚开始使用 Mockito,所以我可以在我的 Android 应用程序上执行单元测试。我不明白为什么会出现以下错误:
谁能告诉我这里缺少什么?
java.lang.AssertionError:
Expected :http://a/h/url
Actual :null
<Click to see difference>
at org.junit.Assert.fail(Assert.java:88)
at org.junit.Assert.failNotEquals(Assert.java:834)
at org.junit.Assert.assertEquals(Assert.java:118)
测试类
@RunWith(MockitoJUnitRunner.class)
public class NewsItemWrapperTest {
public static final String T_URL ="http://a/th/url";
public static final String H_URL ="http://a/h/url";
@Mock
NewsItem newsItem;
@Mock
NewsItemWrapper newsItemWrapper;
@Before
public void setUp() throws Exception {
//initMocks(this);
newsItemWrapper = new NewsItemWrapper(newsItem);
}
@Test
public void testGetThumbImageUrl() throws Exception {
assertEquals(T_URL, newsItemWrapper.getThumbNailImage());
}
@Test
public void testGetHeroImageUrl() throws Exception {
assertEquals(H_URL, newsItemWrapper.getHImage());
}
}
【问题讨论】:
-
添加您的测试用例代码。我的意思是 NewsItemWrapperTest
-
嗨@sankyjain 我添加了我的测试用例。感谢回复
-
你在哪里嘲笑过 newsItem?请添加那部分。问题是 newsItemWrapper.getHImage() 返回 null。所以我们需要看看你是如何嘲笑的
-
嗨 @sankyjain 我不确定你在哪里嘲笑 newsItem 是什么意思?
-
这就是全部代码?如果是,您应该添加所有方法模拟。例如,您应该模拟 newsItem.getMediaList() 方法等等。
标签: android unit-testing junit mockito