{"action":"created","comment":{"url":"https://api.github.com/repos/xitu/gold-miner/pulls/comments/70576887","id":70576887,"diff_hunk":"@@ -1,597 +1,571 @@\n >* 原文链接 : [Unit tests with Mockito - Tutorial](http://www.vogella.com/tutorials/Mockito/article.html)\n * 原文作者 : [vogella](http://www.vogella.com/)\n * 译文出自 : [掘金翻译计划](https://github.com/xitu/gold-miner)\n-* 译者 : \n+* 译者 : [edvardhua](https://github.com/edvardHua/)\n * 校对者:\n \n+>这篇教程介绍了如何使用 Mockito 框架来给软件写测试用例\n \n+## 1\\. 预备知识\n \n-> This tutorial explains testing with the Mockito framework for writing software tests.\n+阅读这篇教程需要了解 JUnit 框架中的单元测试。\n \n-## 1\\. Prerequisites\n+如果你不熟悉 JUnit，请查看下面的教程：\n+[http://www.vogella.com/tutorials/JUnit/article.html](http://www.vogella.com/tutorials/JUnit/article.html)\n \n-The following tutorial requires an understanding of unit testing with the JUnit framework.\n+## 2\\. 使用mock对象来进行测试\n \n-In case your are not familiar with JUnit, please check the following JUnit tutorial: [http://www.vogella.com/tutorials/JUnit/article.html](http://www.vogella.com/tutorials/JUnit/article.html).\n+### 2.1\\. 单元测试的目标和挑战\n \n-## 2\\. Testing with mock objects\n+单元测试的思路是在不涉及依赖关系的情况下测试代码（隔离性），所以测试代码与其他类或者系统的关系应该尽量被消除。一个可行的消除方法是替换掉依赖类（测试替换），也就是说我们可以使用替身来替换掉真正的依赖对象。\n \n-### 2.1\\. Target and challenge of unit testing\n+### 2.2\\. 几种不同的测试类\n \n-A unit test should test a class in isolation. Side effects from other classes or the system should be eliminated if possible. To eliminate these side effects you have to replace dependencies to other classes. This can be done via using replacements for the real dependencies.\n+_dummy object_ 做为参数传递给方法但是绝对不会被使用。譬如说，它里面的方法不会被调用。这种对象是为了在方法参数中起到占位的作用。\n \n-### 2.2\\. Classifications of different test classes\n+_Fake_ 是真正接口或抽象类的实现体，但给对象内部实现很简单。譬如说，它存在内存中而不是真正的数据库中。（译者注：_Fake_ 实现了真正的逻辑，但它的存在只是为了测试，而不适合于用在产品中。）\n \n-A _dummy object_ is passed around but never used, i.e., its methods are never called. Such an object can for example be used to fill the parameter list of a method.\n+_stub_ 类是依赖类的部分方法实现，而这些方法在你测试类和接口的时候会被用到，也就是说 _stub_ 类在测试中会被实例化。_stub_ 类会回应任何外部测试的调用。_stub_ 类有时候还会记录调用的一些信息。\n \n-_Fake_ objects have working implementations, but are usually simplified. For example, they use an in memory database and not a real database.\n+_mock object_ 是指类或者接口的模拟实现，你可以自定义里面方法被调用后的返回值。\n \n-A _stub_ class is an partial implementation for an interface or class with the purpose of using an instance of this stub class during testing. Stubs usually do responding at all to anything outside what’s programmed in for the test. Stubs may also record information about calls\n+测试替代技术能够在测试中模拟测试类以外对象。因此你可以验证测试类是否响应正常。譬如说，你可以验证在 Mock 对象的某一个方法是否被调用。这可以确保隔离了外部依赖的干扰只测试测试类。\n \n-A _mock object_ is a dummy implementation for an interface or a class in which you define the output of certain method calls.\n+我们选择 Mock 对象的原因是因为 Mock 对象只需要少量代码的配置。\n \n-Test doubles can be passed to other objects which are tested. Your tests can validate that the class reacts correctly during tests. For example, you can validate if certain methods on the mock object were called. This helps to ensure that you only test the class while running tests and that your tests are not affected by any side effects.\n+### 2.3\\. Mock 对象的产生\n \n-Mock objects are typically configured. Mock objects typically require less code to configure and should therefore be preferred.\n+你可以手动创建一个 Mock 对象或者使用 Mock 框架来模拟这些类，Mock 框架允许你在 Runtime 的时候创建 Mock 对象并且定义它的行为。\n \n-### 2.3\\. Mock object generation\n+一个典型的例子是把 Mock 对象模拟成数据的提供者。在正式的生产环境中它会被实现用来连接数据源。但是我们在测试的时候 Mock 对象将会模拟成数据提供者来确保我们的测试环境始终是相同的。\n \n-You can create mock objects manually (via code) or use a mock framework to simulate these classes. Mock frameworks allow you to create mock objects at runtime and define their behavior.\n+Mock 对象可以被提供来进行测试。因此，我们测试的类应该避免任何外部数据的强引用。\n \n-The classical example for a mock object is a data provider. In production an implementation to connect to the real data source is used. But for testing a mock object simulates the data source and ensures that the test conditions are always the same.\n+通过 Mock 对象或者 Mock 框架，我们可以测试代码中期望的行为。譬如说，验证只有某个存在 Mock 对象的方法是否被调用了。\n \n-These mock objects can be provided to the class which is tested. Therefore, the class to be tested should avoid any hard dependency on external data.\n+### 2.4\\. 使用 Mockito 生成 Mock 对象\n \n-Mocking or mock frameworks allows testing the expected interaction with the mock object. You can, for example, validate that only certain methods have been called on the mock object.\n+_Mockito_ 是一个流行 mock 框架，并且可以配置 JUnit 一起使用。Mockito 允许你创建和配置 mock 对象。使用 Mockito 可以很简单的为存在外部依赖的代码编写测试脚本。","path":"TODO/Unit-tests-with-Mockito.md","position":68,"original_position":68,"commit_id":"46ca03c64f1bcd477362c29cb8dff971ae0cd99e","original_commit_id":"46ca03c64f1bcd477362c29cb8dff971ae0cd99e","user":{"login":"hackerkevin","id":9269837,"avatar_url":"https://avatars.githubusercontent.com/u/9269837?v=3","gravatar_id":"","url":"https://api.github.com/users/hackerkevin","html_url":"https://github.com/hackerkevin","followers_url":"https://api.github.com/users/hackerkevin/followers","following_url":"https://api.github.com/users/hackerkevin/following{/other_user}","gists_url":"https://api.github.com/users/hackerkevin/gists{/gist_id}","starred_url":"https://api.github.com/users/hackerkevin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hackerkevin/subscriptions","organizations_url":"https://api.github.com/users/hackerkevin/orgs","repos_url":"https://api.github.com/users/hackerkevin/repos","events_url":"https://api.github.com/users/hackerkevin/events{/privacy}","received_events_url":"https://api.github.com/users/hackerkevin/received_events","type":"User","site_admin":false},"body":"can be used in conjunction with JUnit --> 可以和JUnit结合起来使用","created_at":"2016-07-13T07:36:01Z","updated_at":"2016-07-13T07:36:01Z","html_url":"https://github.com/xitu/gold-miner/pull/515#discussion_r70576887","pull_request_url":"https://api.github.com/repos/xitu/gold-miner/pulls/515","_links":{"self":{"href":"https://api.github.com/repos/xitu/gold-miner/pulls/comments/70576887"},"html":{"href":"https://github.com/xitu/gold-miner/pull/515#discussion_r70576887"},"pull_request":{"href":"https://api.github.com/repos/xitu/gold-miner/pulls/515"}}},"pull_request":{"url":"https://api.github.com/repos/xitu/gold-miner/pulls/515","id":76940757,"html_url":"https://github.com/xitu/gold-miner/pull/515","diff_url":"https://github.com/xitu/gold-miner/pull/515.diff","patch_url":"https://github.com/xitu/gold-miner/pull/515.patch","issue_url":"https://api.github.com/repos/xitu/gold-miner/issues/515","number":515,"state":"open","locked":false,"title":"完成 Unit-tests-with-Mockito 的翻译","user":{"login":"edvardHua","id":4105217,"avatar_url":"https://avatars.githubusercontent.com/u/4105217?v=3","gravatar_id":"","url":"https://api.github.com/users/edvardHua","html_url":"https://github.com/edvardHua","followers_url":"https://api.github.com/users/edvardHua/followers","following_url":"https://api.github.com/users/edvardHua/following{/other_user}","gists_url":"https://api.github.com/users/edvardHua/gists{/gist_id}","starred_url":"https://api.github.com/users/edvardHua/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/edvardHua/subscriptions","organizations_url":"https://api.github.com/users/edvardHua/orgs","repos_url":"https://api.github.com/users/edvardHua/repos","events_url":"https://api.github.com/users/edvardHua/events{/privacy}","received_events_url":"https://api.github.com/users/edvardHua/received_events","type":"User","site_admin":false},"body":"@antonia0912 羊哥，完成翻译了。\r\n\r\n里面一些专有名词我保留英文，IBM有一篇文章有着几个概念的解释，大家可以看看\r\n\r\n[如何通过测试替代（Test Doubles）合理隔离单元测试以提高单元测试效率](http://www.ibm.com/developerworks/cn/java/j-lo-TestDoubles/)","created_at":"2016-07-11T11:50:23Z","updated_at":"2016-07-13T07:36:01Z","closed_at":null,"merged_at":null,"merge_commit_sha":"0f9f9632f047d99ff2071ce3a23eaa8ca48de574","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/xitu/gold-miner/pulls/515/commits","review_comments_url":"https://api.github.com/repos/xitu/gold-miner/pulls/515/comments","review_comment_url":"https://api.github.com/repos/xitu/gold-miner/pulls/comments{/number}","comments_url":"https://api.github.com/repos/xitu/gold-miner/issues/515/comments","statuses_url":"https://api.github.com/repos/xitu/gold-miner/statuses/46ca03c64f1bcd477362c29cb8dff971ae0cd99e","head":{"label":"edvardHua:unit-test-translate","ref":"unit-test-translate","sha":"46ca03c64f1bcd477362c29cb8dff971ae0cd99e","user":{"login":"edvardHua","id":4105217,"avatar_url":"https://avatars.githubusercontent.com/u/4105217?v=3","gravatar_id":"","url":"https://api.github.com/users/edvardHua","html_url":"https://github.com/edvardHua","followers_url":"https://api.github.com/users/edvardHua/followers","following_url":"https://api.github.com/users/edvardHua/following{/other_user}","gists_url":"https://api.github.com/users/edvardHua/gists{/gist_id}","starred_url":"https://api.github.com/users/edvardHua/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/edvardHua/subscriptions","organizations_url":"https://api.github.com/users/edvardHua/orgs","repos_url":"https://api.github.com/users/edvardHua/repos","events_url":"https://api.github.com/users/edvardHua/events{/privacy}","received_events_url":"https://api.github.com/users/edvardHua/received_events","type":"User","site_admin":false},"repo":{"id":52771801,"name":"gold-miner","full_name":"edvardHua/gold-miner","owner":{"login":"edvardHua","id":4105217,"avatar_url":"https://avatars.githubusercontent.com/u/4105217?v=3","gravatar_id":"","url":"https://api.github.com/users/edvardHua","html_url":"https://github.com/edvardHua","followers_url":"https://api.github.com/users/edvardHua/followers","following_url":"https://api.github.com/users/edvardHua/following{/other_user}","gists_url":"https://api.github.com/users/edvardHua/gists{/gist_id}","starred_url":"https://api.github.com/users/edvardHua/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/edvardHua/subscriptions","organizations_url":"https://api.github.com/users/edvardHua/orgs","repos_url":"https://api.github.com/users/edvardHua/repos","events_url":"https://api.github.com/users/edvardHua/events{/privacy}","received_events_url":"https://api.github.com/users/edvardHua/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/edvardHua/gold-miner","description":"掘金翻译计划，翻译掘金上优质的英文文章","fork":true,"url":"https://api.github.com/repos/edvardHua/gold-miner","forks_url":"https://api.github.com/repos/edvardHua/gold-miner/forks","keys_url":"https://api.github.com/repos/edvardHua/gold-miner/keys{/key_id}","collaborators_url":"https://api.github.com/repos/edvardHua/gold-miner/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/edvardHua/gold-miner/teams","hooks_url":"https://api.github.com/repos/edvardHua/gold-miner/hooks","issue_events_url":"https://api.github.com/repos/edvardHua/gold-miner/issues/events{/number}","events_url":"https://api.github.com/repos/edvardHua/gold-miner/events","assignees_url":"https://api.github.com/repos/edvardHua/gold-miner/assignees{/user}","branches_url":"https://api.github.com/repos/edvardHua/gold-miner/branches{/branch}","tags_url":"https://api.github.com/repos/edvardHua/gold-miner/tags","blobs_url":"https://api.github.com/repos/edvardHua/gold-miner/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/edvardHua/gold-miner/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/edvardHua/gold-miner/git/refs{/sha}","trees_url":"https://api.github.com/repos/edvardHua/gold-miner/git/trees{/sha}","statuses_url":"https://api.github.com/repos/edvardHua/gold-miner/statuses/{sha}","languages_url":"https://api.github.com/repos/edvardHua/gold-miner/languages","stargazers_url":"https://api.github.com/repos/edvardHua/gold-miner/stargazers","contributors_url":"https://api.github.com/repos/edvardHua/gold-miner/contributors","subscribers_url":"https://api.github.com/repos/edvardHua/gold-miner/subscribers","subscription_url":"https://api.github.com/repos/edvardHua/gold-miner/subscription","commits_url":"https://api.github.com/repos/edvardHua/gold-miner/commits{/sha}","git_commits_url":"https://api.github.com/repos/edvardHua/gold-miner/git/commits{/sha}","comments_url":"https://api.github.com/repos/edvardHua/gold-miner/comments{/number}","issue_comment_url":"https://api.github.com/repos/edvardHua/gold-miner/issues/comments{/number}","contents_url":"https://api.github.com/repos/edvardHua/gold-miner/contents/{+path}","compare_url":"https://api.github.com/repos/edvardHua/gold-miner/compare/{base}...{head}","merges_url":"https://api.github.com/repos/edvardHua/gold-miner/merges","archive_url":"https://api.github.com/repos/edvardHua/gold-miner/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/edvardHua/gold-miner/downloads","issues_url":"https://api.github.com/repos/edvardHua/gold-miner/issues{/number}","pulls_url":"https://api.github.com/repos/edvardHua/gold-miner/pulls{/number}","milestones_url":"https://api.github.com/repos/edvardHua/gold-miner/milestones{/number}","notifications_url":"https://api.github.com/repos/edvardHua/gold-miner/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/edvardHua/gold-miner/labels{/name}","releases_url":"https://api.github.com/repos/edvardHua/gold-miner/releases{/id}","deployments_url":"https://api.github.com/repos/edvardHua/gold-miner/deployments","created_at":"2016-02-29T07:16:17Z","updated_at":"2016-02-29T07:09:25Z","pushed_at":"2016-07-11T11:46:07Z","git_url":"git://github.com/edvardHua/gold-miner.git","ssh_url":"git@github.com:edvardHua/gold-miner.git","clone_url":"https://github.com/edvardHua/gold-miner.git","svn_url":"https://github.com/edvardHua/gold-miner","homepage":"http://gold.xitu.io","size":13530,"stargazers_count":0,"watchers_count":0,"language":null,"has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"xitu:master","ref":"master","sha":"97c217e32d40581b71807888e5e8368a1750142c","user":{"login":"xitu","id":10482599,"avatar_url":"https://avatars.githubusercontent.com/u/10482599?v=3","gravatar_id":"","url":"https://api.github.com/users/xitu","html_url":"https://github.com/xitu","followers_url":"https://api.github.com/users/xitu/followers","following_url":"https://api.github.com/users/xitu/following{/other_user}","gists_url":"https://api.github.com/users/xitu/gists{/gist_id}","starred_url":"https://api.github.com/users/xitu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/xitu/subscriptions","organizations_url":"https://api.github.com/users/xitu/orgs","repos_url":"https://api.github.com/users/xitu/repos","events_url":"https://api.github.com/users/xitu/events{/privacy}","received_events_url":"https://api.github.com/users/xitu/received_events","type":"Organization","site_admin":false},"repo":{"id":45512989,"name":"gold-miner","full_name":"xitu/gold-miner","owner":{"login":"xitu","id":10482599,"avatar_url":"https://avatars.githubusercontent.com/u/10482599?v=3","gravatar_id":"","url":"https://api.github.com/users/xitu","html_url":"https://github.com/xitu","followers_url":"https://api.github.com/users/xitu/followers","following_url":"https://api.github.com/users/xitu/following{/other_user}","gists_url":"https://api.github.com/users/xitu/gists{/gist_id}","starred_url":"https://api.github.com/users/xitu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/xitu/subscriptions","organizations_url":"https://api.github.com/users/xitu/orgs","repos_url":"https://api.github.com/users/xitu/repos","events_url":"https://api.github.com/users/xitu/events{/privacy}","received_events_url":"https://api.github.com/users/xitu/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/xitu/gold-miner","description":"掘金翻译计划，翻译掘金上优质的英文文章","fork":false,"url":"https://api.github.com/repos/xitu/gold-miner","forks_url":"https://api.github.com/repos/xitu/gold-miner/forks","keys_url":"https://api.github.com/repos/xitu/gold-miner/keys{/key_id}","collaborators_url":"https://api.github.com/repos/xitu/gold-miner/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/xitu/gold-miner/teams","hooks_url":"https://api.github.com/repos/xitu/gold-miner/hooks","issue_events_url":"https://api.github.com/repos/xitu/gold-miner/issues/events{/number}","events_url":"https://api.github.com/repos/xitu/gold-miner/events","assignees_url":"https://api.github.com/repos/xitu/gold-miner/assignees{/user}","branches_url":"https://api.github.com/repos/xitu/gold-miner/branches{/branch}","tags_url":"https://api.github.com/repos/xitu/gold-miner/tags","blobs_url":"https://api.github.com/repos/xitu/gold-miner/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/xitu/gold-miner/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/xitu/gold-miner/git/refs{/sha}","trees_url":"https://api.github.com/repos/xitu/gold-miner/git/trees{/sha}","statuses_url":"https://api.github.com/repos/xitu/gold-miner/statuses/{sha}","languages_url":"https://api.github.com/repos/xitu/gold-miner/languages","stargazers_url":"https://api.github.com/repos/xitu/gold-miner/stargazers","contributors_url":"https://api.github.com/repos/xitu/gold-miner/contributors","subscribers_url":"https://api.github.com/repos/xitu/gold-miner/subscribers","subscription_url":"https://api.github.com/repos/xitu/gold-miner/subscription","commits_url":"https://api.github.com/repos/xitu/gold-miner/commits{/sha}","git_commits_url":"https://api.github.com/repos/xitu/gold-miner/git/commits{/sha}","comments_url":"https://api.github.com/repos/xitu/gold-miner/comments{/number}","issue_comment_url":"https://api.github.com/repos/xitu/gold-miner/issues/comments{/number}","contents_url":"https://api.github.com/repos/xitu/gold-miner/contents/{+path}","compare_url":"https://api.github.com/repos/xitu/gold-miner/compare/{base}...{head}","merges_url":"https://api.github.com/repos/xitu/gold-miner/merges","archive_url":"https://api.github.com/repos/xitu/gold-miner/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/xitu/gold-miner/downloads","issues_url":"https://api.github.com/repos/xitu/gold-miner/issues{/number}","pulls_url":"https://api.github.com/repos/xitu/gold-miner/pulls{/number}","milestones_url":"https://api.github.com/repos/xitu/gold-miner/milestones{/number}","notifications_url":"https://api.github.com/repos/xitu/gold-miner/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/xitu/gold-miner/labels{/name}","releases_url":"https://api.github.com/repos/xitu/gold-miner/releases{/id}","deployments_url":"https://api.github.com/repos/xitu/gold-miner/deployments","created_at":"2015-11-04T03:29:13Z","updated_at":"2016-07-13T07:20:28Z","pushed_at":"2016-07-13T06:31:11Z","git_url":"git://github.com/xitu/gold-miner.git","ssh_url":"git@github.com:xitu/gold-miner.git","clone_url":"https://github.com/xitu/gold-miner.git","svn_url":"https://github.com/xitu/gold-miner","homepage":"http://gold.xitu.io","size":14587,"stargazers_count":2342,"watchers_count":2342,"language":null,"has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":447,"mirror_url":null,"open_issues_count":29,"forks":447,"open_issues":29,"watchers":2342,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/xitu/gold-miner/pulls/515"},"html":{"href":"https://github.com/xitu/gold-miner/pull/515"},"issue":{"href":"https://api.github.com/repos/xitu/gold-miner/issues/515"},"comments":{"href":"https://api.github.com/repos/xitu/gold-miner/issues/515/comments"},"review_comments":{"href":"https://api.github.com/repos/xitu/gold-miner/pulls/515/comments"},"review_comment":{"href":"https://api.github.com/repos/xitu/gold-miner/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/xitu/gold-miner/pulls/515/commits"},"statuses":{"href":"https://api.github.com/repos/xitu/gold-miner/statuses/46ca03c64f1bcd477362c29cb8dff971ae0cd99e"}}}}
{"actor":{"display_login":"hackerkevin"}}