简体   繁体   English

如何使用Java对象和Fitnesse在多个测试表之间传输数据?

[英]How to transfer data between several test tables using java objects and Fitnesse?

Currently I'm learning FitNesse. 目前,我正在学习FitNesse。 I have two different test tables in one test page and want to Add/Remove quantities in my "InventoryFixture" tables. 我在一个测试页中有两个不同的测试表,并且想在“ InventoryFixture”表中添加/删除数量。

My problem is that I can't get the value of the quantity from "InventoryFixture" table using "myInventory" object. 我的问题是我无法使用“ myInventory”对象从“ InventoryFixture”表中获取数量的值。 My object is to ensure that I'm working with same object each time and refers to class "InventoryFixture". 我的目的是确保每次都使用相同的对象,并且引用类“ InventoryFixture”。

I'm using java in the development and extends ColumnFixture. 我在开发中使用Java,并扩展了ColumnFixture。

在此处输入图片说明

In class "AddRemoveItemFixture", I get the new items and add them to my quantity which is in class "InventoryFixture". 在“ AddRemoveItemFixture”类中,我获得了新物料并将其添加到“ InventoryFixture”类中的数量中。

it seems I'm missing something, can anyone tell me what? 似乎我缺少了什么,有人可以告诉我吗?

it seems I'm missing something, can anyone tell me what?

Here comes all my java code: 这是我所有的Java代码:

package fitNesseExample;
 import fit.ColumnFixture;

public class InventoryFixture extends ColumnFixture{
    private int quantity;
    private int partNumber;

    public int getQuantity(){
       return  quantity;
    }

    public void setQuantity(int quantity){
       this.quantity = quantity;
    }


    public int getPartNumber() {
       return partNumber;
    }

    public void setPartNumber(int partNumber) {
       this.partNumber = partNumber;
    }

    public boolean valid(){
        if(quantity>0){
            return true;
        }else{
            return false;
        }
     }

    public int addQuantities(int item){

       int items = item;
       return items;
     }

  }

In this class I add the new items by function addItems(), here comes the problem. 在此类中,我通过功能addItems()添加新项,问题来了。 myInventory.getQuantity() returns [0] instead of [28] so the total items is (0+5 = 5) NOT (28+5 = 33). myInventory.getQuantity()返回[0]而不是[28],因此项目总数为(0 + 5 = 5)NOT(28 + 5 = 33)。

package fitNesseExample;
 import fit.ColumnFixture;

 public class AddRemoveItemFixture  extends ColumnFixture{

    public int partNumber;
    public int newItems;
    public InventoryFixture myInventory;

    public void setNewItems(int newItems){
        this.newItems = newItems;
    }

    public void setPartNumber(int partNumber) {
        this.partNumber = partNumber;
    }

    public int addItems(){
      myInventory = StaticInventory.getMyInventory();
      int totalItems = myInventory.addQuantities(myInventory.getQuantity() + newItems);
      myInventory.setQuantity(totalItems);

      return myInventory.getQuantity();
    }

 }

I instantiate class InventoryFixture as: 我将InventoryFixture类实例化为:

package fitNesseExample;
   import fit.ColumnFixture;
   public abstract class StaticInventory extends ColumnFixture{

        public static InventoryFixture myInventory;
        public static InventoryFixture getMyInventory(){
           if(myInventory == null) myInventory = new InventoryFixture();
           return myInventory;
         }
    }

The problem is the first FitNesse table creates a new instance of InventoryFixture , which is not the same instance as your static myInventory . 问题在于,第一个FitNesse表创建一个新的InventoryFixture实例,该实例与您的静态myInventory实例myInventory So the first table does not update myInventory . 因此,第一个表不会更新myInventory

I hope you know why myInventory.getQuantity() is returning 5 because when you say myInventory = StaticInventory.getMyInventory(); 我希望您知道为什么myInventory.getQuantity()返回5,因为当您说myInventory = StaticInventory.getMyInventory();
It creates a new instance and assigns default value to its members. 它创建一个新实例,并为其成员分配默认值。

if you want to transfer data between fixtures then use doFixture and save the data in some global variable when executing first table and reuse from that global variable in the second table execution. 如果要在夹具之间传输数据,请在执行第一个表时使用doFixture并将数据保存在某个全局变量中,并在第二个表执行时从该全局变量中重用。

You may want to read about doFixture from uncle Bob's page http://butunclebob.com/FitNesse.UserGuide.FitLibraryUserGuide.DoFixture 您可能想从Bob叔叔的页面http://butunclebob.com/FitNesse.UserGuide.FitLibraryUserGuide.DoFixture阅读关于doFixture的信息。

Remember you can return a fixture from doFixture so you can potentially do anything with using that. 请记住,您可以从doFixture中返回固定装置,因此您可以使用该装置做任何事情。

Ideally you should pass the previous values (in this case quantity 28, 1) to the new Fixture and return the added value. 理想情况下,您应该将先前的值(在这种情况下为数量28、1)传递给新的灯具,并返回增加的值。 This will help understand the business stake holders as well as look clean. 这将有助于了解业务利益相关者并看起来干净。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM