Unit testing on Android Studio: “not mocked” error

前端 未结 2 598
伪装坚强ぢ
伪装坚强ぢ 2021-02-13 09:17

I am new to Android Studio. I am using Android Studio 1.2 preview 2, gradle 2.2.1 and gradle plugin 1.1.0.

I cannot get around this error, when trying to run my unit te

2条回答
  •  清酒与你
    2021-02-13 09:40

    I couldn't get Instrumentation tests to work, using Android Studio, I guess they're still finalising the implementation. And since it needs to run on the emulator, there are faster options: regular unit tests.

    Thanks to Jared's tips, I switched to Robolectric, which is easy to use on Android Studio.

    androidTestCompile 'junit:junit:4.12'
    androidTestCompile "org.robolectric:robolectric:3.0"
    

    and

    import org.junit.Before;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.robolectric.RuntimeEnvironment;
    import org.robolectric.RobolectricTestRunner;
    import static junit.framework.TestCase.assertEquals;
    
    @RunWith(RobolectricTestRunner.class)
    public class AppPreferencesTest {
    
        AppPreferences preferences;
    
        @Before
        public void setUp() throws Exception {
            preferences = new AppPreferences(RuntimeEnvironment.application.getApplicationContext());
        }
    
        @Test
        public void testIsNotificationsEnabled_Default() throws Exception {
            assertEquals(true, preferences.isNotificationsEnabled());
        }
    
        ...
    

    The info here seems to be correct at this time: http://nenick-android.blogspot.nl/2015/02/android-studio-110-beta-4-and.html But will probably deprecate again in the near future, as all the info I found on this subject using google deprecated already.

提交回复
热议问题