在使用Robolectric测试Android应用程序时,如何创建影子类以解决与自定义属性相关的崩溃问题?
作者:互联网
我正在将a custom widget集成到我的项目中.它使用自定义属性并且正在崩溃Robolectric.从我收集到的内容,custom attributes aren’t supported yet.注意:构造函数是“敌对的”,因为它会在构造上抛出异常,但我现在已将它们注释掉了.
崩溃日志
java.lang.RuntimeException: error inflating layout/main
at com.xtremelabs.robolectric.res.ViewLoader.inflateView(ViewLoader.java:106)
at com.xtremelabs.robolectric.res.ViewLoader.inflateView(ViewLoader.java:82)
at com.xtremelabs.robolectric.res.ViewLoader.inflateView(ViewLoader.java:86)
at com.xtremelabs.robolectric.res.ResourceLoader.inflateView(ResourceLoader.java:377)
at com.xtremelabs.robolectric.shadows.ShadowLayoutInflater.inflate(ShadowLayoutInflater.java:43)
at com.xtremelabs.robolectric.shadows.ShadowLayoutInflater.inflate(ShadowLayoutInflater.java:48)
at android.view.LayoutInflater.inflate(LayoutInflater.java)
at com.xtremelabs.robolectric.shadows.ShadowActivity.setContentView(ShadowActivity.java:101)
at android.app.Activity.setContentView(Activity.java)
at com.blah.MainActivity.onCreate(MainActivity.java:17)
at com.blah.MainActivityTest.setUp(MainActivityTest.java:29)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)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
我正在努力解决这个问题,因为我不太关心测试这个小部件.基本上我希望我的测试不会崩溃并验证视图元素是否出现在屏幕上.
有人建议a hack of placing the java file in a android package,但我不确定它是否适用于我的情况.正如这个答案所暗示的那样,自定义小部件存在于“android”包中,它与我的com.blah结构平行.
我创建了一个小部件的阴影来解决敌意问题(但目前我只是注释掉异常抛出).最初我想绕过构造函数中正在进行的工作,因为它依赖于Robolectric未正确报告的属性.阴影构造函数被调用,但它继续通过正常的构造函数执行.有没有办法绕过额外的建设?
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 );
}
}
在测试框架支持此问题之前,这是解决崩溃的正确方法吗?我的阴影做错了还是我错过了别的什么?
解决方法:
所以我解决了这个问题.我将其放在一个单独的布局文件中,而不是将视图作为main.xml的一部分进行膨胀.在我的Activity的构造函数中,我在受保护的函数中夸大了布局.在我的测试类中,我扩展了测试类,而不是膨胀视图元素,我使用布尔来跟踪调用的函数.
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包中).
标签:android,unit-testing,robolectric 来源: https://codeday.me/bug/20191006/1857931.html