Tapestry: How to test using JUnit4 when there is @InjectComponent -


i testing using junit4 eclipse. want test function expandall

public void expandall(treeexpansionmodel<treedata> expansionmodel)  {       list<treenode<treedata>> roots = gettreemodel().getrootnodes();     (treenode<treedata> root : roots)      {         expandallnode(root, expansionmodel);     }  }   private void expandallnode(treenode<treedata> node, treeexpansionmodel<treedata> expansionmodel)  {         if (node.gethaschildren())        {            expansionmodel.markexpanded(node);            (treenode child : node.getchildren())           {               expandallnode(child, expansionmodel); // recursive call            }         }  }  

the problem having expansionmodel. in program(not test), pass in expansionmodel using tree. here code fragment java.

@injectcomponent private tree tree;  public void onexpandall()  {      expansionmodel = tree.getexpansionmodel();      treefunction.expandall(expansionmodel);       ajaxresponserenderer.addrender(treezone);   } 

i have tried in test using

tree = new tree(); expansionmodel = tree.getexpansionmodel(); testing.expandall(expansionmodel); 

but expansionmodel null. how go testing @injectcomponent tree? appreciated. thanks.

unit testing of pages contain components can difficult, requires adding special constructors components required testing. becomes harder when components external source (ie tapestry-core).

have considered selenium testing instead? find unit testing pages requires lots of effort little gain.

if want unit test page, suggest refactor code isolate tree dependency:

@injectcomponent private tree tree;  public void onexpandall() {     onexpandall(tree.getexpansionmodel()); }  protected void onexpandall(treeexpansionmodel expansionmodel) {     treefunction.expandall(expansionmodel);     ajaxresponserenderer.addrender(treezone); } 

then can unit test second onexpandall method without needing tree instance using defaulttreeexpansionmodel or similar.


Comments