5 Replies Latest reply: Oct 1, 2009 4:42 AM by 807580 RSS

    JUnit test case

    807580
      Hi everyone;
      I would like to write a JUnit test for this class, but I don't know how to use arrayList in my JUnit.

      This is my class:
      import java.util.ArrayList;
      import java.util.*;
      
      public class Stock {
      
           /**
            * Create a collection of items.
            */
           public ArrayList<Item> stockLine = new ArrayList<Item>();
           Iterator<Item> stock = stockLine.iterator();
      
      
      
           public Item name, price, quantity;
      
           public Stock() {
                createStockLine();
           }
      
           //Create a new Item.
      
           public void createStockLine() {
                Item item = new Item("h",25,10);
                stockLine.add(item);
                Item item2 = new Item("Anna Karerina", 25, 10);
                stockLine.add(item2);
                Item item3 = new Item("The Count of Monte Cristo", 50, 10);
                stockLine.add(item3);
                Item item4 = new Item("Remembering Babylon", 25, 10);
                stockLine.add(item4);
                Item item5 = new Item("Pride and Prejudice", 10, 10);
                stockLine.add(item5);
           }
      
           public void removeStockLine(String itemName) {
                Item i = stock.next();
                while (stock.hasNext()) {
                     i.getName().equals(itemName);
                }
                stockLine.remove(itemName);
      
           }
      
           public void addQuantity(String itemName, int amount) {
                Item i = stock.next();
      
                while (stock.hasNext()){
                i.getName().equals(itemName);
                }
                     i.addQuantity(amount);
                     }
      
      
           public void removeQuantity(String itemName, int amount) {
                Item i = stock.next();
      
                while (stock.hasNext()){
                i.getName().equals(itemName);
                }
                     i.removeQuantity(amount);
                     }
      
           }
      Do you think I should add a getter method find the quantity of a specific item in the arrayList ?

      Thank you,
      Henri
        • 1. Re: JUnit test case
          807580
          Why is that ArrayList public? Hide it! The fact that line items are kept in an ArrayList is an implementation detail, it's nobody else's business, least of all your unit tests. Somewhere down the line, you might completely change your mind about that, and have to refactor your test as well as your code. Pointless. The unit test should not give a toss how the Stock class actually does what it does, only that it does it. Should you add a method to expose how many of a particular item there is in stock? That's entirely up to you, that's what we call a requirement. If you do it, your test should add a known number of a particular item to the Stock object, and ensure that the method reports back the right figure. Don't forget to ensure that taking them away affects the figure, and that adding other types of item doesn't affect it, that sort of thing.
          • 2. Re: JUnit test case
            807580
            I didn't know if I had to put my array list public or private, I changed it now.

            this is my JUnit test for one method :
            I also have a class Item (create new Item, and getter method for the price, the quantity and the name of the item)
            public class StockTest {
                 Stock st;
                 Item it;
            
                 @Before
                 public void setUp() throws Exception {
                      st = new Stock();
                 }
            
                 @After
                 public void tearDown() throws Exception {
                 }
            
                   @Test
                 public void testAddQuantity() {
                      st.addQuantity("h", 2);
                      assertEquals(12, it.getName());
                 }
            I think that my error is when I called the method getName(), it doesn't refer to any item.

            Do I have to use the a indexOf() and then getName() ? or something similar ?

            Thanks
            • 3. Re: JUnit test case
              807580
              Henri90 wrote:
              I didn't know if I had to put my array list public or private, I changed it now.

              this is my JUnit test for one method :
              I also have a class Item (create new Item, and getter method for the price, the quantity and the name of the item)
              public class StockTest {
                   Stock st;
                   Item it;
              
                   @Before
                   public void setUp() throws Exception {
                        st = new Stock();
                   }
              
                   @After
                   public void tearDown() throws Exception {
                   }
              
              @Test
                   public void testAddQuantity() {
                        st.addQuantity("h", 2);
                        assertEquals(12, it.getName());
                   }
              I think that my error is when I called the method getName(), it doesn't refer to any item.

              Do I have to use the a indexOf() and then getName() ? or something similar ?
              Nope. That's what we call white-box testing. Don't do it. I think your design is a bit wrong anyway. How does one add a quantity? Looks like you're making mad wild guesses here. Slow down, and think about what you're trying to get the class to do
              • 4. Re: JUnit test case
                807580
                In my JUnit test I wrote :

                Set up the constructor of my stock class, which will create the collection of items. Then take the item h, and finally add 2 to its quantity. Finally verify if the quantity available in stock of the item h has been update to twelve.


                I don't understand ! Is it my getName method which is wrong ?
                • 5. Re: JUnit test case
                  807580
                  Henri90 wrote:
                  I don't understand ! Is it my getName method which is wrong ?
                  I dunno. So far you haven't really indicated that anything is actually wrong