Saturday, 15 March 2014

java - How to run a junit test on a method with parameters and custom return object -



java - How to run a junit test on a method with parameters and custom return object -

i new junit , unit testing , start unit testing code. start method looks following:

public store loadstore(integer customerid, integer storeid){ //the logic of method selects store database based on parameter criteria }

from article http://www.vogella.com/articles/junit/article.html have read next assertions can made:

fail(string) asserttrue(true) asserttrue([message], boolean condition) assertsequals([string message], expected, actual) assertsequals([string message], expected, actual, tolerance) assertnull([message], object) assertnotnull([message], object) assertsame([string], expected, actual) assertnotsame([string],expected, actual)

i confused assertion utilize method. should utilize several? should utilize one? should trying prove unit testing of method?

/** * little illustration started. * illustration of test class test loadstore * assumes method defined in class called storemanager. * encapsulates tests required within same test method * expediency sake. should restrict 1 core functionality per test. * if writing functional test, can groupd core functionality together. * * provide default input data. provide info should pass * info should fail or error according possible conditions can think off. * * fine hardcode info when when starting out later need * defining test info in sort of reusable way easy maintain info changes. * * practice not run test against db databases. * * */ import static org.junit.assert.*; import static org.junit.assert.asserttrue; import static org.junit.assert.fail; import org.junit.after; import org.junit.before; import org.junit.beforeclass; import org.junit.test; public class storemanagertest { @beforeclass public void before() { //in here pre-initilization each test class if needed //database login if needed //mock objects if needed etc } @before public void beforetest() { //in here pre-initilization each test if needed } @after public void aftertest() { //in here cleanup each test if needed } @test public void testloadstore() { storemanager storemanager = new storemanager(); integer validcustomerid = new integer(9876); integer invalidcustomerid = new integer(-10); integer validstoreid = new integer(2345); integer invalidstoreid = new integer(-345); string validcustomername = new string("validname); string validstorename = new string("validstorename); //test first valid id. //expect homecoming store. if store undefined asserttrue fail. //if error fail. need invetigate why failing valid store id , valid client id. try{ store store = storemanager.loadstore(validcustomerid, validstoreid); assertnotnull(store); asserttrue(store.getstoreid().equals(validstoreid)); asserttrue("customer id match : " , store.getcustomerid().equals(validcustomerid)); //you can check other parameters exist in store object assertsame("customer name match : ", store.getcustomername(), validcustomername); assertsame("store name match : ", store.getstorename(), validstorename); }catch(exception e){ fail("testhashcode failed! " + e.getmessage()); } //test valid id , invalid customerid //expect error or null store. if store undefined asserttrue pass. //if error fail valid assume test passed can write other test test //whether error status expected. try{ store store = storemanager.loadstore(invalidcustomerid, validstoreid); assertnull(store); }catch(exception e){ asserttrue("no store matching : "+invalidcustomerid, true); } //test invalid storeid , valid customerid //expect error or null store. if store undefined asserttrue pass. //if error fail valid assume test passed can write other test test //whether error status expected. try{ store store = storemanager.loadstore(validcustomerid, invalidstoreid); assertnull(store); }catch(exception e){ asserttrue(true); } //test invalid storeid , invalid customerid //expect error or null store. if store undefined asserttrue pass. //if error fail valid assume test passed can write other test test //whether error status expected. try{ store store = storemanager.loadstore(null, null); assertnull(store); }catch(exception e){ asserttrue(true); } } }

java unit-testing junit junit4

No comments:

Post a Comment