本文介绍了如何创建一个影子类来解决相关的自定义属性崩溃测试与Robolectric一个Android应用程序是什么时候?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我整合自定义窗口小部件到我的项目。它使用自定义属性和崩溃Robolectric。从我收集,尚不支​​持 自定义属性。注:构造函数是敌对,因为它引发的建筑异常,但我已经注释出来,现在

I'm integrating a custom widget into my project. It uses custom attributes and is crashing Robolectric. From what I gather, custom attributes aren't supported yet. Note: the constructor is "hostile" because it throws exceptions on construction, but I've commented them out for now.

崩溃日志

了java.lang.RuntimeException:错误充气布局/主      在com.xtremelabs.robolectric.res.ViewLoader.inflateView(ViewLoader.java:106)      在com.xtremelabs.robolectric.res.ViewLoader.inflateView(ViewLoader.java:82)      在com.xtremelabs.robolectric.res.ViewLoader.inflateView(ViewLoader.java:86)      在com.xtremelabs.robolectric.res.ResourceLoader.inflateView(ResourceLoader.java:377)      在com.xtremelabs.robolectric.shadows.ShadowLayoutInflater.inflate(ShadowLayoutInflater.java:43)      在com.xtremelabs.robolectric.shadows.ShadowLayoutInflater.inflate(ShadowLayoutInflater.java:48)      在android.view.LayoutInflater.inflate(LayoutInflater.java)      在com.xtremelabs.robolectric.shadows.ShadowActivity.setContentView(ShadowActivity.java:101)      在android.app.Activity.setContentView(Activity.java)      在com.blah.MainActivity.onCreate(MainActivity.java:17)      在com.blah.MainActivityTest.setUp(MainActivityTest.java:29)      在org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:44)      在org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)      在org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)      在org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)

致:java.lang.ClassCastException:com.blah.support.shadows.ShadowMultiDirectionSlidingDrawer不能转换为com.xtremelabs.robolectric.shadows.ShadowView      在com.xtremelabs.robolectric.Robolectric.shadowOf(Robolectric.java:857)      在com.xtremelabs.robolectric.shadows.ShadowViewGroup.addView(ShadowViewGroup.java:70)      在android.view.ViewGroup.addView(ViewGroup.java)      在com.xtremelabs.robolectric.shadows.ShadowViewGroup.addView(ShadowViewGroup.java:60)      在android.view.ViewGroup.addView(ViewGroup.java)      在com.xtremelabs.robolectric.res.ViewLoader $ ViewNode.addToParent(ViewLoader.java:217)      在com.xtremelabs.robolectric.res.ViewLoader $ ViewNode.create(ViewLoader.java:180)      在com.xtremelabs.robolectric.res.ViewLoader $ ViewNode.inflate(ViewLoader.java:150)      在com.xtremelabs.robolectric.res.ViewLoader $ ViewNode.inflate(ViewLoader.java:153)      在com.xtremelabs.robolectric.res.ViewLoader.inflateView(ViewLoader.java:102)      ... 29个

Caused by: java.lang.ClassCastException: com.blah.support.shadows.ShadowMultiDirectionSlidingDrawer cannot be cast to com.xtremelabs.robolectric.shadows.ShadowView at com.xtremelabs.robolectric.Robolectric.shadowOf(Robolectric.java:857) at com.xtremelabs.robolectric.shadows.ShadowViewGroup.addView(ShadowViewGroup.java:70) at android.view.ViewGroup.addView(ViewGroup.java) at com.xtremelabs.robolectric.shadows.ShadowViewGroup.addView(ShadowViewGroup.java:60) at android.view.ViewGroup.addView(ViewGroup.java) at com.xtremelabs.robolectric.res.ViewLoader$ViewNode.addToParent(ViewLoader.java:217) at com.xtremelabs.robolectric.res.ViewLoader$ViewNode.create(ViewLoader.java:180) at com.xtremelabs.robolectric.res.ViewLoader$ViewNode.inflate(ViewLoader.java:150) at com.xtremelabs.robolectric.res.ViewLoader$ViewNode.inflate(ViewLoader.java:153) at com.xtremelabs.robolectric.res.ViewLoader.inflateView(ViewLoader.java:102) ... 29 more

我试图解决这个问题,因为我没有太在意测试这个小部件。基本上,我想我的测试没有崩溃,以验证视图元素出现在屏幕上。

I'm trying to work around this issue, because I don't care too much about testing this widget. Basically I want my tests not to crash and to verify that the view element is appearing on the screen.

有人建议a黑客将Java文件在Android包的,但我不知道这是否适用于我的情况。所建议的这个答案,自定义窗口小部件住在包机器人,这是平行于我com.blah结构。

Someone suggested a hack of placing the java file in a android package, but I'm not sure if it applies in my case. As suggested by this answer, the custom widget lives in package "android" which is parallel to my com.blah structure.

我创建的小部件的阴影,以避开敌意的问题(但目前我只是注释掉异常抛出)。本来我想绕过这是正在做的构造,因为它依赖于Robolectric未正确申报属性的工作。影子构造函数获取调用,但它仍然通过正常的构造函数执行。有没有一种方法可以绕过额外的建设?

I created a shadow of the widget to get around the hostility issue (but currently I just comment out the exception throwing). Originally I wanted to bypass the work that was being done in the constructor since it relies on attributes that Robolectric isn't reporting correctly. The shadow constructor is getting called, but it continues through normal constructor execution. Is there a way to bypass additional construction?

ShadowClass

@Implements (MultiDirectionSlidingDrawer.class)
public class ShadowMultiDirectionSlidingDrawer
{
    public void __constructor__( Context context, AttributeSet attrs )
    {
    }

    public void __constructor__( Context context, AttributeSet attrs, int defStyle )
    {
    }
}

自定义测试运行

public class CustomTestRunner extends RobolectricTestRunner
{
    public CustomTestRunner( Class<?> testClass ) throws InitializationError
    {
        super( testClass );
        addClassOrPackageToInstrument("android");
    }

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

这是正确的方法来解决的崩溃,直到测试框架支持呢?我做得不对我的影子,还是我失去了什么东西?

Is this the right approach to work around the crashes until the testing framework supports this? Am I doing something wrong with my shadow or am I missing something else?

推荐答案

所以,我这解决了一个简单的方法。相反,膨胀的观点为我的main.xml中的一部分,我把它放在一个单独的布局文件。在我活动的构造,我膨胀在受保护的功能布局。在我的测试类,我扩展的类测试和,而不是膨胀的视图元素,我用一个布尔值来跟踪,当调用的函数。

So I solved this a simple way. Instead of inflating the view as part of my main.xml, I put it in a separate layout file. In my Activity's constructor, I inflated the layout in a protected function. In my test class, I extended the class under test and instead of inflating the view element, I used a boolean to track that the functions as called.

MainActivity.java

public class MainActivity extends FragmentActivity
{
    @Override
    public void onCreate( Bundle savedInstanceState )
    {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.main );
        addOptionsShade();
    }

    protected void addOptionsShade()
    {
        ViewGroup viewGroup = (ViewGroup) findViewById( R.id.main_view );
        View view = getLayoutInflater().inflate( R.layout.options_shade, null );
        viewGroup.addView( view );
    }
}

MainActivityTest.java

@Test
public void shouldHaveOptionsShade() throws Exception
{
    assertTrue( mainActivity.hostileLibraryWasCalled );
}

class TestMainActivity extends MainActivity
{
    boolean hostileLibraryWasCalled = false;

    @Override
    protected void addOptionsShade()
    {
        hostileLibraryWasCalled = true;
    }
}

我去掉了阴影类和放大器;绑定,把图书馆早在说理的地方(而不是在Android包)。

I removed the shadow class & bindings and put the library back in a sensible place (not in android package).

这篇关于如何创建一个影子类来解决相关的自定义属性崩溃测试与Robolectric一个Android应用程序是什么时候?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 20:04