如何在TestNG中测试匿名类返回的私有方法

huangapple go评论84阅读模式
英文:

How to test a private method when anonymous class return in TestNG

问题

以下是您要求的翻译内容:

我需要使用TestNG来测试StockPrice类中的**getPrice()**方法,并覆盖priceCalculator()方法内的代码。

public class StockPrice {

  public double getPrice(){
    Stock stock = getStock();
    return finalPrice(stock,1000,true);
  }

  private double finalPrice(Stock stock, int price, boolean isDiscountAvailable){
    return stock.priceCalculator(price,isDiscountAvailable)-100;
  }

  private Stock getStock(){
    return new Stock()
    {
      @Override
      public double priceCalculator(int price, boolean isDiscountAvailable)
      {
        if (isDiscountAvailable){
          return price-price/10;
        }else {
          return price;
        }
      }
    };
  }
}

interface Stock{
  double priceCalculator(int price, boolean isDiscountAvailable);
}

我尝试以几种方式覆盖1个priceCalculator()方法中的代码行,但由于以下原因未成功:

  • getStock()方法是私有方法,因此无法访问它。我尝试使用反射来访问它,但出现了java.lang.NoSuchMethodException:错误。
  • getStock()返回一个匿名对象。然后无法覆盖priceCalculator()方法中的代码行。

您能否为我提供覆盖priceCalculator方法中代码的解决方案?

总之,我需要覆盖以下代码:

@Override
public double priceCalculator(int price, boolean isDiscountAvailable)
{
  if (isDiscountAvailable){
    return price-price/10;
  }else {
    return price;
  }
}
英文:

I need to test getPrice() method in StockPrice class by using TestNG. and cover the lines inside the priceCalculator() method

public class StockPrice {

  public double getPrice(){
    Stock stock = getStock();
    return finalPrice(stock,1000,true);
  }

  private double finalPrice(Stock stock, int price, boolean isDiscountAvailable){
    return stock.priceCalculator(price,isDiscountAvailable)-100;
  }

  private Stock getStock(){
    return new Stock()
    {
      @Override
      public double priceCalculator(int price, boolean isDiscountAvailable)
      {
        if (isDiscountAvailable){
          return price-price/10;
        }else {
          return price;
        }
      }
    };
  }
}

interface Stock{
  double priceCalculator(int price, boolean isDiscountAvailable);
}

I tried to cover the lines inside the 1 priceCalculator() method in several ways but did not succeed due to the following reasons

  • getStock() method is a private method so cannot access it. I tried with using reflections to access it but got java.lang.NoSuchMethodException:
  • getStock() returns and Annonymous object. Then cannot cover the lines priceCalculator() method

can you give me a solution to cover the lines in the priceCalculatormethod.

in summary, I need to cover the following code.

@Override
public double priceCalculator(int price, boolean isDiscountAvailable)
{
  if (isDiscountAvailable){
    return price-price/10;
  }else {
    return price;
  }
}

答案1

得分: 1

通过公共方法进行测试。

Stock 类不访问任何外部且较慢的资源,因此您不需要模拟任何内容。

@Test
public void getPrice_returns_expected_price() {
    StockPrice stockPrice = new StockPrice();

    double actual = stockPrice.getPrice();

    assertEquals(800, actual);
}
英文:

Test it through public method.

Stock class doesn't access any external and slow resources, so you need to mock nothing.

@Test
public void getPrice_returns_expected_price() {
    StockPrice stockPrice = new StockPrice();

    double actual = stockPrice.getPrice();

    assertEquals(800, actual);
}

huangapple
  • 本文由 发表于 2020年10月10日 03:48:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/64286402.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定