2011-08-29 4 views
11

testRuntime 구성에서 jar의 순서를 제어해야합니다.Gradle에서 클래스 경로 순서 지정 방법

robolectric-x.x.jar이 android.jar보다 먼저 나오는지 확인해야합니다. 그렇지 않으면 두려운 RuntimeException ("Stub!")이 표시됩니다.

어떻게하면됩니까?

+0

이는 robolectric 항아리 안드로이드 앞에 와야하는 문서에 말을 하는가 .jar가 클래스 패스에 있습니까? –

+1

예. Robolectric 홈 페이지에서 : "문제 해결 는 java.lang.RuntimeException가! 스텁 는 robolectric 있는지 확인하고 종속성은 클래스 경로에 안드로이드 API 단지 앞에 나타납니다." –

+0

클래스 경로의 순서를 명시 적으로 제어 할 수 있기 때문에 Eclipse에서 작동합니다. Eclipse 프로젝트는 Gradle에서 생성했습니다. –

답변

10

는 RoboGuice를 사용하여 내 안드로이드 앱에 대해 Robolectric 테스트를 실행에 대한 내 전체 build.gradle입니다 :

apply plugin: 'java' 

androidJar = new File(System.getenv('ANDROID_HOME'), '/platforms/android-7/android.jar') 

configurations { robo } 

dependencies { 
    robo('com.pivotallabs:robolectric:1.0-RC1') 
    testCompile('org.roboguice:roboguice:1.1.2') 
    testCompile('junit:junit:4.8.2') 
    testCompile project (':app') 
    testCompile files(androidJar) 
} 

sourceSets.test.compileClasspath = configurations.robo + sourceSets.test.compileClasspath 
sourceSets.test.runtimeClasspath = configurations.robo + sourceSets.test.runtimeClasspath 

test { 
    excludes = ['**/MyRobolectricTestRunner.class'] 
} 

내가 제외를 추가했다 테스트 러너의 경우 Gradle에서 예외가 발생합니다.

MyRobolectricTestRunner.java은 다음과 같습니다

package com.acme.myapp; 

import java.io.File; 
import org.junit.runners.model.InitializationError; 
import roboguice.application.RoboApplication; 
import roboguice.inject.ContextScope; 
import com.google.inject.Injector; 
import com.xtremelabs.robolectric.Robolectric; 
import com.xtremelabs.robolectric.RobolectricTestRunner; 

public class MyRobolectricTestRunner extends RobolectricTestRunner { 
    public MyRobolectricTestRunner(Class<?> testClass) throws InitializationError { 
     // Tell Robolectric where to find AndroidManifest.xml and res/ 
     super(testClass, new File("../app")); 
    } 

    /** 
    * Enable injection into tests as well... 
    */ 
    @Override 
    public void prepareTest(Object test) { 
     RoboApplication myApplication = (RoboApplication) Robolectric.application; 
     Injector injector = myApplication.getInjector(); 
     ContextScope contextScope = injector.getInstance(ContextScope.class); 
     contextScope.enter(myApplication); 
     injector.injectMembers(test); 
    } 

} 

을 그리고 여기 주입 보여주는 샘플 테스트입니다 :

package com.acme.myapp; 

import static org.junit.Assert.assertEquals; 

import org.junit.Test; 
import org.junit.runner.RunWith; 
import roboguice.inject.InjectResource; 

@RunWith(MyRobolectricTestRunner.class) 
public class StringFormattingTest { 

    @InjectResource(R.string.info_pending_amount) 
    private String pendingAmountPattern; 

    @Test 
    public void testFormatInfoPendingAmount() { 
     String s = String.format(pendingAmountPattern, 20.0d, "EUR"); 
     assertEquals("Only a part of your stake (20,00 EUR) was accepted", s); 
    } 

} 
+0

당신의 앱을 어떻게 참조합니까? 자체 설정을 통해. the : app은 또한 테스트가 소비 할 자체 의존성을 노출합니까? – Moritz

+0

우리는 github에서 간단한 샘플을 얻을 수 있습니까? – Gary

4

이 작동 할 수 있습니다 :

여기
configurations { robo } 

dependencies { 
    robo ... 
    testRuntime ... 
} 

sourceSets.test.runtimeClasspath = configurations.robo + sourceSets.test.runtimeClasspath 
+0

roboy를 compileClasspath에도 추가해야했습니다. –

+0

@Peter [이 경우 클래스 경로의 순서를 변경하는 방법]을 알고 계십니까? (http://stackoverflow.com/)/questions/22863845/how-to-configure-of-order-of-the-classpath-for-android-studio) ... – JJD