【问题标题】:Java JUnit, result not changing when updatedJava JUnit,更新时结果不变
【发布时间】:2018-07-22 16:42:19
【问题描述】:

我目前正在为我的 Java 8 代码编写一些单元测试。我有一个问题。

在下面的代码中,您可以看到 3 个测试函数,第一个返回 6,这是正确的。然而,第二个也返回 6,即使它应该返回 16,第三个也返回 6,即使它应该返回 21。

如果我注释掉第一个函数,第二个函数返回 16,这是正确的。但现在第三个函数返回 16 而不是 6,它应该返回 21。

我正在测试的代码是一个静态类。

@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);
}
}

【问题讨论】:

  • 请提供相关Run方法的代码。
  • 鉴于没有其他信息,我的结论是BoardingTest.Run 实现了这样的东西:return input[1][1] == 6 ? 6 : 16
  • 提示:如果你测试的代码是正确的,但是被测方法返回了一些没有通过测试的东西,那么你在被测方法中有一个错误,这个错误应该被修复。
  • 好的,现在我已经添加了运行代码。我的问题不是代码返回错误。问题是当我运行测试时,变量“result”在 test1 之后的测试中没有更新为正确的值。如果是因为静态类的值没有改变或者我不知道。
  • 请阅读(并关注)Java Naming Conventions

标签: java unit-testing testing junit automated-tests


【解决方案1】:

好的,玩了一会儿我发现了问题。

因为我调用的类是静态的。

如果我错了请纠正我,但我认为多次调用同一个静态类时静态变量不会改变。

【讨论】:

  • 静态变量是类级别的变量,意味着不管你为一个类创建了多少对象,类的所有静态变量被所有对象共享。只有一个副本存在。因此,如果您通过一个对象或第一次调用更新静态变量,第二次调用将看到更新后的值(大多数情况下,请参阅 volatile 关键字和 线程安全 我在这里不考虑)因此,如果第一次更新静态变量 static int N = 5 时可能发生,下一次调用将看到其值为 5。
猜你喜欢
  • 2023-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-13
  • 2017-12-14
  • 1970-01-01
  • 2015-10-10
  • 1970-01-01
相关资源
最近更新 更多