2

我正在为我的一些模型使用 ActiveAndroid,我想开始对我的工作进行单元测试。不幸的是,我遇到了很多错误,即无法使用正确的上下文初始化 ActiveAndroid。

ActiveAndroid 初始化:

ActiveAndroid.initialize(上下文)

我试图通过以下方式初始化上下文:

  1. 有一个扩展应用程序的存根类,并使用它来初始化数据库。

    private class TestApp extends com.activeandroid.app.Application{
       @Override
       public void onCreate() {
         super.onCreate();
         initialiseDB(getDatabaseName());
       }
    
       protected String getDatabaseName() {
         return "sad";
       }
    
       private void initialiseDB(String dbName) {
          ActiveAndroid.initialize(this); 
       }
    }
    

这失败了,因为类为 .getPackageName() 和 .getApplicationContext() 返回 null,这两者都由初始化在内部使用。

我也尝试过使用 ShadowContextWrapper,但我可能用错了。这是我的做法:

    ShadowContextWrapper shadowContextWrapper = new ShadowContextWrapper();
    shadowContextWrapper.setApplicationName("appName");
    shadowContextWrapper.setPackageName("package");
    Context context = shadowContextWrapper.getApplicationContext();

这种方法在 ShadowContextWrapper.java:52 上的 NPE 失败,该 NPE 是 Robolectric 的一部分。线路本身:

    Context applicationContext = this.realContextWrapper.getBaseContext().getApplicationContext();

我正在使用 AS 1.2、robolectric3.0 和 activeandroid 3.1。

这是我正在运行的测试的示例。

@RunWith(CustomRobolectricTestRunner.class)
   public class ItemTest {

     public void setUp(){

     }

    @Test
    public void checkJUnitWork() {
       assertThat(true, is(true));
    }

    @Test
    public void testSave(){
       Item item = new Item("name", "units", 5.0, 4.5, 10.0);
       assertThat(item.getName(),is("name"));
    }

   public void tearDown(){

   }
}

我的自定义 Runner 如下:

public class CustomRobolectricTestRunner extends RobolectricTestRunner {

public CustomRobolectricTestRunner(Class<?> testClass)
        throws InitializationError {
    super(testClass);
    String buildVariant = (BuildConfig.FLAVOR.isEmpty()
            ? "" : BuildConfig.FLAVOR+ "/") + BuildConfig.BUILD_TYPE;
    String intermediatesPath = BuildConfig.class.getResource("")
            .toString().replace("file:", "");
    intermediatesPath = intermediatesPath
            .substring(0, intermediatesPath.indexOf("/classes"));

    System.setProperty("android.package",
            BuildConfig.APPLICATION_ID);
    System.setProperty("android.manifest",
            intermediatesPath + "/manifests/full/"
                    + buildVariant + "/AndroidManifest.xml");
    System.setProperty("android.resources",
            intermediatesPath + "/res/" + buildVariant);
    System.setProperty("android.assets",
            intermediatesPath + "/assets/" + buildVariant);

    ShadowContextWrapper shadowContextWrapper = new ShadowContextWrapper();
    shadowContextWrapper.setApplicationName("appName");
    shadowContextWrapper.setPackageName("package");
    Context context = shadowContextWrapper.getApplicationContext();

    ActiveAndroid.initialize(context);

}

}

4

1 回答 1

4

因此,您在测试中遇到的问题TestApp是没有运行。要让它运行,您需要设置您的测试以使用指定TestApp为要运行的应用程序的清单。

在源代码树的目录中按如下方式设置您的 TestApp /test...例如/src/test/java/some-long-package/TestApp.java

package com.some.company;

public class TestApp extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        ActiveAndroid.initialize(this);
    }
}

这是重要的部分

/test在源代码树中创建一个 android 清单文件。将此清单文件指定TestApp为应用程序。/src/test/resources/TestManifest.xml因此,在包含以下内容的路径上创建清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.test">

        <application android:name="com.some.company.TestApp">

    </application>
</manifest>

我建议摆脱CustomRobolectricTestRunner默认的 Robolectric 3.0 测试运行器,它将完成您需要完成的大部分工作。如果您需要测试各种构建变体,请使用@RunWith(RobolectricGradleTestRunner.class).

但是现在,按如下方式设置您的测试:

@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class, manifest = "src/test/resources/TestManifest.xml", sdk = Build.VERSION_CODES.LOLLIPOP)
public class MainAppTest {

    @Test
    public void runtimeApplicationShouldBeTestApp() throws Exception {
       String actualName = RuntimeEnvironment.application.getClass().getName();
       String expectedName = TestApp.class.getName();
       assert(actualName).equals(expectedName);
    }
}

@Config(manifest= ...)位将设置 Robolectric 以使用测试清单和测试应用程序。上面的简单测试验证了测试中使用的应用程序上下文确实是,TestApp.class 这将确保 ActiveAndroid 为测试正确初始化。

我也同意 Eugen 的观点,即您可能会在测试中尝试做一些事情。通过您的应用程序测试您的数据库,您正在有效地创建一个集成测试。我建议尽可能多地拆分功能。

祝测试愉快!

于 2015-04-11T19:45:37.890 回答