Java JUnit,更新时结果不变
我目前正在为我的Java8代码编写一些单元测试。我有个问题 在下面的代码中,您可以看到3个测试函数,第一个返回6个,这是正确的。然而,第二个也返回6,即使它应该返回16,第三个也返回6,即使它应该返回21 如果我注释掉第一个函数,第二个函数返回16,这是正确的。但是现在第三个函数返回16,而不是6,它应该返回21 我正在测试的代码是一个静态类Java JUnit,更新时结果不变,java,unit-testing,testing,junit,automated-tests,Java,Unit Testing,Testing,Junit,Automated Tests,我目前正在为我的Java8代码编写一些单元测试。我有个问题 在下面的代码中,您可以看到3个测试函数,第一个返回6个,这是正确的。然而,第二个也返回6,即使它应该返回16,第三个也返回6,即使它应该返回21 如果我注释掉第一个函数,第二个函数返回16,这是正确的。但是现在第三个函数返回16,而不是6,它应该返回21 我正在测试的代码是一个静态类 @Test public void testRun1() { Integer[][] input = { {6},
@Test
public void testRun1() {
Integer[][] input = {
{6},
{6, 6},
{5, 6},
{4, 6},
{3, 6},
{2, 6},
{1, 6},
};
int expResult = 6;
int result = BoardingTest.Run(input);
assertEquals(expResult, result);
}
@Test
public void testRun2() {
Integer[][] input = new Integer[][]{
{6},
{6, 6},
{6, 4},
{5, 6},
{3, 8},
{1, 9},
{2, 1},
};
int expResult = 16;
int result = instance.Run(input);
assertEquals(expResult, result);
}
@Test
public void testRun3() {
Integer[][] input = new Integer[][]{
{6},
{1, 1},
{2, 2},
{3, 3},
{4, 4},
{5, 5},
{6, 6},
};
int expResult = 21;
int result = BoardingTest.Run(input);
assertEquals(expResult, result);
}
Run的代码位于名为BoardingTest的类中,如下所示
public class BoardingTest {
// N
static int N;
// Total number of passengers seated
static int Seated = 0;
// Total time
static int TotalTime = 0;
// Shortest time for a passanger who is in the act of sitting down
static int ShortestTime = 999999;
// 1.st dimension = Seat, 2.nd = Time
static int[][] Passengers;
// Current passenger
static int Passenger = 0;
// The first seat that a person is in the act of sitting down in
static int FirstOccupied = 999999;
// Refference to the passenger who is in the act of sitting in the plane
static ArrayList<Integer> Plane = new ArrayList<Integer>();
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
}
public static int Run(Integer[][] input){
GetInput(input);
// Loop untill all passengers is seated
while(Seated < N){
Tick();
}
return(TotalTime);
}
// Reads all input
private static void GetInput (Integer[][] input){
N = input[0][0];
Passengers = new int[N][2];
for(int i = 0; i < N; i++){
SplitInput(i, input);
}
}
// Splits an inputline into Seat and Time
private static void SplitInput(int i, Integer[][] input){
Passengers[i][0] = input[i+1][0];
Passengers[i][1] = input[i+1][1];
}
// This handels what happens in a Tick
private static void Tick(){
// Keep putting passengers into the plane, until we can't do that anymore
InserPassengers();
TotalTime += ShortestTime;
// Remove 1 sec from all passangers in the plane
boolean removeOne = RemoveTimeFromPassengersInPlae(ShortestTime);
// If someone was removed, find the new FirstOccupied seat
if(removeOne){
FirstOccupied = FindFirstOccupiedSeat();
}
// End Tick
}
// Will insert as many passengers as posible into the plane
private static void InserPassengers(){
while(Passenger != N && FirstOccupied > Passengers[Passenger][0]){
// If this passengers takes les time to sit, update ShortestTime
if(Passengers[Passenger][1] < ShortestTime){
ShortestTime = Passengers[Passenger][1];
}
// Add this passenger to the plane
Plane.add(Passenger);
// Set FisrstOccupied
FirstOccupied = Passengers[Passenger][0];
// Increase the Passenger number
Passenger++;
}
}
// Will find the Fist Occupied seat
private static int FindFirstOccupiedSeat(){
int result = 999999;
for(Integer i : Plane){
if(Passengers[i][0] < result){
result = Passengers[i][0];
}
}
return result;
}
// Will remove some time from everyone in the plane
// And remove everyone who has finished sitting down
// And find the shortest time for the next one to sit
private static boolean RemoveTimeFromPassengersInPlae(int time){
ShortestTime = 999999;
boolean removed = false;
for(int i = Plane.size() - 1; i >= 0; i--){
// Remove time
Passengers[Plane.get(i)][1] -= time;
// If time is 0
// Remove Passanger
if(Passengers[Plane.get(i)][1] == 0){
Plane.remove(i);
removed = true;
Seated++;
}
// Set new Shortes Time
else if(Passengers[Plane.get(i)][1] < ShortestTime){
ShortestTime = Passengers[Plane.get(i)][1];
}
}
return removed;
}
private static void Print(String s){
System.out.println(s);
}
private static void Print(int i){
System.out.println(i);
}
}
好吧,玩了一段时间后我发现了问题 这是因为我调用的类是静态的
如果我错了,请纠正我,但我认为静态变量在多次调用同一个静态类时不会改变。请提供相关运行方法的代码。鉴于没有其他信息,我得出结论BoardingTest.Run实现了如下内容:返回输入[1][1]==6?6:16.提示:如果您测试的代码是正确的,但是被测试的方法返回了一些未通过测试的东西,那么您在被测试的方法中有一个bug,这个bug应该被修复。好的,现在我已经添加了要运行的代码。我的问题不是代码返回了错误的东西。问题是,当我运行测试时,变量'result'在test1之后的测试中没有更新为正确的值。如果是因为静态类的值没有改变或者我不知道。请阅读并遵循静态变量是类级别的变量,这意味着无论您为一个类创建了多少个对象,该类的所有静态变量都由所有对象共享。只有一个副本存在。因此,在这种情况下,您通过一个对象或第一次调用更新静态变量,第二次调用将看到更新的值大多数情况下看到更多关于volatile关键字和线程安全的信息,我在这里没有考虑到这一点,因此,如果第一次更新静态变量static int N=5时可能发生这种情况,下一次调用将看到它的值为5。