一个JUNIT4测试

/**
 * 
 */
package bookstore.testcase;

import static org.junit.Assert.assertEquals;

import org.junit.Before;
import org.junit.Test;

import bookstore.exception.StockLineEmptyException;
import bookstore.exception.StockLineExistedException;
import bookstore.model.Item;
import bookstore.model.Stock;


/**
 * @author badboy
 *
 */
public class TestStock {
	private Stock stock;
	Item stockLine1;
	Item stockLine2;
	Item stockLine3;
	Item stockLine4;
	
	@Before
	public void before() throws StockLineExistedException{
		stock = new Stock();
		stockLine1 = new Item("book1","This is book1",99.9f,100);
		stockLine2 = new Item("book2","This is book2",88,2);
		stockLine3 = new Item("book3","This is book3",99,1);
		stockLine4 = new Item("book4","This is book4",10,0);
		stock.createStockLine(stockLine1);
		stock.createStockLine(stockLine2);
		stock.createStockLine(stockLine3);
		stock.createStockLine(stockLine4);
	}
	
	@Test(expected=StockLineExistedException.class)
	/*
	 * test duplicate case
	 */
	public void testCreateStockLine() throws StockLineExistedException{
		stock.createStockLine(stockLine1);
	}
	


	@Test
	public void testGetStockLine(){
		String bookId = "book1";
		Item item = stock.getStockLine(bookId);
		assertEquals(bookId,item.getId());
		assertEquals("This is book1",item.getDescription());
		assertEquals(new Float(99.9f),new Float(item.getPrice()));
		assertEquals(100,item.getRemaining());
	}
	
	@Test
	public void testQuantityOperation() throws StockLineEmptyException{
		Item item = stock.getStockLine("book1");
		stock.removeQuantity(item,99);
		assertEquals(1,stock.getStockLine("book1").getRemaining());
		stock.addQuantity(item, 50);
		assertEquals(51,stock.getStockLine("book1").getRemaining());
	}
	@Test(expected = StockLineEmptyException.class)
	public void testQuantityException() throws StockLineEmptyException {
		Item item = stock.getStockLine("book3");
		stock.removeQuantity(item,2);
		
		
	}
	
}

你可能感兴趣的:(JUnit)