Testing custom Views with Robolectric

前端 未结 4 1344
别跟我提以往
别跟我提以往 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:56

    mTestRoboActivityView = LayoutInflater.from(new Activity()).inflate(R.layout.test, null);

    In this line of code you used 'new Activity()' means instance of new Activity, that not for your current Activity. You can resolve this issue by passing instance on current Activity. Use like this-

    public class TestRoboActivityTest {
    private View mTestRoboActivityView;
    private Context mContext;
    
    public TestRoboActivityTest(Context mContext){
        this.mContext=mContext;
    }
    
    @Before
    public void setUp() throws Exception {
        mTestRoboActivityView = (LayoutInflater.from(mContext)).inflate(R.layout.test, null);
    }
    
    @After
    public void tearDown() throws Exception {
        mTestRoboActivityView = null;
    }
    
    @Test
    public void testSanity() throws Exception {
        Assert.assertNotNull(mTestRoboActivityView);
    }}
    

    I am not sure that above code working fine but use for reference, instance of current Activity. Refer it may be help you.

提交回复
热议问题