如何在Robolectric 3.0中创建自定义阴影?

10

我需要模拟一些自定义类(为其创建一个影子)。 我已经在http://robolectric.org/custom-shadows/上阅读了如何做到这一点。

所以,我有一些类:

public class MyClass {

  public static int regularMethod() { return 1; }
}

我创建了一个阴影:
@Implements(MyClass.class)
public class MyShadowClass {

  @Implementation
  public static int regularMethod() { return 2; }
}

我在Test类中设置了阴影:

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, shadows={MyShadowClass.class})
public class MyTest {

  @Test
  public void testShadow() {
      assertEquals(2, MyClass.regularMethod());
  }
}

但是阴影没有被使用。
java.lang.AssertionError: 
Expected :2
Actual   :1

如何使任何自定义阴影对RobolectricGradleTestRunner可见?

我已经尝试过:

  1. http://www.codinguser.com/2015/06/how-to-create-shadow-classes-in-robolectric-3/
  2. https://github.com/jiahaoliuliu/RobolectricSample/blob/master/app-tests/src/main/java/com/jiahaoliuliu/robolectricsample/RobolectricGradleTestRunner.java
  3. 使用Robolectric自定义阴影类模拟本地方法

但是我遇到了各种编译错误,例如:

  • InstrumentingClassLoaderConfig未找到。
  • Setup未找到。
如何在Robolectric 3.0中正确使用自定义阴影?

通常我们会将静态方法包装在自己的类或受保护的方法中,以便在测试中进行模拟。 - Eugen Martynov
2个回答

5

应该避免使用自定义阴影,只有在代码无法进行重构以使您的测试像本地方法调用一样运行时才能使用它。最好使用powermock或mockito来模拟该类的对象或进行监视,而不是自定义阴影它。如果它是一个静态方法,则使用powermock。

在我们的项目中,有一个类有一些本地方法,并且它是应用程序中到处都使用的配置类。因此,我们将本地方法移动到另一个类并进行了阴影处理。那些本地方法导致了测试用例失败。

无论如何,以下是如何在robolectric 3.0中进行自定义阴影的方法:

创建一个扩展RobolectricGradleTestRunner的自定义测试运行器:

public class CustomRobolectricTestRunner extends RobolectricGradleTestRunner {


public CustomRobolectricTestRunner(Class<?> klass) throws InitializationError {
    super(klass);
}

public InstrumentationConfiguration createClassLoaderConfig() {
    InstrumentationConfiguration.Builder builder = InstrumentationConfiguration.newBuilder();
    builder.addInstrumentedPackage("com.yourClassPackage");
    return builder.build();
}

请确保该软件包不包含您正在使用robolectric运行的任何测试用例。


4

我是贾豪,你所提到的第二个代码库的创建者。

首先感谢你检查我的代码。我在Android方面进行了很多研究,很高兴我的研究对别人有用。

然后,关于Robolectric的影子。我在这个项目中使用了Robolectric 3.1,测试Robolectric 3与MarshMallow的兼容性: https://github.com/jiahaoliuliu/robolectricForMarshmallow

我一直在测试新的运行时权限管理器,以及应用程序和活动的阴影处理。

以下是阴影活动的示例代码:

import android.content.Context;
import com.jiahaoliuliu.robolectricformarshmallow.controller.MainController;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;

/**
 * Created by Jiahao on 7/18/16.
 */
@Implements(MainController.class)
public class MainControllerShadow {

    public void __constructor__ (Context context) {
        // Not do anything
    }

    @Implementation
    public String getTextToDisplay(boolean permissionGranted) {
        return "Test";
    }
}

https://github.com/jiahaoliuliu/robolectricForMarshmallow/blob/master/app/src/test/java/com/jiahaoliuliu/robolectricformarshmallow/shadow/MainControllerShadow.java

以下是我在单元测试中的使用方式:

包 com.jiahaoliuliu.robolectricformarshmallow;

import com.jiahaoliuliu.robolectricformarshmallow.shadow.MainControllerShadow;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.annotation.Config;

import static org.junit.Assert.*;

/**
 * Created by Jiahao on 6/30/16.
 */
@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, manifest = Config.NONE, application = FoolApplication.class,
    shadows = { MainControllerShadow.class}, sdk = 18)
public class MainActivityTest {

    private MainActivity mMainActivity;

    @Before
    public void setUp() throws Exception {
        mMainActivity = Robolectric.setupActivity(MainActivity.class);
    }

    @After
    public void tearDown() throws Exception {

    }

    @Test
    public void testOnCreate() throws Exception {
        // Simple test to know that it works
        assertTrue(true);
    }
}

https://github.com/jiahaoliuliu/robolectricForMarshmallow/blob/master/app/src/test/java/com/jiahaoliuliu/robolectricformarshmallow/MainActivityTest.java

如您所见,我没有使用定制的Gradle测试运行程序。我已经检查了Robolectric的源代码,对于版本3.0和3.1(最新版本),只需在标题中指定阴影类即可。希望这能帮到您。

你在MainActivityTest()中如何使用它? - The_Martian

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接