java - 测试装置不会初始化数组

标签 java

我正在尝试测试我的旅程类方法,但是,任何使用 jArray 字段的方法都会给出空指针异常,因为实际上尚未创建 arraylist。我的印象是我的测试装置可以解决这个问题,但问题仍然存在,感谢任何帮助。

private buscard.Journey tj1;
    private buscard.Journey tj2;


    /**
     * Default constructor for test class JourneyTest
     */
    public JourneyTest()
    {
       tj1 = new buscard.Journey(20120101, 120000, "74", 1);
        tj2 = new buscard.Journey(20120102, 120000, "74", 1);
        ArrayList<Journey> jArray = new ArrayList<Journey>();
        jArray.add(tj1); 
    }

    /**
     * Sets up the test fixture.
     *
     * Called before every test case method.
     */
    protected void setUp()
    {
        tj1 = new buscard.Journey(20120101, 120000, "74", 1);
        tj2 = new buscard.Journey(20120102, 120000, "74", 1);
        ArrayList<Journey> jArray = new ArrayList<Journey>();
        jArray.add(tj1);
    }
    public void testGetDate()
    {
        Journey testJ1 = new Journey(20120101, 12.00, "74", 1); 
        assertEquals(20120101, testJ1.getDate());
    }
    public void testGetTime(){
        Journey testJ1 = new Journey(20120101, 12.00, "74", 1);
        assertEquals(12.0, testJ1.getTime());
    }
    public void testGetBusNumber(){
        Journey testJ1 = new Journey(20120101, 12.00, "74", 1);
        assertEquals("74", testJ1.getBusNumber());

    }
    public void testConstructor(){
       Journey testJ1 = new Journey(20120101, 12.00, "74", 1);
       assertEquals("74", testJ1.getBusNumber());
       assertEquals(12.0, testJ1.getTime());
       assertEquals(20120101, testJ1.getDate());
    }  

}
package buscard; 
import java.util.ArrayList;
/**
 * Write a description of class journey here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
class Journey
{
    public int date;
    public double time;
    public String busNumber;
    public int journeyType;
    public static double dayCharge = 0;
    public static final double maxDayCharge = 3.50;
    public static double weekCharge = 0;
    public static final double maxWeekCharge = 15;
    public static double monthCharge = 0;
    public static final double maxMonthCharge = 48;
    private int journeyNumber;
    private static int numberOfJourneys = 0;
    public double costOfJourney; 
    public static ArrayList<Journey> jArray;


    public Journey(int date, double time, String busNumber, int journeyType)
    {
        this.date = date;
        this.time = time;
        this.busNumber = busNumber;
        this.journeyType = journeyType;      
        journeyNumber = ++numberOfJourneys;

    }

    static
    {
      ArrayList<Journey> jArray = new ArrayList<Journey>();               
    }

    public int getDate(){
        return date;
    }  

    public double getTime(){
        return time;
    }

    public String getBusNumber(){
        return busNumber;
    }

    public double getCostOfJourney(){
        return costOfJourney;
    }

    public int getJourneyType(){
        return journeyType;
    }
    public boolean isInSequence(int date, double time){
        Journey prevJourney = jArray.get(jArray.size()-1);
        return((prevJourney.getDate() < date)||(prevJourney.getDate() == date && prevJourney.time < time));                       
    }           

    public static double returnLeast(){
        double d = maxDayCharge - dayCharge;
        double m = maxMonthCharge - monthCharge;
        double w = maxWeekCharge - weekCharge; 
        double least = 0;
        if (d <= w && d <= m)
        {
             least = d;
        }
        else if(w <= d && w <= m)
        {
             least = w;
        }
        else if(m <= d && m <= w)
        {
             least = m;
        }

        return least;
    }

        public double journeyCost(Journey reqJourney){                   
        if (journeyType == 1){                                 
            if (dayCharge <= 2.50 && weekCharge <= 14 && monthCharge <= 47)
            {
                costOfJourney = 1;
            }
            else 
            {
                costOfJourney = returnLeast();
            }

        }
        else if (journeyType == 2)
        {
            if (dayCharge <= 1.80 && weekCharge <= 13.30 && monthCharge <= 46.30)
            {
                costOfJourney = 1.70;
            }
            else 
            {
                costOfJourney = returnLeast();
            }
        }
        else if (journeyType == 3)
        {
            if (dayCharge <= 1.60 && weekCharge <= 13.10 && monthCharge <= 46.10)
            {
                costOfJourney = 1.90;
            }
            else 
            {
                costOfJourney = returnLeast();
            }

        }          
        return costOfJourney;    
    }

    public static void updateCurrentCharges(int date){
        int newDayOfYear = DateFunctions.getYearDay(date);
        int newWeekOfYear = DateFunctions.getYearWeek(date);
        int newMonthOfYear = DateFunctions.getYearMonth(date);

        if (newDayOfYear > WmBusPass.dayOfYear)
        {
            WmBusPass.dayOfYear = newDayOfYear;
            dayCharge = 0;
        }
        if (newWeekOfYear > WmBusPass.weekOfYear)
        {
            WmBusPass.weekOfYear = newWeekOfYear;
            weekCharge = 0;
        }
        if (newMonthOfYear > WmBusPass.monthOfYear)
        {
            WmBusPass.monthOfYear = newMonthOfYear;
            monthCharge = 0;
        }
    } 

}

最佳答案

jArrayJourney 类的静态字段。 您应该替换

    ArrayList<Journey> jArray = new ArrayList<Journey>();

    buscard.Journey.jArray = new ArrayList<Journey>();

关于java - 测试装置不会初始化数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10332317/

相关文章:

java - 如何从 WebDAV 服务器找到文件校验和(MD5 或...)

java - 持久化类中存在多个参数构造函数有什么原因吗?

java - 滑槽和梯子确定单元格是否为空

java - Spring MVC 未选择全局异常页面

java - JSON 和内存问题

java - 如何在 NetBeans 的简单 Java 项目中直接设置 Mockito

java - eclipse for Minecraft 中的 mods 的多个实例

java - 如何在java中的日志文件上获得彩色输出?

java - 在 Maven 项目中使用 OSGi 包的 ClassCastException

java - 在没有轮询的情况下观察变量的变化