//第一次使用Mockito FrameWork
进行测试。当我使用AssertEqual
时,我需要知道为什么它总是通过。我想测试CalculateDistance
它作为参数发生。当我使用假数据它总是通过
@RunWith(MockitoJUnitRunner.class)
public class FullMapScreenTest extends InstrumentationTestCase {
Context context;
Parcel parcel;
@Override
public void setUp() throws Exception {
super.setUp();
}
@Test
public void TestcalculateDistance() {
FullMapScreen fullMapScreen = mock(FullMapScreen.class);
Place place = new Place(30.0508883, 31.2432764);
place.distance = 1000.00;
Place fakeplace = new Place(33.0508883, 31.2432764);
fakeplace.distance = 0;
String expectedDistance = fullMapScreen.calculateDistance(place);
String fakeDistance = fullMapScreen.calculateDistance(fakeplace);
Assert.assertEquals(expectedDistance, fakeDistance);
Assert.assertEquals(fullMapScreen.calculateDistance(place),
fullMapScreen.calculateDistance(fakeplace));
}
}
测试方法
public String calculateDistance(Place p) {
if (p.distance > 0) {
if (selectedPlace.distance > 1000) {
return Long.toString(Math.round(p.distance) / 1000)
+ getString(R.string.km);
} else {
return Long.toString(Math.round(p.distance))
+ getString(R.string.meter);
}
} else {
Location locationA = new Location("Point A");
locationA.setLatitude(MainActivity.lat);
locationA.setLongitude(MainActivity.lng);
Location locationB = new Location("point B");
locationB.setLatitude(p.getLatitude());
locationB.setLongitude(p.getLongitude());
double actualDistance = locationA.distanceTo(locationB);
if (actualDistance > 1000) {
return Long.toString(Math.round(actualDistance) / 1000)
+ " " + getString(R.string.km_away);
} else {
return Long.toString(Math.round(actualDistance))
+ " " + getString(R.string.meter_away);
}
}
}