2011-12-22 33 views
67

有谁知道如何在Android junit测试用例(扩展AndroidTestCase)中获得测试项目的上下文。在Android junit测试用例中获取测试项目的上下文

注:该测试不是仪器测试。

注2:我需要测试项目的上下文,而不是测试的实际应用程序的上下文。

我需要这个从测试项目的资产中加载一些文件。

+0

为什么你不能只使用InstrumentationTestCase? – yorkw 2011-12-22 19:49:33

+0

因为我正在测试服务,而不是UI。 – peceps 2011-12-23 16:48:07

+1

有一个更好的答案在这里找到: [使用AndroidTestCase,而不是一个JUnit测试] [1] [1]:http://stackoverflow.com/questions/3170706/how-do-you- get-hold-of-an-android-context-for-a-junit-test-from-a-java-project – 2012-08-10 19:23:43

回答

77

有与Android测试支持图书馆的新方法(目前com.android.support .test:runner:0.3)发布。

@RunWith(AndroidJUnit4.class) 
@MediumTest 
public class SomeClassTest { 

    private Context instrumentationCtx; 

    @Before 
    public void setup() { 
     instrumentationCtx = InstrumentationRegistry.getContext(); 
    } 

    @Test 
    public void someTest() { 
     ... 

如果您还希望应用程序上下文中运行:

InstrumentationRegistry.getTargetContext(); 

看吧:What's the difference between getTargetContext() and getContext (on InstrumentationRegistry)?

+0

无法编写共享首选项 – 2016-02-03 15:17:49

+0

最后回答如何在InstrumentationTest中使用JUnit4。经过数小时的搜索。喜欢Android开发。 – 2016-06-14 08:33:51

+1

不错! tx(请注意,在你的类成员中存在一个错字) – estoke 2016-09-23 10:00:47

37

经过一番研究,唯一的工作解决方案似乎是一个约克已经指出。你不得不延长InstrumentationTestCase,然后您可以访问使用getInstrumentation()测试应用程序的上下文的getContext() - 这里是使用上述建议一个简短的代码片段:

public class PrintoutPullParserTest extends InstrumentationTestCase { 

    public void testParsing() throws Exception { 
     PrintoutPullParser parser = new PrintoutPullParser(); 
     parser.parse(getInstrumentation().getContext().getResources().getXml(R.xml.printer_configuration)); 
    } 
} 
+7

是的,但似乎很愚蠢,Android在简单的JUnit测试中没有提供对测试项目上下文的访问。上下文在AndroidTestCase.mTestContext中存在,但它是私有的。我不明白为什么。 – peceps 2012-01-17 09:07:53

+0

@peceps完全确认 - 但多数民众赞成在它是如何,我也不喜欢它;) – AgentKnopf 2012-01-19 09:34:27

+0

@downvoters:如果downvote请花时间来解释为什么,这将有助于我和所有其他读者; ) – AgentKnopf 2013-12-09 08:53:30

24

正如你可以在AndroidTestCase source code阅读,getTestContext()方法被隐藏。

/** 
* @hide 
*/ 
public Context getTestContext() { 
    return mTestContext; 
} 

您可以使用反射绕过@hide注释。

只需添加下面的方法在您的AndroidTestCase

/** 
* @return The {@link Context} of the test project. 
*/ 
private Context getTestContext() 
{ 
    try 
    { 
     Method getTestContext = ServiceTestCase.class.getMethod("getTestContext"); 
     return (Context) getTestContext.invoke(this); 
    } 
    catch (final Exception exception) 
    { 
     exception.printStackTrace(); 
     return null; 
    } 
} 

然后调用getTestContext()你想要的任何时间。 :)

+2

工作完美的我,我加载资产使用上下文或AndroidTestCase通过这种方法,或ActivityInstrumentationTestCase2.getInstrumentation().getContext()然后getResources().getAssets() – 2013-08-24 16:56:23

+0

酷解决方案!我在扩展ProviderTestCase2的测试中使用您的解决方案。谢谢!。 – pcambre 2014-04-17 19:03:30

+0

你能猜测他们为什么隐藏它吗?如果我们使用这种技术,他们能否在以后的版本中使用这种方法(破坏我们的测试代码)? – 2014-11-12 23:38:44

0

如果你只需要访问到你的项目的资源,你可以使用getActivity()方法ActivityInstrumentationTestCase2 class

//... 
private YourActivityClass mActivity; 
@Override 
protected void setUp() throws Exception { 
//... 
    mActivity = getActivity(); 

} 
//... 
public void testAccessToResources() { 
    String[] valueList; 
    valueList = 
     mActivity.getResources().getStringArray(
       com.yourporject.R.array.test_choices); 
} 
0

其他的答案是过时的。现在,每当您扩展AndroidTestCase时,都可以使用mContext Context对象。