2011-12-22 134 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對象。