使用Mockito框架测试活动

时间:2016-12-08 08:32:47

标签: mocking mockito junit4

下面是简单计算器应用程序,我想使用Junit4和mockito Framework i测试它在Junit4上测试该应用程序并且它正常工作但我面临着模拟实现的问题

CalculatorOperationsInterfaces.java

public interface CalculatorOperationsInterfaces {
    public double add(double input1, double input2);
    public double subtract(double input1, double input2);
    public double multiply(double input1, double input2);
    public double divide(double input1, double input2);
}

MainActivity.java

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements CalculatorOperationsInterfaces {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public double add(double input1, double input2) {
        return (input1 + input2);
    }

    @Override
    public double subtract(double input1, double input2) {
        return (input1 - input2);
    }

    @Override
    public double multiply(double input1, double input2) {
        return (input1 * input2);
    }

    @Override
    public double divide(double input1, double input2) {
        if(input2==0)
            return -1;
        else
            return (input1 / input2);
    }
}

CalculatorOperationsMockTest.java

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import static org.mockito.Mockito.when;

// @RunWith attaches a runner with the test class to initialize the test data
@RunWith(MockitoJUnitRunner.class)
public class CalculatorOperationsMockTest {

    //@Mock annotation is used to create the mock object to be injected
    @Mock
    CalculatorOperationsInterfaces calcService;

    @Before
    public void setUp() {
        calcService = new MainActivity();
    }

    @After
    public void tearDown() {
        calcService = null;
    }


    @Test
    public void add(){
        //add the behavior of calc service to add two numbers
        when(calcService.add(10.0,20.0)).thenReturn(30.00);
        //test the add functionality
        Assert.assertEquals(calcService.add(10.0, 20.0),30.0,0);
    }

}

我使用MainActivity.java成功测试了以上Junit,但是当我尝试运行CalculatorOperationsMockTest.java文件时,我是Mockito框架的新用户我收到以下错误,我不知道如何解决它

Class not found: "myappmania.local_jvm_unit_test.CalculatorOperationsMockTest"Empty test suite.

1 个答案:

答案 0 :(得分:2)

这里至少有3个问题:

  1. "空测试套件的问题"查看this answer可以解决错误消息 - 确保您有一个指定InstrumentationRunner的运行配置。
  2. 您无法通过调用new MainActivity()来获取活动的实例。正确的习语是:

    @Rule
    public ActivityTestRule<MainActivity> activityRule = new ActivityTestRule<>(MainActivity.class, true, false);
    

    然后:

    calcService = activityRule.launchActivity(null);
    
  3. 你没有通过嘲笑CalculatorService来完成任何事情。在测试时,您可以使用模拟来测试名为“测试系统”的程序的行为。在这里,您的受测系统是CalculatorService的实现,即Activity。据我所知,你没有任何依赖注入。我希望this tutorial on mocking可以为您澄清这些概念。

  4. 另请查看official tutorial for instrumented unit tests in Android

    中的说明