java - Testing controllers using Spring, JUNIT, MockMvc and Hamcrest -
i trying test controller of mine returns me list of objects on method populate dropdown on page.
i trying write junit test using mockmvc , hamcrest test same.
i want compare list of objects , test if fails or not.
i have created static list of objects in test.java , getting list of objects model.attribute method.
to test: if both list of objects equal , don't contain other objects.
my object called option has 3 properties. key, value , selected. have check if keys exists in list or not.
i unable create matcher same. trying create matcher compare list.
so far have done following:
@before public void setup() throws exception { // build mockmvc following controller this.mockmvc = mockmvcbuilders.standalonesetup(openaccountcontroller) .build(); } @test public void testopenaccount() { try { setalllegislations(); this.mockmvc .perform(get("/open_account.htm")) // method used print out actual httprequest // , httpresponse on console. .anddo(print()) // checking if status 200 .andexpect(status().isok()) .andexpect( model().attributeexists("appformaccountplans", "appformliralegislations", "appformlrsplegislations", "appformrlsplegislations")) .andexpect( model().attribute("appformaccountplans", hassize(5))) .andexpect( model().attribute("appformliralegislations", hassize(8))) .andexpect( model().attribute("appformlrsplegislations", hassize(2))) .andexpect( model().attribute("appformrlsplegislations", hassize(1))) .andexpect( model().attribute( "appformliralegislations", haskeyfeaturematcher(getliralegislations(alllegislations)))); private matcher<list<option>> haskeyfeaturematcher( final list<option> expectedoptions) { return new featurematcher<list<option>, list<option>>( equalto(expectedoptions), "options are", "was") { @override protected list<option> featurevalueof(list<option> actualoptions) { boolean flag = false; if (actualoptions.size() == expectedoptions.size()) { (option expectedoption : expectedoptions) { (option actualoption : actualoptions) { if (expectedoption.getkey().equals( actualoption.getkey())) { flag = true; } else { flag = false; break; } } } } if (flag) return actualoptions; else return null; } }; } private list<option> getliralegislations(list<option> legislation) { list<option> liralegislations = new arraylist<option>(); iterator<option> iterator = legislation.iterator(); while (iterator.hasnext()) { option option = iterator.next(); if (lira_legislations.contains(option.getkey())) { liralegislations.add(option); } } return liralegislations; } private list<option> alllegislations; public list<option> getalllegislations() { return alllegislations; } public void setalllegislations() { alllegislations = new arraylist<option>(); (string key : all_legislations) { option option = new option(); option.setkey(key); alllegislations.add(option); } } private static final set<string> all_legislations = new hashset<string>( arrays.aslist(accountlegislationenum.ab.tostring(), accountlegislationenum.mb.tostring(), accountlegislationenum.nb.tostring(), accountlegislationenum.nl.tostring(), accountlegislationenum.ns.tostring(), accountlegislationenum.on.tostring(), accountlegislationenum.qc.tostring(), accountlegislationenum.sk.tostring(), accountlegislationenum.bc.tostring(), accountlegislationenum.fe.tostring(), accountlegislationenum.nt.tostring(), accountlegislationenum.pe.tostring(), accountlegislationenum.yt.tostring(), accountlegislationenum.nu.tostring(), accountlegislationenum.unknown.tostring()));
this how getting model attribute as:
attribute = appformliralegislations value = [com.abc.arch.core.gui.eform.gui.option@199d1739, com.abc.arch.core.gui.eform.gui.option@185fac52, com.abc.arch.core.gui.eform.gui.option@312a47fe, com.abc.arch.core.gui.eform.gui.option@4edc8de9, com.abc.arch.core.gui.eform.gui.option@71e8e471, com.abc.arch.core.gui.eform.gui.option@70edf123, com.abc.arch.core.gui.eform.gui.option@15726ac1, com.abc.arch.core.gui.eform.gui.option@abeafe7]
thanks in advance.
you can make life easier when correctly implement option
object hashcode()
, equals()
methods using key
attribute; can write:
model().attribute("appformliralegislations",getliralegislations(alllegislations)))
and rely on list1.equals(list2)
method work you.
option hashcode
, equals
implementation:
public class option { private string key; private string label; ... @override public int hashcode() { final int prime = 31; int result = 1; result = prime * result + ((key == null) ? 0 : key.hashcode()); return result; } @override public boolean equals(object obj) { if (this == obj) return true; if (obj == null) return false; if (getclass() != obj.getclass()) return false; option other = (option) obj; if (key == null) { if (other.key != null) return false; } else if (!key.equals(other.key)) return false; return true; }
}
methods above generated ide. don't know structure of option
class, add label
property example in addition key
property.
Comments
Post a Comment