简体   繁体   English

Android中的Robolectric:测试事件?

[英]Robolectric in Android: Testing events?

I have written a few tests using robolectric and I now want to do some real test classes. 我用robolectric写了一些测试,现在我想做一些真正的测试类。

One thing I notice is that I can't test the events like onCreate, onLocationChanged etc. 我注意到的一件事是我无法测试onCreate,onLocationChanged等事件。

What is the standard practice for testing the events? 测试事件的标准做法是什么?

Should iI extract the code that's inside the events and place them in a method, the event would call the method and also robolectro could call the method, of course the method would need to be public, right? 如果iI提取事件中的代码并将它们放在方法中,事件将调用该方法,并且robolectro也可以调用该方法,当然该方法需要公开,对吧?

Also if I wish to test something within my method that is normally a private variable then I would need to add a public getter, right? 另外,如果我想在我的方法中测试通常是私有变量的东西,那么我需要添加一个公共getter,对吧? Can I check this from robolectric? 我可以从robolectric检查一下吗?

Is there a better way to expose data to robolectric ? 有没有更好的方法将数据公开给robolectric?

When testing onCreate, I get robolectric to call onCreate and then test that the activity is in the correct state after onCreate. 当测试onCreate时,我得到robolectric来调用onCreate,然后在onCreate之后测试活动是否处于正确的状态。 Here's an example: 这是一个例子:

@RunWith(RoboTestRunner.class)
public class DashboardActivityTest {

    private DashboardActivity activity;

    @Before
    public void setUp() throws Exception {
        activity = new DashboardActivity();
    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void testDashboardHasButtons() {
        activity.onCreate(null);
        Button btn1= (Button) activity.findViewById(R.id.btn1);
        assertNotNull(btn1);
        Button btn2= (Button) activity.findViewById(R.id.btn2);
        assertNotNull(btn2);
    }
}

Testing private methods usually indicates your design could be improved and isn't a Robolectric specific problem. 测试私有方法通常表明您的设计可以改进,而不是Robolectric特定的问题。

See this question for lots of discussion: How do I test a class that has private methods, fields or inner classes? 请参阅此问题以进行大量讨论: 如何测试具有私有方法,字段或内部类的类?

As of v2 of Robolectric, this is not the proper way to start activities now: 截至Robolectric的v2,这不是现在开始活动的正确方法:

MyActivity testActivity = new MyActivity();
testActivity.onCreate(null);

Now the proper way is to use this: 现在正确的方法是使用这个:

MyActivity testActivity = Robolectric.buildActivity(MyActivity.class).create().get();

This will give you an instance of the activity after calling onCreate. 这将在调用onCreate后为您提供活动的实例。
If you want to test onStart, onResume, onPause, etc, it is the same way, just more methods. 如果你想测试onStart,onResume,onPause等,它的方法是相同的,只是更多的方法。

MyActivity testActivity = Robolectric.buildActivity(MyActivity.class).create().start().resume().pause().stop().destroy().get();  

(add or remove the methods in above line of code to test the exact instance of the activity you want) (添加或删除上面代码行中的方法以测试您想要的活动的确切实例)

Just wanted to clarify the really new nice feature of Robolectric. 只是想澄清一下Robolectric真正的新功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM