dimanche 27 janvier 2019

How to test a method that has parameters EditText and TextView

I have a method that i want to test, the method has signature public void processBalance(EditText incomeField, HashMap yearsMappedToObjectYearsMap, boolean isPaymentCircleSet,TextView balanceLabel) and is in a util class MainActivityUtil. My MainActivity is

public class MainActivity extends AppCompatActivity implements IHODClientCallback {

@Override
protected void onCreate(Bundle savedInstanceState) {

....

mainUtil = new MainActivityUtil(this);
incomeField = new EditText(this);
incomeField = (EditText) findViewById(R.id.incomeField); 
isPaymentCircleSet = sharedprefs.getBoolean(ISPAYMENTCIRCLE, false);
balanceLabel = new TextView(this);
balanceLabel = (TextView) findViewById(R.id.balanceView);

yearsMappedToObjectYearsMap = new HashMap<>();
yearsMappedToObjectYearsMap = util.readTheFile();

//and the method later is called

mainUtil.processBalance(incomeField, yearsMappedToObjectYearsMap,isPaymentCircleSet,balanceLabel);
}
}

And i want to write a Junit test to test this method, i have seen that i can use Mockito so far i have done this:

@RunWith(MockitoJUnitRunner.class)
public class MainActivityTests {
    @Mock
    private Context context;
    @Mock
    private EditText incomeField;
    @Mock
    MainActivity mMockMainActivity;
    @Test
    public void testProcessBalance(){

        MainActivityUtil mainUtil = new MainActivityUtil(context);
        incomeField = (EditText) mMockMainActivity.findViewById(R.id.incomeField);
        incomeField.setText(1000);
        HashMap<String, AnyYear> yearsMappedToObjectYearsMap = new HashMap<>();
        boolean isPaymentCircleSet = false;
        TextView balanceLabel = new TextView(context);
        mainUtil.processBalance(incomeField,yearsMappedToObjectYearsMap,isPaymentCircleSet,balanceLabel);

    }

I see that the incomeField is null and that is wrong. Any help would be appreciated.

Aucun commentaire:

Enregistrer un commentaire