2

Currently I'm learning FitNesse. I have two different test tables in one test page and want to Add/Remove quantities in my "InventoryFixture" tables.

My problem is that I can't get the value of the quantity from "InventoryFixture" table using "myInventory" object. My object is to ensure that I'm working with same object each time and refers to class "InventoryFixture".

I'm using java in the development and extends ColumnFixture.

enter image description here

In class "AddRemoveItemFixture", I get the new items and add them to my quantity which is in class "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:

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. myInventory.getQuantity() returns [0] instead of [28] so the total items is (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:

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;
         }
    }
Gilana
  • 111
  • 12

2 Answers2

0

The problem is the first FitNesse table creates a new instance of InventoryFixture, which is not the same instance as your static myInventory. So the first table does not update myInventory.

Mike Stockdale
  • 5,256
  • 3
  • 29
  • 33
  • Thanks, I see the problem now. Can you please help me more to solve this problem. I defined partNumber and quantity as public static variables in class InventoryFixture but still doesn't work. – Gilana Sep 22 '14 at 13:20
  • I would create a separate class with a static instance to hold the inventory - then both fixtures can use that instance to pass information – Mike Stockdale Sep 22 '14 at 15:17
0

I hope you know why myInventory.getQuantity() is returning 5 because when you say 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.

You may want to read about doFixture from uncle Bob's page http://butunclebob.com/FitNesse.UserGuide.FitLibraryUserGuide.DoFixture

Remember you can return a fixture from doFixture so you can potentially do anything with using that.

Ideally you should pass the previous values (in this case quantity 28, 1) to the new Fixture and return the added value. This will help understand the business stake holders as well as look clean.

Dheerajs83
  • 64
  • 4