Mockito 使用注解来初始化的代码片断 例子
2016-09-05 14:03
495 查看
initMocks(this); 这句话的意思是初始化所有需要mock的对象,这些对象是使用@Mock注解所定义的
import org.junit.Before; import org.junit.Test; import org.mockito.Mock; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; import static org.mockito.Mockito.when; import static org.mockito.MockitoAnnotations.initMocks; public class HealthServiceTest { @Mock private MetricsClient metricsClient; @Mock private SequenceService sequenceService; @Mock private MetricsFormatter metricsFormatter; HealthService healthService; @Before public void setUp() throws Exception { initMocks(this); healthService = new HealthService(new SystemStatus(new SDAStatus(true, true)), metricsClient, metricsFormatter, sequenceService); } @Test public void willReturnAvailable() throws Exception { when(sequenceService.isHealth()).thenReturn(true); HealthReport healthReport = healthService.getHealthReport(); assertThat(healthReport.isAvailable(), is(true)); } @Test public void willReturnUnavailableAfterTimeout() throws Exception { HealthReport healthReport = healthService.getHealthReport(); assertThat(healthReport.isAvailable(), is(true)); healthService.setHealthReportTimeout(10); Thread.sleep(100); HealthReport anotherHealthReport = healthService.getHealthReport(); assertThat(anotherHealthReport.isAvailable(), is(false)); } }
相关文章推荐
- 使用Spring+Junit+Mockito做代码自测
- 当Mockito遭遇使用注解注入的变量
- junit与testng 分别和mockito 结合使用例子
- mockito-初始化注解
- Ext的使用和实用的例子和代码
- 使用类成员初始化特性简化构造代码
- Android 2.0上使用蓝牙通信代码片断(服务端、客户端、数据传输)
- 使用JAVA发送HTTP请求(Http Request),返回HTTP响应(Http Response)内容,代码程序例子及原理说明
- MyPage从Page派生,MyPage总有一个Label控件。以便以后从MyPage派生的页都可以直接使用Label?给个代码例子看看?
- Linux DirectFB DFB 简单 初始化 例子 代码
- 使用iPhone三轴感应器的代码例子
- 使用 Eclipse Snippet 插件来管理常用代码片断。
- 使用 minizip 解压 ZIP 文件的代码例子
- Winsock例子代码:初始化、阻塞/非阻塞、超时设置、select等
- 使用扩展方法对代码的行为进行封装的例子:封装UIElement的“拖动”
- 使用RTP发送h.264的例子代码分析
- zt WinForm应用程序使用后台线程进行长耗时初始化代码
- Winsock例子代码:初始化、阻塞/非阻塞、超时设置、select等
- 在 Oracle中使用UTL_SMTP 进行邮件发送的例子代码
- ZedGraph控件的使用--属性和例子代码