首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >array.length在junit中抛出NullPointerException,但在运行应用程序时不抛出

array.length在junit中抛出NullPointerException,但在运行应用程序时不抛出
EN

Stack Overflow用户
提问于 2020-03-06 12:21:39
回答 1查看 51关注 0票数 1

我遇到了一个奇怪的问题。我对编码还很陌生,所以这可能源于我对数组本质的误解。我的问题是:我试图为我的应用程序中的某些数组编写单元测试,其中我调用了arr.length,然后抛出一个NPE。数组是在类的构造函数中初始化的,所以这不是问题所在。我已经尝试过在每个索引上使用值初始化它们,以查看这是否改变了什么,但没有改变。最让我困惑的是,当我在同一个数组上调用arr.length时,只要运行应用程序,就会得到一个值。

我有什么关于JUnit的东西吗?

正在测试的方法是不完整的。当我开始遇到NPE时,我正在编写它,所以我从来没有真正得到分配值给数组的代码。下面是测试和正在测试的类:

代码语言:javascript
复制
@Test
void testCFArray() throws IOException {
    WebReaderFilter adder = new WebReaderFilter();
    Stock stock = new Stock("INTC");
    stock.setProfile(adder.getStockDetails(stock.getTicker()));

//此方法调用引发NPE

代码语言:javascript
复制
    stock.setArrays(stock.getProfile());
    BigInteger[] arr = stock.getCashFlowArray();
    assertEquals(BigInteger.valueOf(33145000000L), arr[0]);
}

package companyValueModel;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.HashMap;

public class Stock {

    private String ticker;
    private BigDecimal currentPrice;

    private BigInteger[] freeCashFlow;
    private BigInteger[] cashFlow;
    private BigInteger[] debt;
    private BigDecimal[] divPerShare;

/*
 * Keys: [companyName], [EBITDA], [Enterprise Value], [description], [industry],
 * [yearHigh], [price], [Total shareholders equity], [Goodwill and Intangible
 * Assets], [Capital Expenditure], [sector], [yearLow], [marketCap], [Dividend
 * payments], [ticker], [Revenue Growth], [Cash and short-term investments],
 * [Net Income], [Revenue]
 * 
 * Array keys: [Long-term debt-1], [Free Cash Flow0], [Long-term debt-2],
 * [Long-term debt0], [Free Cash Flow-2], [Dividend per Share0], [Free Cash
 * Flow-1], [Dividend per Share-1], [Operating Cash Flow0], [Operating Cash
 * Flow-1], [Operating Cash Flow-2]
 * 
 * keys with numbers at the end represent (0) = this year, (-1) = year prior,
 * etc.
 */

    private HashMap<String, String> profile;

    public Stock(String ticker) {
        this.ticker = ticker;
    }

    public Stock(HashMap<String, String> profile) {
        this.profile = profile;
        this.ticker = profile.get("ticker");
        freeCashFlow = new BigInteger[3];
        cashFlow = new BigInteger[3];
        debt = new BigInteger[3];
        divPerShare = new BigDecimal[2];
    }

    public HashMap<String, String> getProfile() {
        return profile;
    }

    public void setProfile(HashMap<String, String> profile) {
        this.profile = profile;
    }

    public String getTicker() {
        return ticker;
    }

    public void setCurrentPrice(BigDecimal price) {
        currentPrice = price;
    }

    public BigDecimal getCurrentPrice() {
        return currentPrice;
    }

    public void setArrays(HashMap<String, String> profile) throws NumberFormatException {
        int j = 0;
//      this line throws the NPE.  It disappears if I replace cashFlow.length with 3

        for (int i = 0; i < cashFlow.length; i++) {

//          This line was to verify that the key existed (it does)

            System.out.println(profile.get("Operating Cash Flow" + j));

//          also as an aside if anybody could tell me whether I'm parsing this string properly to bigInt I would appreciate it.

            double flow = Double.parseDouble(profile.get("Operating Cash Flow" + j));
            BigInteger cf = BigInteger.valueOf((long) flow);
            j--;

// Here is where the assigning code would have gone

        }
    }

    public BigInteger[] getCashFlowArray() {
        return cashFlow;
    }

    public BigInteger[] getFreeCashFlowArray() {
        return freeCashFlow;
    }

    public BigInteger[] getDebtArray() {
        return debt;
    }

    public BigDecimal[] getDivPerShare() {
        return divPerShare;
    }
}

我突然想到,我可以为单元测试编写一个类似的常量方法,并在程序中使用另一个方法,但这将使该方法没有适当的单元测试,这不会使我感到不快。最后,可能没有必要使用数组,但我认为它们稍后会很方便。我要求加深我的理解,这样如果我再次遇到这个问题,我就知道需要做什么。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-06 12:38:52

在测试用例中创建Stock对象时,您将使用字符串参数构造函数。

->初始化cashFlow在.

代码语言:javascript
复制
public Stock(String ticker) {
    this.ticker = ticker;
    cashFlow = new BigInteger[3]; // add this
}

->在测试用例中初始化Stock时使用其他HashMap参数构造函数。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60563973

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档