Read Data from JSON - Notes By ShariqSP

How to Read Data from JSON in Android Mobile Application Testing (Using Eclipse)

When working on Android mobile application testing in Eclipse, you can use JSON files to manage test data for inputs, API responses, and configurations. Below is a step-by-step guide to reading JSON files in Eclipse:

Steps to Read Data from JSON

  1. Set Up the Environment:
    • Ensure you have installed the Android Development Tools (ADT) plugin in Eclipse.
    • Set up your Android project in Eclipse.
  2. Add Gson Library:
    • Download the Gson JAR file from the Gson GitHub page.
    • Include the JAR file in your project by:
      1. Right-click your project in Eclipse.
      2. Select Build Path > Configure Build Path.
      3. Under the Libraries tab, click Add External JARs... and select the downloaded Gson JAR.
  3. Place the JSON File:
    • Store the JSON file in the assets folder of your Android project.
    • Right-click the assets folder in Eclipse and select New > File to add the JSON file.
  4. Write Code to Read the JSON File:
    • Access the JSON file using InputStream.
    • Parse the JSON file using the Gson library.

Sample Code in Eclipse


              import com.google.gson.Gson;
              import com.google.gson.reflect.TypeToken;
              import java.io.InputStream;
              import java.io.InputStreamReader;
              import java.util.List;
              
              public class JsonReaderExample {
                  public void readJsonDataFromAssets(Context context) {
                      try {
                          // Access JSON file from assets folder
                          InputStream inputStream = context.getAssets().open("testdata.json");
                          InputStreamReader reader = new InputStreamReader(inputStream);
              
                          // Use Gson to parse the JSON file
                          Gson gson = new Gson();
                          List testDataList = gson.fromJson(reader, new TypeToken>() {}.getType());
              
                          // Iterate through the data and print results
                          for (TestData data : testDataList) {
                              System.out.println("Username: " + data.username);
                              System.out.println("Password: " + data.password);
                              System.out.println("Expected Result: " + data.expectedResult);
                          }
              
                          // Close resources
                          reader.close();
                      } catch (Exception e) {
                          e.printStackTrace();
                      }
                  }
              
                  // Define a model class for your JSON data
                  class TestData {
                      String username;
                      String password;
                      String expectedResult;
                  }
              }
                

Example JSON File


              [
                {
                  "username": "testuser1",
                  "password": "password123",
                  "expectedResult": "Success"
                },
                {
                  "username": "testuser2",
                  "password": "wrongpassword",
                  "expectedResult": "Error"
                }
              ]
                

Real-Time Scenarios

  1. Login Testing:
    • Store multiple login credentials in the JSON file.
    • Use automation scripts to simulate login attempts with the data.
    • Validate app responses based on the expectedResult field.
  2. API Response Testing:
    • Mock API responses in JSON format to test app behavior without the live backend.
    • Verify UI updates or error handling based on the mock data.
  3. Dynamic UI Testing:
    • Define UI elements and their properties in a JSON file.
    • Automate tests to validate the app dynamically creates or configures UI elements based on the JSON data.

Advantages of Using JSON

  • JSON is lightweight and easy to parse, making it efficient for mobile testing.
  • It supports complex, nested data structures, which are common in real-world apps.
  • Compatible with various tools and APIs, allowing sea