我只是遇到了这个问题,我试图测试是否显示了片段,但是由于某些原因,我无法基于片段的根ID进行测试。这是代码:

MainActivity.java:

package com.gregspitz.simplefragmenttesttest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}


activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="vertical"
     tools:context="com.gregspitz.simplefragmenttesttest.MainActivity">

    <fragment
         android:id="@+id/main_fragment_holder"
         android:name="com.gregspitz.simplefragmenttesttest.BasicFragment"
         android:layout_width="match_parent"
         android:layout_height="match_parent"/>

 </LinearLayout>


BasicFragment.java:

package com.gregspitz.simplefragmenttesttest;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class BasicFragment extends Fragment {


    public BasicFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_basic, container, false);
    }

}


fragment_basic.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:tools="http://schemas.android.com/tools"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
              android:orientation="vertical"
              android:id="@+id/basic_fragment"
         tools:context="com.gregspitz.simplefragmenttesttest.BasicFragment">

    <TextView
        android:id="@+id/simple_text"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment"/>

</LinearLayout>


MainActivityTest.java:

package com.gregspitz.simplefragmenttesttest;

import android.support.test.rule.ActivityTestRule;

import org.junit.Rule;
import org.junit.Test;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static org.junit.Assert.*;

/**
 * Created by gregs on 2/6/2018.
 */
public class MainActivityTest {
    @Rule
    public ActivityTestRule<MainActivity> mActivityTestRule =
            new ActivityTestRule<MainActivity>(MainActivity.class);

    @Test
    public void mainActivity_basicFragmentIsDisplayedAtStart() {
        onView(withId(R.id.basic_fragment)).check(matches(isDisplayed()));
    }

    @Test
    public void mainActivity_basicFragmentInnerIdIsFound() {
        onView(withId(R.id.simple_text)).check(matches(isDisplayed()));
    }

}


第二个测试可以在我在布局内寻找ID的地方工作,但是第一个测试找不到布局的根ID。我只是不明白为什么这不起作用,我应该怎么做呢?谢谢您的帮助。

最佳答案

fragment在视图上膨胀时,Android框架会将fragment布局的根ID替换为fragment本身的ID。

LinearLayout的ID将更改为fragment的ID。

@Test public void mainActivity_basicFragmentIsDisplayedAtStart() { onView(withId(R.id.main_fragment_holder)).check(matches(isDisplayed())); }

09-04 10:44