Test Android Calculator App
Test Android Calculator App
Test Android Calculator App
We will move on to design our logic to test AndroidCaculator. We need to create test case class where we will write code to test AndroidCalculator's main class (Main.java).
On New Junit Test Case screen, most of the options will be automatically filled as we have already created test project (AndroidCalculatorTest) with project (AndroidCalculator). We need to enter the Name of Test case, which I will enter TestMain, as I am going to test (main.java) of AndroidCalculator project. On next section check Setup(), tearDown() & Constructor options and click on Finish.
A new test case by the name of TestMain will be created into com.calculator.test package of my test project (AndroidCaculatorTest)
com.jayway.android.robotium.solo.Solo error is shown if the Robotium jar is not exported . To fix the issue, after adding the Robotium jar go to the "Order & Export" tab and click the check-box besides the Robotium Jar and then click "OK". Please see the screenshot below.
public class TestMain extends ActivityInstrumentationTestCase2<Main> { private Solo solo; public TestMain() { super("com.calculator", Main.class); } @Override protected void setUp() throws Exception { super.setUp(); solo = new Solo(getInstrumentation(), getActivity()); } public void testDisplayBlackBox() { //Enter 10 in first editfield solo.enterText(0, "10"); //Enter 20 in first editfield solo.enterText(1, "20"); //Click on Multiply button solo.clickOnButton("Multiply"); //Verify that resultant of 10 x 20 assertTrue(solo.searchText("200")); }
public void testDisplayWhiteBox() { //Defining our own values to multiply float firstNumber = 10; float secondNumber = 20; float resutl = firstNumber * secondNumber ; //Access First value (editfiled) and putting firstNumber value in it EditText FirsteditText = (EditText) solo.getView(R.id.EditText01); solo.enterText(FirsteditText, String.valueOf(firstNumber)); //Access Second value (editfiled) and putting SecondNumber value in it EditText SecondeditText = (EditText) solo.getView(R.id.EditText02); solo.enterText(SecondeditText, String.valueOf(secondNumber)); //Click on Multiply button solo.clickOnButton("Multiply");
assertTrue(solo.searchText(String.valueOf(resutl))); TextView outputField = (TextView) solo.getView(R.id.TextView01); ArrayList currentTextViews = solo.getCurrentTextViews(outputField); assertFalse(currentTextViews.isEmpty()); TextView output = (TextView) currentTextViews.get(0); //Assert to verify result with visible value assertEquals(String.valueOf(resutl), output.getText().toString()); } @Override protected void tearDown() throws Exception{ solo.finishOpenedActivities(); } }