如何在Android单元测试中用模拟数据填充适配器?

时间:2018-10-14 10:53:59

标签: java android mockito android-espresso

在我的应用程序中,我从服务器获取了一些数据并显示到RecyslerView中。
我可以获取数据并填充adapter并显示到RecyclerView中,但是我想用 Mockito Test 为此编写测试,然后填充adapter以显示到{{1 }}。

我将此链接用于json: https://api.learn2crack.com/android/jsonandroid/

我写下面的代码,但是我不知道如何为此写 Test

我的活动课程:

RecyclerView

我的适配器类:

public class MainActivity extends AppCompatActivity {

    private RecyclerView list;
    private Context context;
    private ApiServices apiServices;
    private List<Android> model = new ArrayList<>();
    private RecyclerAdapter adapter;

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

        context = this;
        apiServices = ApiClient.getClient().create(ApiServices.class);
        list = findViewById(R.id.list);
        list.setLayoutManager(new LinearLayoutManager(context));
        list.setHasFixedSize(true);
        adapter = new RecyclerAdapter(model);
        list.setAdapter(adapter);

        Call<AndroidResponse> call = apiServices.getAndroid();
        call.enqueue(new Callback<AndroidResponse>() {
            @Override
            public void onResponse(Call<AndroidResponse> call, Response<AndroidResponse> response) {
                if (response.isSuccessful()) {
                    if (response.body() != null) {
                        model.clear();
                        model.addAll(response.body().getAndroid());
                        adapter.notifyDataSetChanged();
                    }
                }
            }

            @Override
            public void onFailure(Call<AndroidResponse> call, Throwable t) {

            }
        });
    }
}

我的测试班级:

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
    private List<Android> model;
    public RecyclerAdapter(List<Android> model) {
        this.model = model;
    }
    @NonNull
    @Override
    public RecyclerAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_list, parent, false);
        return new ViewHolder(view);
    }
    @Override
    public void onBindViewHolder(@NonNull RecyclerAdapter.ViewHolder holder, int position) {
        holder.name.setText(model.get(position).getName());
        holder.version.setText(model.get(position).getVer());
        holder.api.setText(model.get(position).getApi());
    }
    @Override
    public int getItemCount() {
        return model.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView name, version, api;
        public ViewHolder(View itemView) {
            super(itemView);

            name = itemView.findViewById(R.id.rowName);
            version = itemView.findViewById(R.id.rowVersion);
            api = itemView.findViewById(R.id.rowApi);
        }
    }
}

我将 json 上方的内容保存到@RunWith(AndroidJUnit4.class) public class MainActivityTest extends InstrumentationTestCase { @Rule public ActivityTestRule<MainActivity> activityTest = new ActivityTestRule<>(MainActivity.class, true, false); private MockWebServer mockWebServer; @Before public void setUp() throws Exception { super.setUp(); mockWebServer = new MockWebServer(); mockWebServer.start(); injectInsrumentation(InstrumentationRegistry.getInstrumentation()); AppConstants.BASE_URL = mockWebServer.url("/").toString(); } @Test public void setupAndroidListAdapterTest() throws Exception { } @After public void tearDown() throws Exception { mockWebServer.shutdown(); } } 文件夹中。

我不知道如何将测试写到asset中以填充setupAndroidListAdapterTest(),并显示模拟数据adapter中。

我该怎么办?

1 个答案:

答案 0 :(得分:0)

模拟此方法,用假数据model.addAll(response.body().getAndroid());

填充模型