lundi 4 avril 2016

How to unit test with actual HttpURLConnection in android?

I am writing unit tests/integration tests that invoke class library that uses HttpsURLConnection objects. Many times whilst invoking methods on the connection objects through the unit tests, like addRequestProperty, setDoOutput, setRequestMethod I get exceptions like java.lang.IllegalStateException: Already connected. The behaviour is not consistent and the exception is thrown intermittently.

I have set following in the gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    testOptions {
        unitTests.returnDefaultValues = true
    }

    defaultConfig {
        applicationId "com.example.app"
        minSdkVersion 16
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.2.1'
}

The same code works fine in the App

Aucun commentaire:

Enregistrer un commentaire