2013-01-16 160 views
0

我目前正在使用Eclipse ADT中的Robolectrics測試項目,該測試項目針對我的主要Android應用程序進行測試。我正在使用Junit 4.但是,我無法使其工作。任何人都可以指出我的代碼中有任何錯誤。代碼如下。我真的很掙扎。下面是錯誤跟蹤:使用Robolectrics框架對Android應用程序進行JUnit測試

java.lang.RuntimeException: java.lang.NumberFormatException: For input string: "FFFFFF 

    " 
    at com.xtremelabs.robolectric.res.ResourceLoader.init(ResourceLoader.java:148) 
    at com.xtremelabs.robolectric.res.ResourceLoader.setLayoutQualifierSearchPath(ResourceLoader.java:506) 
    at com.xtremelabs.robolectric.RobolectricTestRunner.setupApplicationState(RobolectricTestRunner.java:362) 
    at com.xtremelabs.robolectric.RobolectricTestRunner.internalBeforeTest(RobolectricTestRunner.java:307) 
    at com.xtremelabs.robolectric.RobolectricTestRunner.methodBlock(RobolectricTestRunner.java:285) 
    at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49) 
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) 
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) 
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) 
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) 
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) 
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236) 
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) 
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) 
Caused by: java.lang.NumberFormatException: For input string: "FFFFFF 

    " 
    at java.lang.NumberFormatException.forInputString(Unknown Source) 
    at java.lang.Long.parseLong(Unknown Source) 
    at com.xtremelabs.robolectric.res.ColorResourceLoader.convertRawValue(ColorResourceLoader.java:44) 
    at com.xtremelabs.robolectric.res.ColorResourceLoader.convertRawValue(ColorResourceLoader.java:9) 
    at com.xtremelabs.robolectric.res.ResourceReferenceResolver.processResource(ResourceReferenceResolver.java:29) 
    at com.xtremelabs.robolectric.res.ColorResourceLoader.processNode(ColorResourceLoader.java:38) 
    at com.xtremelabs.robolectric.res.XpathResourceXmlLoader.processResourceXml(XpathResourceXmlLoader.java:27) 
    at com.xtremelabs.robolectric.res.DocumentLoader.loadResourceXmlFile(DocumentLoader.java:58) 
    at com.xtremelabs.robolectric.res.DocumentLoader.loadResourceXmlDir(DocumentLoader.java:52) 
    at com.xtremelabs.robolectric.res.DocumentLoader.loadResourceXmlDir(DocumentLoader.java:39) 
    at com.xtremelabs.robolectric.res.ResourceLoader.loadValueResourcesFromDir(ResourceLoader.java:233) 
    at com.xtremelabs.robolectric.res.ResourceLoader.loadValueResourcesFromDirs(ResourceLoader.java:227) 
    at com.xtremelabs.robolectric.res.ResourceLoader.loadValueResources(ResourceLoader.java:170) 
    at com.xtremelabs.robolectric.res.ResourceLoader.init(ResourceLoader.java:132) 
    ... 19 more 



    @RunWith(CustomTestRunner.class) 
public class AccessActivity_Test{ 

    private AccessActivity activity; 
    private Button signup; 
    private Button signupwithFB; 

    @Before 
    public void setUp() { 

     activity = new AccessActivity(); 
     activity.onCreate(null); 
     //MockitoAnnotations.initMocks(this); 
     //FrameLayout layout = (FrameLayout) activity.findViewById(R.layout.access_main); 
     signup= (Button)activity.findViewById(R.id.btn_signup); 
     signupwithFB = (Button) activity.findViewById(R.id.btn_signupFB); 
     //frag = Robolectric.shadowOf(activity.getSupportFragmentManager().findFragmentById(R.id.container)); 
    } 

    @After 
    public void tearDown() { 
    } 

    @Test 
    public void TestSignUpButton()throws Exception{ 

     signup.performClick(); 


    } 

    @Test 
    public void TestSignUpWithFBButton(){ 
     signupwithFB.performClick(); 

    } 

此處是自定義的TestRunner:

public class CustomTestRunner extends RobolectricTestRunner { 

    public CustomTestRunner(Class<?> testClass) throws InitializationError { 
     super(testClass); 
     addClassOrPackageToInstrument("com.actionbarsherlock.app.SherlockActivity"); 
     addClassOrPackageToInstrument("com.actionbarsherlock.app.SherlockFragmentActivity"); 
    } 


    @Override 
    protected void bindShadowClasses() { 
     super.bindShadowClasses(); 
     Robolectric.bindShadowClass(ShadowSherlockActivity.class); 
     Robolectric.bindShadowClass(ShadowSherlockFragmentActivity.class); 
    } 
} 

這裏是活動:

public class AccessActivity extends RoboSherlockFragmentActivity { 

    private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item"; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.access_main); 

     // Set up the action bar. 
     getSupportActionBar().show(); 

     getSupportFragmentManager().beginTransaction() 
       .replace(R.id.container, new AccessOptionsFragment()) 
       .commit(); 
    } 

    @Override 
    public void onRestoreInstanceState(Bundle savedInstanceState) { 
     if (savedInstanceState.containsKey(STATE_SELECTED_NAVIGATION_ITEM)) { 
      getSupportActionBar().setSelectedNavigationItem(
        savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM)); 
     } 
    } 

    @Override 
    public void onSaveInstanceState(Bundle outState) { 
     outState.putInt(STATE_SELECTED_NAVIGATION_ITEM, 
       getSupportActionBar().getSelectedNavigationIndex()); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     MenuInflater inflater = getSupportMenuInflater(); 
     inflater.inflate(R.menu.main_menu, menu); 
     return true; 
    } 
} 

這裏是該片段的代碼:

public class AccessOptionsFragment extends RoboSherlockFragment implements 
     AnimationListener, OnClickListener { 
    // @InjectView(tag = "logo_layout") 
    // LinearLayout logo_layout; 
    // @InjectView(tag = "signup_layout") 
    // LinearLayout signup_layout; 
    //result codes if other than standard 
    //@InjectView(tag = "signup_layout")LinearLayout signup_layout; 

    public final static int LOGIN_REQUESTED = 1; 
    public final static int SIGNUP_REQUESTED = 2; 
    public final static int SIGNUPFB_REQUESTED = 3; 
    public final static int SKIPACCESS_REQUESTED = 4; 

    private Animation fadeInAnimation; 
..... 

.. 

任何人都可以給我任何暗示的地方它出錯了?或者任何一個有更好的主意?

感謝您提前提供任何幫助。

+0

當你說它不起作用時,是否有錯誤?有什麼症狀?另外,我認爲片段缺少一些代碼。 – Joe

+0

片段在主應用程序中運行良好。我只是從中得到了一些代碼。我在我的問題中附加了錯誤跟蹤,Joe –

+0

Robolectric通過在java之上重新實現android庫來工作。在某些情況下,重新實現有缺陷或不完整。這可能是您看到問題的原因。它看起來像加載資源XML文件時遇到問題。試着找出當xml資源文件給它的問題。然後,您可以查看[Robolectric資源](http://pivotal.github.com/robolectric/)並查看它是否正確實施。另外,請確保您使用的是最新版本的Robolectric。 – Joe

回答

0

由於某種原因,Robolectric未找到Android SDK。 轉到您testProject的運行配置,並設置Enviromnt變量:

ANDROID_HOME = C:\您的\ SDK \位置

爲我工作。希望hel。。

0

這可能已經很晚了,但您需要將android.jar作爲外部庫添加到您的項目中,以便Robolectric可以使用它的Shadow類重新實現它。

您可以在sdk\platforms\android-xx\android.jar找到jar文件。

+0

你現在刪除了你的問題嗎? – Rohit