android - 如何为单元测试期间流程中调用的静态方法返回不同的值?

标签 android unit-testing junit mockito robolectric

我正在尝试为以下代码段编写单元测试。

class ABC {
    int getMyValue(final Activity activity) {
        if(MyClass.getInstance(activity).getValue() == 1) return 10;
        else return 20;
    }

    void doSomething() {
    }
}

我试过类似的方法来测试 doSomething 函数。

mABC = new ABC();

public void test_doSomething() {
   doReturn(20).when(mABC).getMyValue();
   //validate
}

如何以类似的方式测试 getMyValue?我想断言,当值为 1 时,它会返回 10,而在所有其他情况下,它会返回 20。

我正在我的 android 应用程序中执行此操作。是否有任何现有框架可以帮助我做到这一点?

编辑:

MyClass 看起来像这样

public class MyClass {

   private static Context mContext;
   public static getInstance(Context context) {
     mContext = context;
     return new MyClass();
   }

   private MyClass() {}

   public void getDreamValue() {
     Settings.Secure.getInt(mContext.getContentResolver(), "dream_val", -1);
   }
}

最佳答案

您可能会考虑按如下方式修改您的 MyClass

public class MyClass {

   private static Context mContext;

   // Create a private variable that holds the instance. 
   private Myclass instance;

   public static getInstance(Context context) {
     mContext = context;

     if (instance == null) 
         instance = new MyClass(); // Assign the instance here 

     return instance;
   }

   private MyClass() {}

   public void getDreamValue() {
     Settings.Secure.getInt(mContext.getContentResolver(), "dream_val", -1);
   }
}

现在,当您使用 Robolectric 时,您可以在测试类中将 instance 值设置为模拟,如下所示。

@RunWith(RobolectricTestRunner.class) 
public class ABCTest {

    @Mock
    MyClass mockInstance; 

    @Mock
    Context mockContext; 

    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);

        // Set the mock instance for MyClass
        ReflectionHelpers.setStaticField(MyClass.class, "instance", mockInstance);
    }

    @Test
    public void testWhen1() {
       doReturn(1).when(mockInstance).getDreamValue();
       Assert.assertEquals(10, new ABC().getMyValue());
    }

    @Test
    public void testWhenNot1() {
       doReturn(2).when(mockInstance).getDreamValue();
       Assert.assertEquals(20, new ABC().getMyValue());
    }

    @After
    public void tearDown() {
        // Set the instance to null again to enable further tests to run 
        ReflectionHelpers.setStaticField(MyClass.class, "instance", null);
    }
}

希望对您有所帮助。

注意:看起来您正在尝试提供 MyClass 的单例实例。因此,您真的不应该在 getInstance 函数中创建 MyClass 的新实例。我避免每次都创建一个新实例,在我的代码中使用 null 检查。

关于android - 如何为单元测试期间流程中调用的静态方法返回不同的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59444370/

相关文章:

android - 不支持的操作异常 : addView(View) is not supported in AdapterView

找不到 xml 和命名空间中的 Android Studio 未知属性

java - 如何在 grails 2.0.3 中对服务进行单元测试?

java - 对来自 NetBeans 的 Web 服务客户端进行单元测试

android - Service.startForeground() 在使用 ServiceTestCase 运行时抛出 NullPointerException

android - 在单个目录中存储多个音频文件-Android

android - 以编程方式将项目添加到 ScrollView 顶部,但不应滚动到顶部

java - 使用 Mockito 2 模拟最后一个类

android - Dagger2 可以用于单元测试和插桩测试吗?

c# - 使用 Moq 模拟事件(基于事件的异步模式) - 如何对 UT 中的事件使用react?