Java 测试夹具不会初始化数组
我试图测试我的旅程类方法,但是,任何使用jArray字段的方法都会出现空指针异常,因为arraylist实际上还没有创建。我的印象是,我的测试夹具将解决这个问题,但问题仍然存在,任何帮助表示感谢Java 测试夹具不会初始化数组,java,Java,我试图测试我的旅程类方法,但是,任何使用jArray字段的方法都会出现空指针异常,因为arraylist实际上还没有创建。我的印象是,我的测试夹具将解决这个问题,但问题仍然存在,任何帮助表示感谢 private buscard.Journey tj1; private buscard.Journey tj2; /** * Default constructor for test class JourneyTest */ public Journey
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;
}
}
}
专用总线卡tj1;
私人公交卡。旅程tj2;
/**
*测试类JourneyTest的默认构造函数
*/
公共旅程测试()
{
tj1=新的总线卡行程(20120101120000,“74”,1);
tj2=新的总线卡行程(20120102120000,“74”,1);
ArrayList jArray=新的ArrayList();
jArray.add(tj1);
}
/**
*设置测试夹具。
*
*在每个测试用例方法之前调用。
*/
受保护的无效设置()
{
tj1=新的总线卡行程(20120101120000,“74”,1);
tj2=新的总线卡行程(20120102120000,“74”,1);
ArrayList jArray=新的ArrayList();
jArray.add(tj1);
}
public void testGetDate()
{
旅程测试J1=新旅程(20120101,12.00,“74”,1);
assertEquals(20120101,testJ1.getDate());
}
公共void testGetTime(){
旅程测试J1=新旅程(20120101,12.00,“74”,1);
assertEquals(12.0,testJ1.getTime());
}
public void testGetBusNumber(){
旅程测试J1=新旅程(20120101,12.00,“74”,1);
assertEquals(“74”,testJ1.getBusNumber());
}
公共void testConstructor(){
旅程测试J1=新旅程(20120101,12.00,“74”,1);
assertEquals(“74”,testJ1.getBusNumber());
assertEquals(12.0,testJ1.getTime());
assertEquals(20120101,testJ1.getDate());
}
}
包装总线卡;
导入java.util.ArrayList;
/**
*在这里写一篇课堂旅程的描述。
*
*@author(你的名字)
*@version(版本号或日期)
*/
班级旅行
{
公共int日期;
公众双倍时间;
公共字符串总线号;
公共int journeyType;
公共静态双日收费=0;
公共静态最终双maxDayCharge=3.50;
公共静态双周收费=0;
公共静态最终双maxWeekCharge=15;
公共静态双月收费=0;
公共静态最终双maxMonthCharge=48;
私人int日志编号;
私有静态int NumberOfTravelies=0;
公共旅行的双重成本;
公共静态ArrayList jArray;
公共行程(整数日期、双倍时间、字符串总线号、整数行程类型)
{
this.date=日期;
这个时间=时间;
this.busNumber=总线号;
this.journeyType=journeyType;
journeyNumber=++NumberOfJournes;
}
静止的
{
ArrayList jArray=新的ArrayList();
}
public int getDate(){
返回日期;
}
公共双getTime(){
返回时间;
}
公共字符串getBusNumber(){
返回总线号;
}
公共双倍旅行费用(){
返程费;
}
public int getJourneyType(){
返回日志类型;
}
公共布尔值isInSequence(整数日期,双倍时间){
旅程prevTourney=jArray.get(jArray.size()-1);
return((prevTourney.getDate() 如果(djArray
是旅程
类的静态字段。
你应该替换
ArrayList<Journey> jArray = new ArrayList<Journey>();
ArrayList jArray=new ArrayList();
与
buscard.travely.jArray=new ArrayList();
jArray
是旅程
类的静态字段。
你应该替换
ArrayList<Journey> jArray = new ArrayList<Journey>();
ArrayList jArray=new ArrayList();
与
buscard.travely.jArray=new ArrayList();
您使用什么测试框架?(另外:调用list jArray很奇怪-如果应该是jList真的!)您使用什么测试框架?(另外:调用list jArray很奇怪-如果应该是jList真的!)