mercredi 7 juin 2017

Expected exception breaking Jacoco coverage

I am writing some instrumentation tests where an exception is supposed to be thrown. Using the JUnit @Rule, the test is expecting the exception and the test passes. However, when I run connectedDebugAndroidTest, I get a build failure:

FAILURE: Build failed with an exception. What went wrong: Execution failed for task ':app:connectedDebugAndroidTest'. There were failing tests.

The tests themselves are passing, the only thing that's broken is the Jacoco build/coverage report. Here is the test:

@Test
public void TestQueryCase2WithNonNullSelection() {
    resetDatabase();

    try {
        expectedException.expect(IllegalArgumentException.class);
        expectedException.expectMessage("Too many arguments");
        Cursor firstGoal = context.getContentResolver().query(
                ContentUris.withAppendedId(GoalsContract.Goals.CONTENT_URI, 1),
                null, "not null", null, null);
    }
    catch (IllegalArgumentException e){
    }
}

and the code it's testing:

public Cursor query(Uri uri, String[] projection, String selection, 
    String[] selectionArgs, String sortOrder) {
    String tableName;
    int uriNum = uriMatcher.match(uri);

    switch (uriNum) {
        //Case 1 is for the entire Goals table
        case 1:
            if (TextUtils.isEmpty(sortOrder)) {
                sortOrder = "_ID ASC";
            }
            break;

        //Case 2 is for a specific row in the Goals table
        case 2:
            if (selection != null) {
                    throw new IllegalArgumentException("Too many arguments. " +
                            "There should not be any selection terms when querying a specific Goal.");
            }
            else {
                selection = "_ID = " + uri.getLastPathSegment();
            }
            break;

Aucun commentaire:

Enregistrer un commentaire