Sunday, 15 April 2012

unit testing - Robolectric tests running in Android Studio but not on the command line -


I am trying to run unit tests using the robollector; They go fine under the Android studio, but the exact same test fails when running in the command line - which is a great deal, I should be able to run them on a continuous integration platform, not just IDE

I suspect that I'm calling some command line logic (which is called a classpath or something similar) or wrong work - otherwise this test will not do anything from the Android studio, some relevant details; Test looks like this:

  @RunWith (RobolectricTestRunner.class) @Config (manifest = "application / src / main / AndroidManifest.xml", resourceDir = "Race", emulateSdk = 19) public class Splash Aktiwwaittst {@Test public award Test activity () (Splash Aktiwwaiti Splash Aktiwhi = New Splash activity (); string appName = splashActivity.getString (R.string.app_name); // here, line 20 assertEquals (appName, " App ");}}  

As mentioned above, it runs fine in Android studio (tester) By right-clicking the original file and selecting run 'splash activivist' ), but when it is running the command line with this low stack trace, line here fails marked with:

  android.content.res.Resources $ NotFoundException: unknown resource 2131492893 org .robolectric.shadows.ShadowAssetManager.get and Resolve (shadowAssetManager.java.309) org.robolectric On shadows.sssetManager.getResourceText (ShadowAssetManager.java:69) on android.content.res.AssetManager.getResourceText (AssetManager.java) on Android. content.res.Resources.getText org.robolectric.shadows.ShadowResources.getText (ShadowResources.java:361) to (Resources.java:240) on android.content.res.Resources.getText (Resources.java) on android.content .res.Resources.getString on org.robolectric.shadows.ShadowContext.getString (ShadowContext.java39) on Org.robolectric.sh (Snsadnkjawa: 330) adows.ShadowContextWrapper.getString (ShadowContextWrapper.java:69) android.content. Context.getString (Context.java) at path.to.myApp.activities.SplashActivityTest.testActivity (SplashActivityTest.java20) at // ... and if so ...  

I'm using it to run from the command line (note that I'm using the griddle wrapper here and in the Android studio I'm using Android Studio 1.1.0 ; Gradell version << code> code> 2.3 , the version of Roboclick is 3.0-SNAPSHOT and the graml plugin version of the RoboticTrack 1.0.1

It is known that this is a known issue, while running a test from the command line, the resources of a project are seen in the wrong directory, here is a problem; Now performing as a solution to write a custom test runner:

  public class RobolectricGradleTestRunner extends RobolectricTestRunner {public RobolectricGradleTestRunner (? Class & lt; & gt; testClass) throws InitializationError {super ( Testclass); String build variant = (BuildConfig.FLAVOR.isEmpty ()? "": BuildConfig.FLAVOR + "/") + BuildConfig.BUILD_TYPE; String intermediates path = buildconfig.clash.getresource (""). ToString () Replace ("File:", ""); Intermediate path = intermediate path. Substring (0, intermediate path. Indochoff ("/ square")); System.setProperty ("android.package", BuildConfig.APPLICATION_ID); System.setProperty ("android.manifest", Intermediate Path + "/ Manifest / Full /" + BuildVirent + "/ AndroidManffast.xml"); System.setProperty ("android.resources", Intermediate Path + "/ Ridge /" + BuildVirient); System.setProperty ("android.assets", Intermediate Path + "/ Assets /" + buildVariant); }}  

Testing cases @Config annotations instead of custom test runner

  

In addition, it is recommended to display a clean @RunWith (RobolectricGradleTestRunner.class) public class SplashActivityTest {first in the same form // Test}> When the test runs from the command line, given that the workaround build depends on the contents of the directory, we do not want to keep the data there:

  project-root $ ./ Gradlew clean test - release  

No comments:

Post a Comment