2014-12-22 3 views
6

Espresso 2.0과 함께 제공되는 ActivityInstrumentationTestCase2 클래스를 사용할 때 별난 경고 Method annotated with @Test inside class extending junit3 testcase이 나타납니다.Espresso 2.0 - @Test로 주석이 달린 메소드에서 junit3 테스트 케이스를 확장하는 클래스

내 클래스는 단지 구글은 예로서 제공되는 하나처럼 보이는 : 나는 build.gradle에 필요한 모든 일을 추가 한

import android.support.test.InstrumentationRegistry; 
import android.support.test.runner.AndroidJUnit4; 
import android.test.ActivityInstrumentationTestCase2; 
import android.test.suitebuilder.annotation.LargeTest; 

import org.junit.After; 
import org.junit.Before; 
import org.junit.Test; 
import org.junit.runner.RunWith; 

import static android.support.test.espresso.matcher.ViewMatchers.assertThat; 
import static org.hamcrest.Matchers.notNullValue; 

@RunWith(AndroidJUnit4.class) 
@LargeTest 
public class MyCoolActivityTests extends ActivityInstrumentationTestCase2<MyCoolActivity> { 

    private MyCoolActivity mActivity; 

    public MyCoolActivityTests() { 
     super(MyCoolActivity.class); 
    } 

    @Before 
    public void setUp() throws Exception { 
     super.setUp(); 
     injectInstrumentation(InstrumentationRegistry.getInstrumentation()); 
     mActivity = getActivity(); 
    } 

    @Test 
    public void checkPreconditions() { 
     assertThat(mActivity, notNullValue()); 
     // Check that Instrumentation was correctly injected in setUp() 
     assertThat(getInstrumentation(), notNullValue()); 
    } 

    @After 
    public void tearDown() throws Exception { 
     super.tearDown(); 
    } 
} 

: 어떤 방법으로는

android { 
    defaultConfig { 
     testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 
    } 
} 

dependencies { 
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0' 
    androidTestCompile 'com.android.support.test:testing-support-lib:0.1' 
} 

있는가 이 경고를 멀리합니까?

+0

이 지침을 사용하고 있습니까? https://code.google.com/p/android-test-kit/wiki/EspressoSetupInstructions – helleye

+0

그래도 사용 했으므로 빌드시에 – Niklas

+0

을 사용했습니다. 당신은 또한 테스트를 포함하여'// App의 의존성을 가지고있다.com.android.support : support-annotations:21.0.3 ' ' – helleye

답변

16

ActivityInstrumentationTestCase2TestCase에서 확장되어 있기 때문에 JUnit 3 테스트 케이스입니다.

@Test 주석은 JUnit 3에서 사용 된 test-prefix 명명 규칙을 대체합니다. JUnit 4 테스트 클래스는 더 이상 TestCase 또는 그 하위 클래스를 확장 할 필요가 없습니다. 실제로 JUnit 4 테스트는 TestCase를 확장 할 수 없습니다. 그렇지 않으면 AndroidJUnitRunner가 JUnit 3 테스트로 처리합니다.

http://developer.android.com/tools/testing-support-library/index.html#AndroidJUnitRunner

당신은 (또는 이상) com.android.support.test:rules:0.4에서 제공 ActivityTestRule로 이전, 또는 JUnit을 3

을 고수 할 수 중 또 다른 옵션은 getInstrumentation()을 가지고 에스프레소 2에 의해 제공, InstrumentationRegistry이다, getContext(), getTargetContext() (이상) 이 메소드는 정적 인 방식으로 현재 Instrumentation, Test Context 및 Target Context에 대한 액세스를 제공합니다. 따라서 JUnit 4 테스트 케이스 클래스에서 사용할 정적 유틸리티 메소드를 작성할 수 있습니다. 이 유틸리티는 현재 기본 JUnit 3 테스트 케이스 클래스에서만 사용할 수있는 기능을 모방합니다. (더 이상 필요하지 않습니다.)

+1

혼란 스러워요 ... AndroidJUnitRunnerUserGuide에서 ActivityInstrumentationTestCase2 및 JUnit4를 사용하는 방법에 대한 예제가 있습니다. 실제로 Junit4와 Android를 찾을 때 찾은 유일한 방법입니다. ActivityInstrumentationTestCase2가 JUnit3이라고 말하면 얻을 수 있습니다. 그러나 사용자 가이드는 JUnit4에서 사용할 수 있다고 말합니다. 실제로 사용자 가이드의 예제와 정확히 동일한 예제가 있는데 경고 메시지가 나타납니다. 게다가, 그것을 실행하려고하면 "Empty Test Suite"라고 말합니다. –

+0

모든 googlecode 링크가 망가졌습니다. –

+0

링크를 업데이트하겠다고 언급 해 주셔서 감사합니다. –