lundi 26 janvier 2015

Android UnitTest - RuntimeExceptions for Android classes

I'm developing an SDK and I'm trying to perform UnitTests on it. This means most of the my project is pure java code which involves Android code in some places. I want to perform UnitTest on my SDK and I decided to with with Roboelectric, Mockito and PowerMock (for static methods mocks).


Everything works fine except one issue: When my test calls any method which contains Android class, my test crashes (due to Stub issues). I know I can't test Activity,Views and more classes but the problem is I get RuntimeException even when my functions contain a use with Log class.


How can I handle this issue? I decided to work with pure UnitTest because most of my code doesn't contain Android classes except of Log class. By using pure java UnitTest I don't need any device to run and as a result I can perform multi test task on the same time.


I've tried to include the android.jar file in my gradle but it didn't work.


What should I do? 1. Stick to pure Java UnitTest: so how can I ignore/import the Log instructions. 2. Move to Android test framework: What is the best for my needs?


Here is a section in my gradle file relevant for the tests:



robolectric {
// configure the set of classes for JUnit tests
include '**/*UnitTest.class'


// confgure max heap size of the test JVM
maxHeapSize = '2048m'

// configure the test JVM arguments
jvmArgs '-XX:MaxPermSize=512m', '-XX:-UseSplitVerifier'

// configure whether failing tests should fail the build
ignoreFailures true

// use afterTest to listen to the test execution results
afterTest { descriptor, result ->
println "Executing test for {$descriptor.name} with result: ${result.resultType}"
}
}

dependencies {
androidTestCompile 'org.robolectric:robolectric:2.3'
androidTestCompile 'junit:junit:4.10'

androidTestCompile 'org.mockito:mockito-core:1.8.5'
androidTestCompile 'org.powermock:powermock-mockito-release-full:1.4.9'
androidTestCompile files('../libs-test/json.jar')
}


And here is an Example of a TestCase class:



import android.util.Log;


import junit.framework.TestCase;

import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.junit.Test;
import org.json.JSONObject;

import static org.powermock.api.mockito.PowerMockito.when;


@RunWith(PowerMockRunner.class)
@PrepareForTest(StaticInClass.class)
public class ClassExampleUnitTest extends TestCase{


@Test
public void testSimple(){
Log.d("UnitTest", "test");
assertTrue(true);
}


}

Aucun commentaire:

Enregistrer un commentaire