2015-03-05 2 views
0

я делал тест JUnit форумов для струнного класса здесь в чистой фасоли:Junit ToString Частичного покрытие

public String toString() { 
     try { 
      return "I am ContainerTruck " + getIdentifier() + ".\n\tI am at " 
        + getLocation() + " and am heading to " + getDestination() 
        + ".\n\tMy load is " + getCurrentLoadWeight() + " and my max load is " 
        + getMaxLoadWeight() + ".\n\tDistance to my destination is " 
        + String.format("%4.2f", distance(getDestination())) + ". " 
        + (atDestination() ? "I am there!" : "I'm not there yet"); 
     } catch (InvalidDataException ex) { 
      return ex.getMessage(); 
     } 
    } 

и NetBeans твердит я получаю частичное покрытие на моем тесте:

public void testToString(){ 

       double lX = 1.0; 
     double lY = 1.0; 
     double lZ = 1.0; 
     double dX = 1.0; 
     double dY = 1.0; 
     double dZ = 1.0; 
     double spd = 1.0; 
     double mxSpd = 1.0; 
     double mlw = 1.0; 
      try{ 
     ContainerTruck result = new ContainerTruck(lX, lY, lZ, dX, dY, dZ, spd, mxSpd, mlw); 
       IdentifiableImpl imp = new IdentifiableImpl(result.getIdentifier()); 

     assertNotNull(result); 
     assertEquals("I am ContainerTruck " + imp.getIdentifier() + ".\n\tI am at [1.00, 1.00, 1.00] and am heading to [1.00, 1.00, 1.00].\n\tMy load is 0.0 and my max load is 1.0.\n\tDistance to my destination is 0.00. I am there!", result.toString()); 
     assertEquals(imp.getIdentifier(),result.getIdentifier()); 
     assertEquals(0.0, result.getCurrentLoadWeight(), delta); 
     assertEquals(1.0, result.getLocationZ(), delta); 
     assertEquals(1.0, result.getLocationY(), delta); 
     assertEquals(1.0, result.getLocationX(), delta); 
     assertEquals(1.0, result.getMaxLoadWeight(), delta); 
     assertEquals(1.0, result.getDestinationX(), delta); 
     assertEquals(true, result.atDestination()); 
     assertEquals(1.0, result.getDestinationY(), delta); 
     assertEquals(1.0, result.getDestinationZ(), delta); 
     assertEquals(1.0, result.getSpeed(), delta); 
     assertEquals(1.0, result.getMaxSpeed(), delta); 
     }catch (InvalidDataException ex) { 
      fail(ex.getMessage()); 
     } 
    } 

может кто-то объясните мне, почему это все еще считается частичным охватом?

ответ

2

Ваш метод toString рассматривает только счастливый сценарий, где некоторые из ваших методов не генерируют исключение и, следовательно, никогда не попадают в блок catch.

Вы должны попробовать вызвать метод таким образом, чтобы вы попали в блок catch, где вы могли бы отправить сообщение об исключении.

+0

Ah Спасибо, я все еще думаю, что у меня проблемы с частичным покрытием. На основе netbeans частичное покрытие находится на линии строки, и я не понимаю, почему это так. – YummyTree

Смежные вопросы