Integration tests for your Android app that uses Microsoft Graph

You can create tests that exercise the integration of your Android app with services like Microsoft Graph. You can get an access token using the Resource Owner Password Credentials grant in OAuth 2.0 is your service supports it. Then you can use the access token in your test framework, like JUnit.

Browse the full example in the file of the Connect sample for Android.

Do you need UI Automation tests on Android? Check UI Automated tests for your Android app that uses Microsoft Graph instead.

Use the Resource Owner Password Credentials Grant to get an access token

OAuth 2.0 defines the Resource Owner Password Credentials Grant, that we can use to get an access token without user interaction. The grant flow consists in a single POST request to the authorization server that includes a valid username and password. The authorization server responds with an access token for the specified user.

Note: It’s up to the authorization server whether to implement the Resource Owner Password Credentials grant flow or not. Azure Active Directory implements the flow, this means that we can use a username and password of an Azure Active Directory account in our tests.

The following figure explains the Resource Owner Password Credentials grant flow.

 +---------+                                  +---------------+
 |         |>------- Username/Password ------>|               |
 |         |                                  | Authorization |
 | Client  |                                  |     Server    |
 |         |<--------- Access Token ---------<|               |
 +---------+                                  +---------------+

        Figure: Resource Owner Password Credentials Flow

Important: You should only use this grant when there is a high degree of trust between the resource owner and the client. Don’t use a username and password of an account that stores valuable data, use an account created specifically for testing.

Example POST request for the Resource Owner Password Credentials grant

The following table summarizes the POST request required to obtain the access token via the grant.

Parameter Value
Scheme: https
Section: /common/oauth2/token
  Content-type: application/x-www-form-urlencoded
  grant_type: password
  client_id: your_app_client_id
  password: your_password

Here’s the raw body in URL-encoded format.


Example response for the Resource Owner Password Credentials grant

The Microsoft Graph authorization service responds with the following values.

  "token_type": "Bearer",
  "scope": "Mail.Send User.Read",
  "expires_in": "3600",
  "expires_on": "1465325263",
  "not_before": "1465321363",
  "resource": "",
  "access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSU...",
  "refresh_token": "AAABAAAAiL9Kn2Z27UubvWFPbm0gL..."

Note: You should verify the access token for validity. Validate an Exchange identity token shows a method to validate a token. We’ll skip this step for the sake of brevity.

Getting the access token during the test setup

You can request one access token to use across multiple tests. Access tokens from Microsoft Graph last for about an hour, which should cover most scenario . In this case, you can request the access token during your test setup and make it available for all your tests.

The following code shows an example of a method annotated with **@BeforeClas **. This annotation ensures that the method is executed once before running the tests in the class.

public static void getAccessTokenUsingPasswordGrant() 
    throws  IOException, 
            JSONException {
    URL url = new URL("");
    HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();

    String urlParameters = String.format(
      URLEncoder.encode("", "UTF-8"),
      URLEncoder.encode("", "UTF-8"),
      URLEncoder.encode("your_password", "UTF-8")


    DataOutputStream dataOutputStream =
        new DataOutputStream(connection.getOutputStream());


    BufferedReader in = new BufferedReader(
            new InputStreamReader(connection.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();

    while ((inputLine = in.readLine()) != null) {

    JsonParser jsonParser = new JsonParser();
    JsonObject grantResponse = (JsonObject)jsonParser.parse(response.toString());

    // Store the access token in a member variable that your tests can use
    accessToken = grantResponse.get("access_token").getAsString();

Your test suite has an access token that the individual tests can use to make authenticated requests to Microsoft Graph.

Using the access token in the individual tests

The following test is an example of how to use the access token in a JUnit test. The app uses Retrofit 2.0, which accepts an interceptor that we can use to inject the access token in every HTTP request.

public void sendMail_messageSent() throws IOException {
    Interceptor interceptor = new Interceptor() {
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            // Use member variable that our test setup already populated
            // with a valid access token from Microsoft Graph
            request = request.newBuilder()
                    .addHeader("Authorization", "Bearer " + accessToken)

            Response response = chain.proceed(request);
            return response;

    MSGraphAPIController controller = new MSGraphAPIController(interceptor);
    Call<Void> result = controller.sendMail(username, SUBJECT, BODY);
    retrofit2.Response response = result.execute();

        "HTTP Response was not successful", 

In the case of this app, the MSGraphAPIController controller object provides a constructor overload which allows us to provide a custom interceptor. The default constructor is used for the normal operation of the app, while the constructor that accepts an interceptor is used in the integration tests.

I hope this is article helps you increase the quality of your apps that use Microsoft Graph.

Happy testing!