Asamplejunittestcasewealreadyhavebasicunderstandingonjunit howtocreateatestcase forgettingproductinformationsuchaspriceorinventorythroughadataprovider providerisrepresentedbythedataproviderinterface varietyofdatasources publicdoublegetprice publicintgetinventory publicbooleanexists publicclasspurchasemanager privatedataproviderdataprovider publicpurchasemanager this publicdoublegetprice thrownewproductnotfoundexception returndataprovider publicintgetinventory thrownewproductnotfoundexception returndataprovider publicdoublegettotal throwsproductnotfoundexception thrownewillegalargumentexception thrownewproductnotfoundexception productno intinventory thrownewnotenoughinventoryexception doubleprice returnprice publicbooleanexistsproduct thrownewillegalargumentexception returndataprovider sincethisisforunittesting dummydataproviderwhichusesamaptostoretestingdataasfollows importjava importjava publicclassdummydataproviderimplementsdataprovider privatemap publicdummydataprovider productproduct data product data publicdoublegetprice productproduct returnproduct else return- publicintgetinventory productproduct returnproduct else return- publicbooleanexists returndata classproduct stringproductno doubleprice intinventory product this this this stringgetproductno returnproductno doublegetprice returnprice intgetinventory returninventory oncetheexternaldatasourceisallset
A�SAMPLE�JUNIT�TEST�CASE
We�already�have�basic�understanding�on�JUnit.�Now,�let’s�use�a�simple�example�to�explain how�to�create�a�test�case.�The�following�example�has�a�manager�class�which�is�responsible for�getting�product�information�such�as�price�or�inventory�through�a�data�provider.�A�data provider�is�represented�by�the�DataProvider�interface.�It�can�be�implemented�based�on�a variety�of�data�sources.
import�static�org.junit.Assert.*;
�
import�org.junit.After;
import�org.junit.AfterClass;
import�org.junit.Before;
import�org.junit.BeforeClass;
import�org.junit.Test;
�
public�class�PurchaseManagerTest�{
private�static�DataProvider�provider;
private�PurchaseManager�purchaseMgr;
�
@BeforeClass
public�static�void�setUpClass()�{
provider�=�new�DummyDataProvider();
}
�
@AfterClass
public�static�void�tearDownClass()�{
provider�=�null;
}
�
@Before
public�void�setUp()�{
purchaseMgr�=�new�PurchaseManager(provider); }
�
@After
public�void�tearDown()�{
purchaseMgr�=�null;��������
}
�
@Test(expected=IllegalArgumentException.class)
public�void�nullProduct()�{
purchaseMgr.existsProduct(null);
}
�
@Test
public�void�nullProductException()�{
try�{
purchaseMgr.existsProduct(null);
fail();
}�catch(Exception�ex)�{
}
}���
�
@Test
public�void�existsProduct()�{