Having helper methods in junit class

10,486

Solution 1

JUnit classes function like every other class in java; you're welcome to have helper functions. The only thing that determines whether other functions will be run is (depending on the version of junit) whether or not there is an annotation instructing the harness to run it as a test.

However, this problem is precisely why you should design your classes carefully. If Class B calls out to a database for information, this is a great opportunity to compartmentalize that functionality. You don't clarify 'dependency' (are you extending?), but if Class B is a component of Class A, you can create a whole stub class that is a stand in for Class B, and returns hardcoded (or otherwise coded) data specifically for the testing of class A. As long as the signature of the Class B proxy is the same, you can have confidence Class A works.

It is often useful to make these inner classes of your test class, to keep things from getting too messy in your workspace.

public class testA {

  @Test
  public void testDetails {
    ClassA a = new ClassA();
    a.setDependency(new StubClassB());
    //run your test
  }


  private class StubClassB() extends ClassB {
    public boolean saveDetails() {
      //return fake information;
    }
  }
}

Solution 2

Helper methods will not have the @Testcase annotation. You would write the test code just like any other code, but would include only the relevant methods for testing, and validate only relevant portions of that test method.

Solution 3

You can put the helper method in the test class, omit the Testcase annotation and call it where needed (ad hoc per method or in your setup if it's needed for all tests). But probably a better solution is to use something like JMock to represent Class B and give back desired results.

Share:
10,486
Rachel
Author by

Rachel

I am here for learning and I learn different aspects of Computer Science from the Valuable Answers which I get from Stackoverflow Users. Thank you SO Community. I owe my knowledge to you.

Updated on June 12, 2022

Comments

  • Rachel
    Rachel almost 2 years

    I want to unit test a class, Class A, but it has a dependency on another class that uses a user session method. The method that I want to test does not involve user session. Can I make helper method in Class A that would replicate the behavior of the Class B method that I need?

    I know it's not clear, let's dig some code to get clear understanding....

    public void testDetails() throws Exception
    {
        //Some logic that generates DetailsTOList
        saveDetailsHelper(DetailsTOList);
        int detailsSize = getDetailsSize();
        assertNotNull(detailsSize);
    }
    

    Now getDetailsSize() get size information from a database. Class B has this method, but I cannot create an object of Class B and test this method since Class B gets session information from User, and I can set session information from JUnit class.

    I have created saveDetailsHelper method that replicates behavior of Class B - saveDetails() method, and am calling that in testDetails() method as shown above.

    My question:

    1. can we have helper methods in junit class?
    2. What is best approach to deal with this situation?
  • Nathaniel Ford
    Nathaniel Ford almost 12 years
    Your prose is a bit jumbled. You should try and use shorter sentences. Short sentences make it easy for the reader to know where they lose you. Also, it might help if you actually give names to the methods you're discussing. This makes it easier to follow your logic. Finally, the basic idea is to create a class that acts like B should act for the test input defined, and use that to test A. However, it doesn't need to function just like B does: it can parrot back dumb responses. It just needs to look like B as far as A is concerned.
  • Rachel
    Rachel almost 12 years
    I am not extending class B with Class A. From class B I call method of class A and pass in some arguments to that method. Those Arguments are calculated by methods present in class B. In my unit test I cannot make reference to class B and call those methods as Class B get's user session. Even though those methods in question do not deal with user session, I cannot use them as I cannot create objects of class B. So, I just replicated that behavior in helper methods in my unit test class, hope this clarifies (Modified so it's more readable...)
  • Nathaniel Ford
    Nathaniel Ford almost 12 years
    Can you create a class that extends B and overrides the methods you need from Class B? Or is construction of Class B the issue?
  • Rachel
    Rachel almost 12 years
    I can create class that extends B but is it worth to create new class just for the sake of testing purpose. Why not just replicate method in question, in the unit class?
  • Nathaniel Ford
    Nathaniel Ford almost 12 years
    It depends how your code is built. If A is not actually dependent on B, just calls B statically or a third party calls B and reports that information to A, then creating a mock class for B isn't necessary. If A is actually dependent on B, it's better to create a mock class because then A is behaving the same as if B where actually there.