SORU
20 ŞUBAT 2013, ÇARŞAMBA


Kullanın bazı yöntemler ama diğerleri alay Mockito

Herhangi bir şekilde, Mockito kullanarak, bir sınıf, ama diğerleri değil bazı yöntemler alay etmek var mı?

Örneğin, bu (kuşkusuz yapmacık) Stok sınıf istediğim için sahte getPrice() ve getQuantity() dönüş değeri (gösterildiği gibi test parçacığının) ama istediğim getValue() gerçekleştirmek için çarpma olarak kodlanmış Stok sınıf

public class Stock {
  private final double price;
  private final int quantity;

  Stock(double price, int quantity) {
    this.price = price;
    this.quantity = quantity;
  }

  public double getPrice() {
    return price;
  }

  public int getQuantity() {
    return quantity;
  }
  public double getValue() {
    return getPrice() * getQuantity();
  }

  @Test
  public void getValueTest() {
    Stock stock = mock(Stock.class);
    when(stock.getPrice()).thenReturn(100.00);
    when(stock.getQuantity()).thenReturn(200);
    double value = stock.getValue();
    // Unfortunately the following assert fails, because the mock Stock getValue() method does not perform the Stock.getValue() calculation code.
    assertEquals("Stock value not correct", 100.00*200, value, .00001);
}

CEVAP
24 Mayıs 2013, Cuma


Bir sınıfın kısmi alay yoluyla da desteklenmektedirCasusmockito

List list = new LinkedList();
List spy = spy(list);

//optionally, you can stub out some methods:
when(spy.size()).thenReturn(100);

//using the spy calls real methods
spy.add("one");
spy.add("two");

//size() method was stubbed - 100 is printed
System.out.println(spy.size());

Belgeleri mevcut ayrıntılı açıklama için here kontrol edin

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • backyardjay

    backyardjay

    8 ŞUBAT 2009
  • BigDawsVlogs

    BigDawsVlogs

    17 HAZİRAN 2013
  • spederson7

    spederson7

    17 Temmuz 2006