java - Having helper methods in junit class -


i want unit test class, class a, has dependency on class uses user session method. method want test not involve user session. can make helper method in class a replicate behavior of class b method need?

i know it's not clear, let's dig code clear understanding....

public void testdetails() throws exception {     //some logic generates detailstolist     savedetailshelper(detailstolist);     int detailssize = getdetailssize();     assertnotnull(detailssize); } 

now getdetailssize() size information database. class b has method, cannot create object of class b , test method since class b gets session information user, , can set session information junit class.

i have created savedetailshelper method replicates behavior of class b - savedetails() method, , calling in testdetails() method shown above.

my question:

  1. can have helper methods in junit class?
  2. what best approach deal situation?

junit classes function every other class in java; you're welcome have helper functions. thing determines whether other functions run (depending on version of junit) whether or not there annotation instructing harness run test.

however, problem precisely why should design classes carefully. if class b calls out database information, great opportunity compartmentalize functionality. don't clarify 'dependency' (are extending?), if class b component of class a, can create whole stub class stand in class b, , returns hardcoded (or otherwise coded) data testing of class a. long signature of class b proxy same, can have confidence class works.

it useful make these inner classes of test class, keep things getting messy in workspace.

public class testa {    @test   public void testdetails {     classa = new classa();     a.setdependency(new stubclassb());     //run test   }     private class stubclassb() extends classb {     public boolean savedetails() {       //return fake information;     }   } } 

Comments

Popular posts from this blog

jquery - Invalid Assignment Left-Hand Side -

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -