0
私は2つの場所間の距離を計算する関数のテストケースを作成しようとしています。Androidユニットテストは常に成功する
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);
// locationA.setLatitude(30.050922);
// locationA.setLongitude(31.243414);
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);
}
}
}
が、これは私が実際の距離を変更すると、テストがあっても、常に渡し
public class FullMapScreenTest {
private FullMapScreen mapScreen ;
private Place place ;
// 30.050922, 31.243414
// 30.050976, 31.243494
@Before
public void setUp() throws Exception {
place = new Place(30.050922 , 31.243414) ;
}
@After
public void tearDown() throws Exception {
}
@Test(expected = NullPointerException.class)
public void calculateDistance() throws Exception {
/// 30.054291, 31.242572
double destLat = 30.054291 ;
double desLang = 31.242572 ;
double actualDistance = 100.00;
Place distanation = new Place(destLat ,desLang) ;
Assert.assertEquals(mapScreen.calculateDistance(distanation) ,actualDistance);
}
}
テストするという私のTestClass
です。
私は@Test(予想= NullPointerException.class)を削除したときに取得しないようにしたい場合、私はAhmedNu'man @ nullポインタ例外 –
に直面しましたNullPointerExceptionは、setUpメソッドの初期mapScreenをお願いします –
今、私はこのエラーに直面しましたjunit.framework.AssertionFailedError:期待通り:<0 null>はありませんでした:<200.0> 期待:0 null 実際:200.0 –