如何对依赖ZooKeeper的代码写单元测试
生活随笔
收集整理的這篇文章主要介紹了
如何对依赖ZooKeeper的代码写单元测试
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
日常開發中,如何使用了ZooKeeper,在對這部分做單元測試的時候,往往很不方便,因為實際安裝一個ZooKeeper專門用來做單元測試,實在是太浪費。這種情況下,可以使用curator-test。curator是Netflix公司開源的一個Zookeeper客戶端,curator-test正是其中的一員,通過它可以Mock一個本地(127.0.0.1)ZooKeeper Server用于測試,非常方便。
Maven Dependency
<dependency><groupId>org.apache.curator</groupId><artifactId>curator-test</artifactId><version>2.6.0</version><scope>test</scope> </dependency>Getting Started
private static TestingServer server; private static CuratorFramework client;@BeforeClass public static void setUpBeforeClass() throws Exception {server = new TestingServer(2181, true);server.start();client = CuratorFrameworkFactory.newClient("127.0.0.1",new ExponentialBackoffRetry(1000, 3));client.start(); }@AfterClass public static void tearDownAfterClass() throws IOException {server.stop();client.close(); }@Test public void testFoobar() throws Exception {System.out.println("client: " + client);client.create().forPath("/test", "test-data".getBytes());byte[] data = client.getData().forPath("/test");System.out.println("data: " + new String(data)); }總結
以上是生活随笔為你收集整理的如何对依赖ZooKeeper的代码写单元测试的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 计算机网络原理笔记-三次握手
- 下一篇: 文档加载完后执行相关事件