0

我目前正在 Eclipse ADT 中使用 Robolectrics 进行测试项目,该项目针对我的主要 android 应用程序进行测试。我正在使用 Junit 4。但是,我无法让它工作。谁能指出我的代码中的任何错误。代码如下。我真的很努力。这是错误跟踪:

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;
.....

..

谁能给我任何提示哪里出错了?或者任何人有更好的主意?

感谢您提前提供任何帮助。

4

2 回答 2

0

这可能已经晚了,但是您需要将android.jar其作为外部库添加到您的项目中,以便可以使用其类Robolectric重新实现它。Shadow

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

于 2013-11-11T12:07:06.107 回答
0

出于某种原因,Robolectric 没有找到 Android SDK。转到 testProject 的运行配置并将 Enviromnt 变量设置为:

ANDROID_HOME = C:\your\sdk\location

这对我有用。希望有帮助。

于 2013-02-18T16:24:41.703 回答