这是我的代码:
import android.test.suitebuilder.annotation.SmallTest;
import junit.framework.*;
public class ParametrizedStaticMapsRequestTest extends TestCase {
@SmallTest
public void testParametrizedStaticMapsRequest() {
ParametrizedStaticMapsRequest mapsRequest = new ParametrizedStaticMapsRequest.Builder()
.centerValue(new ParametrizedStaticMapsRequest.LatLngPoint(42.7000, 23.3333))
.imageFormat(ParametrizedStaticMapsRequest.GoogleMapsImageFormat.JPG)
.size(new ParametrizedStaticMapsRequest.SizeInPixels(320, 150))
.zoomLevel(new ParametrizedStaticMapsRequest.GoogleMapsZoomLevel(15))
.build();
System.out.println(mapsRequest.toString());
}
@SmallTest
public void testParametrizedStaticMapsRequestThrowingExceptionWithInvalidLatLng() {
try{
ParametrizedStaticMapsRequest mapsRequest = new ParametrizedStaticMapsRequest.Builder()
.centerValue(new ParametrizedStaticMapsRequest.LatLngPoint(422.7000, 233.3333))
.imageFormat(ParametrizedStaticMapsRequest.GoogleMapsImageFormat.JPG)
.size(new ParametrizedStaticMapsRequest.SizeInPixels(320, 150))
.zoomLevel(new ParametrizedStaticMapsRequest.GoogleMapsZoomLevel(15))
.build();
Assert.fail("Should have thrown IllegalArgumentException");
System.out.println(mapsRequest.toString());
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
// Success
}
}
@SmallTest
public void testParametrizedStaticMapsRequestThrowingExceptionWithNullLatLng() {
try{
ParametrizedStaticMapsRequest mapsRequest = new ParametrizedStaticMapsRequest.Builder()
.centerValue(null)
.imageFormat(ParametrizedStaticMapsRequest.GoogleMapsImageFormat.JPG)
.size(new ParametrizedStaticMapsRequest.SizeInPixels(320, 150))
.zoomLevel(new ParametrizedStaticMapsRequest.GoogleMapsZoomLevel(15))
.build();
Assert.fail("Should have thrown IllegalArgumentException");
System.out.println(mapsRequest.toString());
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
// Success
}
}
@SmallTest
public void testParametrizedStaticMapsRequestThrowingExceptionWithNullImageFormat() {
try{
ParametrizedStaticMapsRequest mapsRequest = new ParametrizedStaticMapsRequest.Builder()
.centerValue(new ParametrizedStaticMapsRequest.LatLngPoint(422.7000, 233.3333))
.imageFormat(null)
.size(new ParametrizedStaticMapsRequest.SizeInPixels(320, 150))
.zoomLevel(new ParametrizedStaticMapsRequest.GoogleMapsZoomLevel(15))
.build();
Assert.fail("Should have thrown IllegalArgumentException");
System.out.println(mapsRequest.toString());
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
// Success
}
}
@SmallTest
public void testParametrizedStaticMapsRequestThrowingExceptionWithInvalidSize() {
try{
ParametrizedStaticMapsRequest mapsRequest = new ParametrizedStaticMapsRequest.Builder()
.centerValue(new ParametrizedStaticMapsRequest.LatLngPoint(422.7000, 233.3333))
.imageFormat(ParametrizedStaticMapsRequest.GoogleMapsImageFormat.JPG)
.size(new ParametrizedStaticMapsRequest.SizeInPixels(2, -23))
.zoomLevel(new ParametrizedStaticMapsRequest.GoogleMapsZoomLevel(15))
.build();
Assert.fail("Should have thrown IllegalArgumentException");
System.out.println(mapsRequest.toString());
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
// Success
}
}
}
以下是实际输出:
LatLntPoint is mandatory and cannot be null
https://maps.googleapis.com/maps/api/staticmap?key=AIQ¢er=42.7,23.3333&format=jpg&zoom=15&size=320x150
Latitude should be within -90.0 and 90.0
Latitude should be within -90.0 and 90.0
Latitude should be within -90.0 and 90.0
Process finished with exit code 0
这是预期的输出:
LatLntPoint is mandatory and cannot be null
https://maps.googleapis.com/maps/api/staticmap?key=AIz_mORNS-0Q¢er=42.7,23.3333&format=jpg&zoom=15&size=320x150
Latitude should be within -90.0 and 90.0
Image format should not be null
Size should be within 180 and 600
Process finished with exit code 0
的build.gradle
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.1.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
build.gradle(app)
buildscript {
repositories {
maven { url 'https://maven.fabric.io/public' }
}
dependencies {
classpath 'io.fabric.tools:gradle:1.+'
}
}
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'
repositories {
maven { url 'https://maven.fabric.io/public' }
}
android {
compileSdkVersion 22
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.exmple"
minSdkVersion 16
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.0.0'
testCompile 'junit:junit:4.12'
compile 'com.android.support:recyclerview-v7:22.0.0'
compile 'com.android.support:cardview-v7:22.0.0'
}