我有以下的套餐和课程:
MainActivity
向用户显示map
,并使用markers
填充该markers
。
通过GetSailorsTask
GetSailorsTask
我尝试做的是在GoogleMap
中测试所述方法,该方法接收List<Marker>
和GMaps markers
mock map
作为参数。
问题是我无法找到创建mock markers
和MainActivity
的方法。
所以我接下来尝试的是启动生成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);
}
}
答案 0 :(得分:0)
根据Fox in socks
的建议,我使用了非模拟方法。
开始活动后,我使用以下内容来掌握MainActivity
中使用的地图:
FragmentManager fm = getActivity().getSupportFragmentManager();
gmap = ((SupportMapFragment) fm.findFragmentById(com.blindmatchrace.R.id.map))
.getMap();
之后我可以发送gmap
作为参数。
总结的步骤:
整个测试:
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();
}
}
}