模拟注射使用的Mockito - Android电子 [英] Mock injection using Mockito - Android

查看:290
本文介绍了模拟注射使用的Mockito - Android电子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始在Android单元测试。我试图测试一个简单的计算器应用程序,我被困在关于注射嘲笑一个问题。

按我的理解,也有嘲笑注射两种方式。使用依赖注入或使用注解如 @Mock @InjectMocks

因此​​,这里是我的问题:

我的计算器类使用另一个类的瓦尔持有我打算用变量的缺省值。这就是我要来嘲弄,这样我可以使用,而不是默认的其他变量的类。

  @RunWith(MockitoJUnitRunner.class)
公共类CalcActivityTest扩展ActivityUnitTestCase< CalcActivity> {在私人意图;
私人按钮btnAdd,btnSub,btnMul,btnDiv,btnDef;
私人TextView的资源;
@InjectMocks
私人CalcActivity mActivity;
@嘲笑
私人瓦尔mockVar;
公共CalcActivityTest(){
    超(CalcActivity.class);
}@之前
保护无效设置()抛出异常{    super.setUp();
    在=新意图(getInstrumentation()getTargetContext(),CalcActivity.class。);
    in.putExtra(NUM1,20.0);
    in.putExtra('NUM2',20.0);
    startActivity(中,NULL,NULL);
    mockVar =模拟(Vars.class);
    mockVar.setn1(20);
    mockVar.setn2(40);    mActivity = getActivity();}

然而,当我尝试使用mockVar任何操作的,里面存放的瓦尔使用默认值而不是mockVar的这可能意味着嘲笑变量不正确注射。任何人都可以指出我在那儿找到了问题?

编辑:更新与CalcActivity code问题

 包com.example.advancedcalc;进口com.example.advancedcalc.R;进口android.os.Bundle;
进口android.app.Activity;
进口android.view.Menu;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;
进口android.widget.TextView;公共类CalcActivity扩展活动实现OnClickListener {私人按钮btnAdd,btnSub,btnMul,btnDiv,btnDefault;
私人TextView的资源;私人双N1,N2;私人瓦尔变种;公共CalcActivity商(VAR VAR){
    this.var = VAR;
}@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_calc);    btnAdd =(按钮)findViewById(R.id.add);
    btnSub =(按钮)findViewById(R.id.sub);
    btnMul =(按钮)findViewById(R.id.mul);
    btnDiv =(按钮)findViewById(R.id.div);    btnDefault =(按钮)findViewById(R.id.btnDef);    RES =(的TextView)findViewById(R.id.res);    捆绑额外= getIntent()getExtras()。
    N1 = extras.getDouble(NUM1);
    N2 = extras.getDouble(NUM2);    VAR =新瓦尔();    btnAdd.setOnClickListener(本);
    btnSub.setOnClickListener(本);
    btnMul.setOnClickListener(本);
    btnDiv.setOnClickListener(本);    btnDefault.setOnClickListener(本);}
@覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
    //充气菜单;如果是present这增加了项目操作栏。
    。getMenuInflater()膨胀(R.menu.calc,菜单);
    返回true;
}@覆盖
公共无效的onClick(视图v){
    开关(v.getId()){
    案例R.id.add:
        res.setText(将String.valueOf(添加(N1,N2)));
        打破;
    案例R.id.sub:
        res.setText(将String.valueOf(分(N1,N2)));
        打破;
    案例R.id.mul:
        res.setText(将String.valueOf(MUL(N1,N2)));
        打破;
    案例R.id.div:
        res.setText(将String.valueOf(格(N1,N2)));
        打破;
    案例R.id.btnDef:
        N1 = var.getn1();
        N2 = var.getn2();
    }}公共双增(双N1,N2双){
    返回N1 + N2;
}
公共双分(双N1,N2双){
    返回N1-N2;
}
公共双MUL​​(双N1,N2双){
    返回N1 N2 *;
}
公共双DIV(双N1,N2双){
    返回N1 / N2;
}
}


解决方案

  1. 既然你已经使用了@Mock类瓦尔,你并不需要使用 -

    mockVar =模拟(Vars.class);


  2. 您需要在存根上你的调用模拟对象。相反的 -

    mockVar.setn1(20);
    mockVar.setn2(40);


您需要将您的存根干将(我肯定会像getn1()的方法在瓦尔类) -

  Mockito.when(mockVar.getn1())thenReturn(20)。
Mockito.when(mockVar.getn2())thenReturn(40)。

,然后写你的测试。

I've just started unit testing on Android. I've tried testing a simple calculator app and i'm stuck on a problem regarding injecting mocks.

As per my understanding there are two ways of injecting mocks. Using dependency injections or using annotations like @Mock and @InjectMocks.

So here's my problem:

My calculator class uses another class Vars that holds the default value of the variables i intend to use. This is the class i want to mock so that i can use other variables instead of the default ones.

@RunWith(MockitoJUnitRunner.class)
public class CalcActivityTest extends ActivityUnitTestCase<CalcActivity> {

private Intent in;
private Button btnAdd,btnSub,btnMul,btnDiv,btnDef;
private TextView res;
@InjectMocks
private CalcActivity mActivity;
@Mock
private Vars mockVar;


public CalcActivityTest() {
    super(CalcActivity.class);
}

@Before
protected void setUp() throws Exception{

    super.setUp();
    in = new Intent(getInstrumentation().getTargetContext(),CalcActivity.class);
    in.putExtra("num1", 20.0);
    in.putExtra("num2",20.0);
    startActivity(in, null, null);
    mockVar = mock(Vars.class);
    mockVar.setn1(20);
    mockVar.setn2(40);

    mActivity = getActivity();

}

However when i try using mockVar for any of the operations, the default value stored inside Vars is used instead that of mockVar which probably means that the mocked variable is not injected properly. Can anyone point out where i've went wrong ?

EDIT : Updating question with code from CalcActivity

package com.example.advancedcalc;

import com.example.advancedcalc.R;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class CalcActivity extends Activity implements OnClickListener {

private Button btnAdd,btnSub,btnMul,btnDiv,btnDefault;
private TextView res;

private double n1,n2;

private Vars var;

public CalcActivity(Vars var){
    this.var  = var;
}

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

    btnAdd = (Button) findViewById(R.id.add);
    btnSub = (Button) findViewById(R.id.sub);
    btnMul = (Button) findViewById(R.id.mul);
    btnDiv = (Button) findViewById(R.id.div);

    btnDefault = (Button) findViewById(R.id.btnDef);

    res = (TextView) findViewById(R.id.res);

    Bundle extras = getIntent().getExtras();
    n1 = extras.getDouble("num1");
    n2 = extras.getDouble("num2");

    var = new Vars();

    btnAdd.setOnClickListener(this);
    btnSub.setOnClickListener(this);
    btnMul.setOnClickListener(this);
    btnDiv.setOnClickListener(this);

    btnDefault.setOnClickListener(this);

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.calc, menu);
    return true;
}

@Override
public void onClick(View v) {
    switch(v.getId()){
    case R.id.add: 
        res.setText(String.valueOf(add(n1,n2)));
        break;
    case R.id.sub: 
        res.setText(String.valueOf(sub(n1,n2)));
        break;
    case R.id.mul: 
        res.setText(String.valueOf(mul(n1,n2)));
        break;
    case R.id.div: 
        res.setText(String.valueOf(div(n1,n2)));
        break;
    case R.id.btnDef:
        n1 = var.getn1();
        n2 = var.getn2();
    }

}

public double add(double n1,double n2){
    return n1+n2;
}
public double sub(double n1,double n2){
    return n1-n2;
}
public double mul(double n1,double n2){
    return n1*n2;
} 
public double div(double n1,double n2){
    return n1/n2;
}
}

解决方案

  1. Since you have already used @Mock for Vars class, you don't need to use -

    mockVar = mock(Vars.class);

  2. You need to stub the calls on you mock object. Instead of -

    mockVar.setn1(20); mockVar.setn2(40);

You need to stub your getters (I am sure there will be methods like getn1() in Vars class) -

Mockito.when(mockVar.getn1()).thenReturn(20);
Mockito.when(mockVar.getn2()).thenReturn(40);

And then write your test.

这篇关于模拟注射使用的Mockito - Android电子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆