سؤال

I've got the following packages and classes:

  • com.blindmatchrace
    • MainActivity.java
  • com.blindmatchrace.classes
    • GetSailorsTask.java

MainActivity presents a map to the user and populates it with markers. The markers are retrieved from a remote server by a method in GetSailorsTask

What I'm trying to do is test said method in GetSailorsTask, which receives a GoogleMap and a List<Marker> of GMaps markers as parameters.

Problem is I couldn't find a way to create a mock map and mock markers. so what I tried next was to start the MainActivity which spawns the map and then somehow access the map and send it as a parameter.

Any ideas?

public class GetSailorsTaskTest extends ActivityInstrumentationTestCase2<MainActivity> {

Activity activity;

public GetSailorsTaskTest() {
    super(MainActivity.class);
    // TODO Auto-generated constructor stub
}

@Override
protected void setUp() throws Exception {
    super.setUp();
    Intent intent = new Intent();
    intent.putExtra(C.USER_NAME, "Sailortest");
    intent.putExtra(C.USER_PASS, "1234");
    intent.putExtra(C.EVENT_NUM, "1234");
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    setActivityIntent(intent);

    activity = getActivity();

}

public void testPreConditions() {
    assertNotNull(activity);

}

public void testGetSailorsTask(){
    GetSailorsTask getSailors = new GetSailorsTask("GetSailorsTask",  googleMap, sailorMarkers, fullUserName, event);
    getSailors.execute(C.URL_CLIENTS_TABLE);

}

}

هل كانت مفيدة؟

المحلول

Following Fox in socks's advice I used a non mock approach.

After starting the activity I used the following to get hold of the map being used in MainActivity:

FragmentManager fm = getActivity().getSupportFragmentManager();
gmap = ((SupportMapFragment) fm.findFragmentById(com.blindmatchrace.R.id.map))
.getMap();

After which I was able to send gmap as a parameter.

The steps summarized:

  1. Extend ActivityInstrumentationTestCase2
  2. Start the activity containing the map.
  3. Get hold of the map
  4. Send the map as a parameter to the function under test

The entire test:

package com.blindmatchrace.test;

import android.app.Activity;
import android.content.Intent;
import android.test.ActivityInstrumentationTestCase2;

import com.blindmatchrace.*;
import java.util.ArrayList;
import java.util.List;

import com.blindmatchrace.classes.C;
import com.blindmatchrace.classes.GetSailorsTask;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.Marker;
import android.support.v4.app.*;

public class GetSailorsTaskTest extends ActivityInstrumentationTestCase2<MainActivity> {

    Activity activity;
    GoogleMap gmap;
    List<Marker> sailorMarkers;

    public GetSailorsTaskTest() {
        super(MainActivity.class);
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        Intent intent = new Intent();
        intent.putExtra(C.USER_NAME, "Sailortest");
        intent.putExtra(C.USER_PASS, "1234");
        intent.putExtra(C.EVENT_NUM, "1234");
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        setActivityIntent(intent);

        activity = getActivity();

    }

    public void testPreConditions() {
        assertNotNull(activity);

    }

    public void testGetSailorsTask() throws Exception{

        FragmentManager fm = getActivity().getSupportFragmentManager();
        gmap = ((SupportMapFragment) fm.findFragmentById(com.blindmatchrace.R.id.map)).getMap();

        sailorMarkers = new ArrayList<Marker>();

        GetSailorsTask getSailors = new GetSailorsTask("GetSailorsTask",  gmap, sailorMarkers, "Sailoruser2004_2004_2004", "1234");
        getSailors.execute(C.URL_CLIENTS_TABLE);

        if(getSailors.getException() != null){
            throw getSailors.getException();
        }

    }



}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top