Configure the unit test for Android using both Powermock and Robolectric

Source: Internet
Author: User
<span id="Label3"></p>Robolectric a configuration tutorial on the web, but I used it to configure it and found<span style="line-height: 1.6;"><span style="line-height: 1.6;">Use</span></span><span style="line-height: 1.6;"><span style="line-height: 1.6;">the Mockito.spy function will appear Exception. </span></span>Later on the Powermock official online found another tutorial, which said that using Powermockrule is not reliable, to use the Powermock 1.6.0 introduced by the new @powermockrunnerdelegate annotation to configure The specific configuration file is as Follows: module inside the Build.gradle add dependency:<pre><pre> <span style="color: #008080;" "> 1 </span> <span style=" color: #000000; ">dependencies {</span> <span style=" Color : #008080; "> 2 </span> <span style="color: #000000;" "> ... </span> <span style="color: #008080;" "3 < span> <span style=" color: #008080; "> 4 </span> testcompile" org.robolectric: robolectric:3.0 "<span style=" color: #008080; "> 5 </span> testcompile ' org.mockito:mockito-core:1.10.19 ' <span style="color: #008080;"> 6 </span> testcompile ' junit:junit:4.12 ' <span style="color: #008080;"> 7 </span> testcompile "org.powermock:powerm ock-module-junit4:1.6.4 "<span style=" color: #008080; " 8 < span> testcompile" org.powermock: powermock-module-junit4-rule:1.6.4 "<span style=" color: #008080; " 9 < span> testcompile" org.powermock: powermock-api-mockito:1.6.4 "<span style=" color: #008080; ">10 </span> testcompile" org.powermock: powermock-classloading-xstream:1.6.4 "<span style=" color: #008080; ">11 </span>} </span></span></span></pre></pre>The following robolectrictest base class is inherited, and can be tested normally using Powermock and Robolectric.<pre><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">org.junit.runner.RunWith;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">org.powermock.core.classloader.annotations.PowerMockIgnore;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">org.powermock.modules.junit4.PowerMockRunner;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">org.powermock.modules.junit4.PowerMockRunnerDelegate;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">org.robolectric.RobolectricGradleTestRunner;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">org.robolectric.annotation.Config;</span></span><span style="color: #008000;"><span style="color: #008000;">/**</span></span><span style="color: #008000;"><span style="color: #008000;">* Base class extended by every robolectric test in this project. * <p/> * You can use Powermock together with R Obolectric. </span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span><span style="color: #000000;"><span style="color: #000000;">@RunWith (powermockrunner.</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;">) @PowerMockRunnerDelegate (robolectricgradletestrunner.</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;">) @Config (constants</span></span>= Buildconfig.<span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;">, the SDK</span></span>= 21<span style="color: #000000;"><span style="color: #000000;">) @PowerMockIgnore ({</span></span>"org.mockito.*", "org.robolectric.*", "android.*"<span style="color: #000000;"><span style="color: #000000;">})</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Abstract</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;">robolectrictest {}</span></span></pre><span style="line-height: 1.5;"><span style="line-height: 1.5;">Remember to import the Powermock version of the Mockito api, otherwise there will be strange bugs. </span></span>Like what<pre><pre><span style="color: #0000ff;">Import</span> <span style="color: #0000ff;">Static</span> <span style="color: #000000;">org.powermock.api.mockito.PowerMockito.mock;</span> <span style="color: #0000ff;">Import</span> <span style="color: #0000ff;">Static</span> <span style="color: #000000;">org.powermock.api.mockito.PowerMockito.spy;</span> <span style="color: #0000ff;">Import</span> <span style="color: #0000ff;">Static</span> org.powermock.api.mockito.PowerMockito.when;</pre></pre>Sometimes robolectric will prompt you not to find manifest.xml, this time general clean Project. Robolectric 3.0 temporarily does not support the loading of jnilibs, if your code uses jnilibs, you can only use Android instrumentation tests, if you like me<span style="font-family: Consolas, ‘Liberation Mono‘, Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; color: #000080; font-weight: bold;"><span style="font-family: Consolas, ‘Liberation Mono‘, Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; color: #000080; font-weight: bold;">extends</span></span><span style="font-family: Consolas, ‘Liberation Mono‘, Menlo, Courier, monospace; font-size: 13px; background-color: #ffffff; line-height: 1.6;"><span style="font-family: Consolas, ‘Liberation Mono‘, Menlo, Courier, monospace; font-size: 13px; background-color: #ffffff; line-height: 1.6;">android.app.Application</span></span><span style="font-size: 13px; background-color: #ffffff; line-height: 1.6;"><span style="font-size: 13px; background-color: #ffffff; line-height: 1.6;"><span style="font-family: 微软雅黑;">to create your own application class and write code in the application class that needs to call jnilibs, you can skip the code in unit test using the following method. </span></span></span><pre><span style="color: #000000;"><span style="color: #000000;">@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">onCreate () {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span><span style="color: #000000;"><span style="color: #000000;">. OnCreate (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span><span style="color: #000000;"><span style="color: #000000;">(isjunittest ()) {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">omit the initialization of the library containing the Jnilibs</span></span>}<span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span><span style="color: #000000;"><span style="color: #000000;"> { </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">perform a normal initialization</span></span><span style="color: #000000;"><span style="color: #000000;"> }}</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Boolean</span></span><span style="color: #000000;"><span style="color: #000000;">isjunittest () {stacktraceelement[] stackTrace</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">throwable (). getstacktrace (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span><span style="color: #000000;"><span style="color: #000000;">(stacktraceelement Element:stacktrace) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(element.getclassname (). StartsWith ("org.junit."<span style="color: #000000;"><span style="color: #000000;">)) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">true</span></span><span style="color: #000000;"><span style="color: #000000;">; } } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">false</span></span><span style="color: #000000;"><span style="color: #000000;">;}</span></span></pre><p><p></p></p><p><p> Configure the unit test for Android using both Powermock and robolectric </p> </p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.