setup Robolectric for tests

This commit is contained in:
tibbi 2015-12-26 22:35:45 +01:00
parent e5e64f93fa
commit b0f64390dd
5 changed files with 44 additions and 16 deletions

View File

@ -21,7 +21,9 @@ android {
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.jakewharton:butterknife:7.0.1'
testCompile 'junit:junit:4.12'
testCompile "org.robolectric:robolectric:3.0"
}

View File

@ -0,0 +1,2 @@
android.library.reference.1=../../build/intermediates/exploded-aar/com.android.support/support-v4/23.1.1
android.library.reference.2=../../build/intermediates/exploded-aar/com.android.support/appcompat-v7/23.1.1

View File

@ -1,15 +0,0 @@
package calculator.simplemobiletools.com.simple_calculator;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* To work on unit tests, switch the Test Artifact in the Build Variants view.
*/
public class ExampleUnitTest {
@Test
public void addition_isCorrect() throws Exception {
assertEquals(4, 2 + 2);
}
}

View File

@ -0,0 +1,17 @@
package calculator.simplemobiletools.com.simple_calculator;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.annotation.Config;
@RunWith(MyTestRunner.class)
@Config(constants = BuildConfig.class, manifest = "app/src/main/AndroidManifest.xml", sdk = 21)
public class MainActivityTest {
MainActivity activity;
@Before
public void setUp() {
activity = Robolectric.setupActivity(MainActivity.class);
}
}

View File

@ -0,0 +1,22 @@
package calculator.simplemobiletools.com.simple_calculator;
import org.junit.runners.model.InitializationError;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.manifest.AndroidManifest;
import org.robolectric.res.Fs;
public class MyTestRunner extends RobolectricTestRunner {
public MyTestRunner(Class<?> testClass) throws InitializationError {
super(testClass);
}
@Override
protected AndroidManifest getAppManifest(Config config) {
return new AndroidManifest(
Fs.fileFromPath("../app/src/main/AndroidManifest.xml"),
Fs.fileFromPath("../app/src/main/res"),
Fs.fileFromPath("../app/src/main/assets"));
}
}