Testing custom Views with Robolectric

前端 未结 4 1346
别跟我提以往
别跟我提以往 2020-12-30 18:10

I\'m trying to run unit tests with Robolectric 2.1.1 and I cannot get it to inflate custom layouts (e.g. ViewPagerIndicator classes). Suppose this is my layout:

<         


        
4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-30 18:53

    I test views in the same test class with the Activity that uses them. In this case I tell Robolectric to give an instance of that Activity and from that I get an instance of the inflated view:

    @Before
    public void setup(){
        activity = Robolectric.buildActivity(MyActivity.class).create().get();
        View view = LayoutInflater.from(activity).inflate(R.layout.myView, null);
    }
    @Test
     public void allElementsInViewProduct(){
         assertNotNull(view.findViewById(R.id.view1));
         assertNotNull(view.findViewById(R.id.view2));
         assertNotNull(view.findViewById(R.id.view3));
     }
    

    LE: I use Robolectric 3.0 so I am not sure if this applies to you.

提交回复
热议问题