{"id":423270,"date":"2021-03-30T23:35:23","date_gmt":"2021-03-30T15:35:23","guid":{"rendered":"http:\/\/4563.org\/?p=423270"},"modified":"2021-03-30T23:35:23","modified_gmt":"2021-03-30T15:35:23","slug":"%e4%b8%80%e4%b8%aa%e9%80%82%e7%94%a8%e4%ba%8e-android-jvm-%e7%9a%84%e5%86%92%e7%83%9f%e5%8d%95%e6%b5%8b%e7%94%9f%e6%88%90%e5%99%a8-randunit","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=423270","title":{"rendered":"\u4e00\u4e2a\u9002\u7528\u4e8e android\/jvm \u7684\u5192\u70df\u5355\u6d4b\u751f\u6210\u5668 randunit"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u4e00\u4e2a\u9002\u7528\u4e8e android\/jvm \u7684\u5192\u70df\u5355\u6d4b\u751f\u6210\u5668 randunit               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : williamfzc <\/span>  <span><i><\/i> 4<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<h2>\u80cc\u666f<\/h2>\n<p>\u5728\u6301\u7eed\u4ea4\u4ed8\u8d8a\u6765\u8d8a\u6d41\u884c\u7684\u4eca\u5929\uff0c\u5355\u6d4b\u4f5c\u4e3a\u4fdd\u969c CI \u8d28\u91cf\u7684\u91cd\u8981\u4e00\u73af\u4e5f\u5f00\u59cb\u5728\u56fd\u5185\u88ab\u91cd\u89c6\u8d77\u6765\u3002<\/p>\n<p>\u4e0d\u8fc7\u5728\u5355\u6d4b\u4e0a\u5927\u5bb6\u7684\u6001\u5ea6\u8fd8\u662f\u6bd4\u8f83\u77db\u76fe\u7684\uff0c\u62c5\u5fc3\u7684\u4e8b\u60c5\u4e3b\u8981\u6709\u4e24\u4e2a\uff1a<\/p>\n<ul>\n<li>\u4ece 0 \u5230 1 \u7684\u7b2c\u4e00\u6b65\u4e0d\u77e5\u9053\u4ece\u4f55\u4e0b\u624b<\/li>\n<li>\u4e07\u4e00\u82b1\u5927\u529b\u6c14\u505a\u4e86\u4e4b\u540e\u53d1\u73b0\u6ca1\u4ec0\u4e48\u7528\u600e\u4e48\u529e<\/li>\n<\/ul>\n<p>\u4f46\u662f\u7f3a\u5c11\u8fd9\u4e00\u73af\uff0c\u5728\u6574\u4e2a devops \u6d41\u7a0b\u4e2d\u5f88\u591a\u7f16\u8bd1\u65f6\u4e25\u91cd\u95ee\u9898\u4f1a\u88ab\u5ef6\u7f13\u5230\u8fd0\u884c\u65f6\u66b4\u9732\uff0c\u8fd9\u5bf9\u4e8e\u9879\u76ee\u6548\u7387\u963b\u788d\u4e5f\u4e0d\u5c0f\u3002\u3002<\/p>\n<p>\u4e8e\u662f\u5c31\u6709\u4e86\u7075\u611f\u6765\u6e90\uff1a<\/p>\n<ul>\n<li>\u81ea\u52a8\u751f\u6210\u4e00\u7cfb\u5217\u5192\u70df\u7ea7\u522b\u5355\u6d4b\u7528\u4f8b\uff0c\u5e76\u80fd\u53d1\u73b0\u4e25\u91cd\u95ee\u9898<\/li>\n<li>\u63a5\u5165\u6210\u672c\u4f4e\uff0c\u80fd\u65e0\u75db\u4e0e\u73b0\u6709\u6d41\u7a0b\u7ed3\u5408<\/li>\n<li>\u80fd\u5bf9 android \u751f\u6548\uff08\u6211\u53f8\u91cd\u79fb\u52a8\u7aef<\/li>\n<\/ul>\n<h2>\u505a\u4e86\u4ec0\u4e48<\/h2>\n<p>RandUnit \u53d6\u4e49\u81ea <code>Random UnitTest<\/code>\uff0c\u4ed6\u4f1a\uff1a<\/p>\n<ul>\n<li>\u6839\u636e\u63d0\u4f9b\u7684\u5305\u540d\u6216\u5165\u53e3\u7c7b\uff0c\u641c\u7d22\u6240\u6709\u76f8\u5173\u7684\u5f85\u6d4b\u8bd5\u7c7b\u4e0e\u65b9\u6cd5<\/li>\n<li>\u6839\u636e\u641c\u7d22\u7ed3\u679c\uff0c\u4e3a\u6bcf\u4e2a\u65b9\u6cd5\u751f\u6210\u4e00\u7cfb\u5217 statements \u7528\u4e8e\u6d4b\u8bd5<\/li>\n<li>\u50cf\u5e38\u89c4\u5355\u6d4b\u6d41\u7a0b\u4e00\u822c\uff0c\u5728 junit \u4e0a\u8fd0\u884c\u8fd9\u4e9b statements\uff0c\u5f97\u5230\u6d4b\u8bd5\u7ed3\u679c<\/li>\n<\/ul>\n<p>\u800c\u8fd9\u4e00\u5207\u53ea\u9700\u8981\u4e00\u6b21\u7b80\u5355\u7684\u590d\u5236\u7c98\u8d34\uff1a<\/p>\n<pre><code>import com.williamfzc.randunit.env.NormalTestEnv import com.williamfzc.randunit.models.StatementModel import com.williamfzc.randunit.scanner.ScannerConfig import org.junit.Test import org.junit.runner.RunWith import org.junit.runners.Parameterized  @RunWith(Parameterized::class) class MinExampleTest(private val statementModel: StatementModel) {     companion object {         private val testEnv = NormalTestEnv()         private const val packageName = \"com.your.package\"         private val cases by lazy {             val scannerConfig = ScannerConfig()             scannerConfig.includeFilter.add(packageName)              RandUnit.collectStatementsWithPackage(packageName, scannerConfig)         }          @JvmStatic         @Parameterized.Parameters(name = \"{0}\")         fun data(): Collection&lt;StatementModel&gt; {             return cases         }     }      @Test     fun runStatements() {         testEnv.runStatementInSandbox(statementModel)     } } <\/code><\/pre>\n<p>\u7531\u4e8e\u5b83\u662f\u5408\u6cd5\u7684 junit \u7528\u4f8b\uff0c\u6240\u4ee5\u4f60\u53ef\u4ee5\u5728 ide \u91cc\u76f4\u63a5\u8fd0\u884c\u5b83\u3002\u76f4\u63a5 run with coverage \u7684\u8bdd\uff1a<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" referrerpolicy=\"no-referrer\" rel=\"noreferrer\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2021\/04\/20210404_6069457c5d1a9.jpg\" alt=\"\u4e00\u4e2a\u9002\u7528\u4e8e android\/jvm \u7684\u5192\u70df\u5355\u6d4b\u751f\u6210\u5668 randunit\" \/><\/p>\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u5b83\u5e2e\u4f60\u751f\u6210\u4e86 500+\u6761\u7528\u4f8b\u5e76\u6267\u884c\u4e86\u3002<\/p>\n<h2>\u89e3\u51b3\u4e86\u5565<\/h2>\n<p>\u518d\u56de\u5934\u770b\u770b\u662f\u4e0d\u662f\u89e3\u51b3\u4e86\u4e0a\u9762\u7684\u4e09\u4e2a\u95ee\u9898\uff1a<\/p>\n<ul>\n<li>\u81ea\u52a8\u751f\u6210\uff1a\u76ee\u524d\u7684\u7b56\u7565\u662f\u5728\u6bcf\u6b21\u7f16\u8bd1\u540e\u81ea\u52a8\u641c\u7d22\u751f\u6210\u4e00\u7cfb\u5217 junit \u7528\u4f8b\u5e76\u6267\u884c\uff0c\u6709\u4ee3\u7801\u53d8\u66f4\u4e5f\u65e0\u9700\u6539\u7528\u4f8b<\/li>\n<li>\u63a5\u5165\u6210\u672c\u4f4e\uff1a\u4e00\u6b21\u7c98\u8d34\u5373\u53ef\u63a5\u5165\u3002\u9664\u6b64\u4e4b\u5916\uff0c\u56e0\u4e3a\u4ed6\u662f\u5408\u6cd5 junit \u7528\u4f8b\uff0c\u90a3\u81ea\u7136\u4e5f\u53ef\u4ee5\u50cf\u6b63\u5e38\u4f7f\u7528 junit \u7528\u4f8b\u4e00\u6837\u7528\u5b83\uff0c\u4f8b\u5982\u5728 CI \u73af\u5883\u91cc<\/li>\n<li>\u5bf9 android \u751f\u6548\uff1a\u5f97\u76ca\u4e8e robolectric \u7684\u652f\u6301, randunit \u80fd\u591f\u5f88\u597d\u5730\u88ab\u5e94\u7528\u5230 android \u9879\u76ee\u4e2d\u3002\u4e0d\u8fc7\u4e0a\u9762\u7684\u4ee3\u7801\u9700\u8981\u6709\u5fae\u8c03\uff0c\u611f\u5174\u8da3\u53ef\u4ee5\u770b\u770b\u9879\u76ee\u4e3b\u9875<\/li>\n<\/ul>\n<p>\u50cf\u8fd9\u79cd\u95ee\u9898\uff1a<\/p>\n<pre><code>override fun getCastOptions(context: Context?): CastOptions? {      \/\/ oh, you import a non-existed class here!     \/\/ it should cause a ClassNotFoundException     Class.forName(\"import unknown class here!\")      ... } <\/code><\/pre>\n<p>\u5c31\u4e0d\u4f1a\u88ab\u9057\u6f0f\u5230\u96c6\u6210\u4e4b\u540e\u5566\uff1a<\/p>\n<pre><code>WARNING: error happened inside sandbox: java.lang.reflect.InvocationTargetException  java.lang.ClassNotFoundException: import unknown class here!   at org.robolectric.internal.bytecode.SandboxClassLoader.getByteCode(SandboxClassLoader.java:158) <\/code><\/pre>\n<h2>\u94fe\u63a5<\/h2>\n<ul>\n<li>\u5177\u4f53\u4ecb\u7ecd\u4e0e\u9879\u76ee\u4e3b\u9875\uff1a https:\/\/github.com\/williamfzc\/randunit<\/li>\n<li>\u63a5\u5165\u770b\u8fd9\uff1a https:\/\/github.com\/williamfzc\/randunit#installation<\/li>\n<\/ul><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>1<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"5616193\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : hantsy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8fd9\u4e2a\u4e0d\u9519\uff0c\u548c\u56fd\u5185\u6ee1\u5927\u8857\u7684 UI \u6362\u80a4\u5f00\u6e90\u9879\u76ee\u6bd4\uff0c\u8fd9\u7b97\u662f\u771f\u6b63\u6709\u70b9\u4ef7\u503c\u7684\u5f00\u6e90\u9879\u76ee\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u4e2a\u9002\u7528\u4e8e android\/jvm&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/423270"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=423270"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/423270\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=423270"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=423270"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=423270"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}