How to fill adapter with mock data in unit test on

2019-06-23 23:32发布

In my application I get some data from server and show into RecyslerView.
I can get data and fill adapter and show into RecyclerView, but I want write Test for this with Mockito Test and fill adapter for show into RecyclerView.

I use this link for json : https://api.learn2crack.com/android/jsonandroid/

I write below codes but I don't know how can I write Test for this.

My Activity class :

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) {

            }
        });
    }
}

My Adapter class :

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);
        }
    }
}

My Test class :

@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();
    }
}

I saved above json into asset folder.

I don't know how can I write test into setupAndroidListAdapterTest() for fill adapter and show mock data into recyclerView.

How can I do it?

1条回答
来,给爷笑一个
2楼-- · 2019-06-23 23:58

Mock this Method to fill your Model with fake Data model.addAll(response.body().getAndroid());

查看更多
登录 后发表回答