views:

51

answers:

3

i am new to junit so any help is appreciated.

I have a class called sysconfig.java for which i have written a junit class file called TestSysconfig.java that tests some methods in the sysconfig.java. The method that i am testing in sysconfig.java calls another class file "ethipmapping.java" i have created a mock of this class file as Testethipmapping.java. so my question is how do i tell sysconfig.java to call this mock object?

A: 

so my question is how do i tell sysconfig.java to call this mock object?
By calling it. Wherever you created 'ethipmapping' object before (new ethipmapping()), you'll have to create Testethipmapping.

There are other options not involving changes in sysconfig.java, but it's difficult to give specific advice without seeing the code.

Nikita Rybak
+2  A: 

instead of mixing "new" operators with the class you're testing, you'll need to pass in your test instance of ethipmapping to the sysconfig class you are testing, either in the constructor, or via a setter. so, your class you are testing will look something like:

   private EthipMapping mapping;
   public Sysconfig(EthipMapping mapping) {
        this.mapping = mapping;
    }

    public String someMethodIWantToTest() {
        return mapping.doSomeStuffThatReturnsAString();
    }

problems like this are why dependency injection frameworks like spring and google guice are so popular, although for simple cases like the above you don't need them.

Paul Sanwald
+1  A: 

You may be interested in Mockito.

Steven Sproat