簡體   English   中英

在Java中模擬多級數組以進行單元測試

[英]Mock multiple level arrays in java for unit testing

假設我有

class Box{
   Item[] items;
}

class Item{
   Toy[] toys;
}

class Toy{
   Color[] colors;
}

方法是測試盒子是否有綠色玩具;

public bool testHasColor(Box){
   //There will be code here that
   //Streams over each type till we get to color
   // Final point is
   if(color==Color.GREEN){
    return true;
   }
}

BoxText.class

public void testBoxHasColorGreenMethod(){
    // Need mocking here
}

在測試用例中模擬Box類的最佳方法是什么?

謝謝

您不需要模擬任何Java API。 您只需要“模擬”一些通常不稱為“模擬”的數據-只是測試即可。 因此,您只需創建Box的測試實例,然后測試您的方法是否產生了預期的結果。

    Box box = new Box();
    Item item = new Item();
    Toy toy = new Toy();

    box.items = new Item[] {item};
    item.toys = new Toy[] {toy};
    toy.colors = new Color[] {Color.RED, Color.GREEN};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM