测试接收Google地图的方法

时间:2014-05-15 13:10:48

标签: android testing

我有以下的套餐和课程:

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

MainActivity向用户显示map,并使用markers填充该markers。 通过GetSailorsTask

中的方法从远程服务器检索GetSailorsTask

我尝试做的是在GoogleMap中测试所述方法,该方法接收List<Marker>GMaps markers mock map作为参数。

问题是我无法找到创建mock markersMainActivity的方法。 所以我接下来尝试的是启动生成map的{​​{1}},然后以某种方式访问​​map并将其作为参数发送。

有什么想法吗?

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

}

}

1 个答案:

答案 0 :(得分:0)

根据Fox in socks的建议,我使用了非模拟方法。

开始活动后,我使用以下内容来掌握MainActivity中使用的地图:

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

之后我可以发送gmap作为参数。

总结的步骤:

  1. 扩展ActivityInstrumentationTestCase2
  2. 开始包含地图的活动。
  3. 抓住地图
  4. 将地图作为参数发送到测试中的函数
  5. 整个测试:

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