Q:

Setting up Roboelectric testing for android

I am trying to add some Roboelectric unit tests to my app.
Using Roboelectric 3.0 i want to be able to test my activity PinActivity and the fragment that is in it.
import android.support.v7.app.AppCompatActivity;
import android.app.Fragment;

PinActivity extends AppCompatActivity {

gradle file contains:
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    testCompile "org.robolectric:robolectric:3.0"
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
}

PinActivityTest contains:  (Edited to add @Config did not fix)
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;

import static org.junit.Assert.*;

@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21)
public class PinActivityTest {

    @Test
    public void onCreate_shouldInflateLayout() throws Exception {
        PinActivity activity = Robolectric.buildActivity(PinActivity.class).create().get();
        assertNotNull(activity);
    }

Currently getting:
WARNING: No manifest file found at .\AndroidManifest.xml.Falling back to the Android OS resources only. To remove this warning, annotate your test class with @Config(manifest=Config.NONE). and  java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
Why can't it find my AndroidManifest?
Anyone know how i can fix this or more Roboelectric tutorials with similar examples?

A:

As log says, you forgot about @Config annotation, so it could not find your AndroidManifest file:
Try this:
@RunWith(RobolectricGradleTestRunner.class) 
@Config(constants = BuildConfig.class, sdk = 21) public class PinActivityTest {
    @Test
    public void onCreate_shouldInflateLayout() throws Exception {
        PinActivity activity = Robolectric.buildActivity(PinActivity.class).create().get();
        assertNotNull(activity);
    }

As Roboelectric not support already API23, I set up test sdk as API 21.
EDIT: Change also:
    PinActivity activity = Robolectric.buildActivity(PinActivity.class).create().get()

to
    PinActivity activity = Robolectric.setupActivity(PinActivity.class);

NOTE: My Robolectric dependencies looks now:
 testCompile("org.robolectric:robolectric:3.0") {
        exclude module: 'classworlds'
        exclude module: 'commons-logging'
        exclude module: 'httpclient'
        exclude module: 'maven-artifact'
        exclude module: 'maven-artifact-manager'
        exclude module: 'maven-error-diagnostics'
        exclude module: 'maven-model'
        exclude module: 'maven-project'
        exclude module: 'maven-settings'
        exclude module: 'plexus-container-default'
        exclude module: 'plexus-interpolation'
        exclude module: 'plexus-utils'
        exclude module: 'wagon-file'
        exclude module: 'wagon-http-lightweight'
        exclude module: 'wagon-provider-api'
    }

If you have any question, please free to ask.
Hope it help

