-1

I have a class A which has a private integer member b. Here is the structure of the class:

public class A {

    private int b= Integer.parseInt(C.getValue(anyString));

    public int getB() {
       return b;
    }

    public void setB(int b) {
       this.b= b;
    }

}

I am testing some method of this class using jmockit with junit4 `

public class ATest {

    @Tested
    A a;

    @BeforeClass
    public void setUp() {
       //mock C.getValue
    }

    @Test public void test1() {
       //some test code
    }
}

but I am unable to mock the call (C.getValue(anyString)) thus the test fails while building the project. I have tried using @Before and @BeforeClass but none of those executed even once. Please suggest a way out. Considering I can't change the main class A.

talex
  • 17,973
  • 3
  • 29
  • 66
sbajpai
  • 1
  • 3

3 Answers3

0

First of all the way you are doing this is not very good since you are not able to control values that are returned from class C. But if you want to keep your code like this, you can extract

C.getValue(anyString)

to new protected function in your class

public class A {

   private int b= Integer.parseInt(getValue(anyString));

   public int getB() {
      return b;
   }

   public void setB(int b) {
      this.b= b;
   }

   protected String getValue(String anyString) {
      return C.getValue(anyString);
   }
}

After that you can in your test clas can create new private class that extends class A and then override its

getValue(String anyString)

function.

In test class:

public class ATest extends A {

    @Override protected String getValue(String anyString) {
      return "return 1 or any other string reperesentation of number";
   }
}

Function getValue(String anyString) in that, let's call it AText class, then can return value of your choice. This way you'll have controlled environment and you can test this new ATest class.

TheTechWolf
  • 2,084
  • 2
  • 19
  • 24
0

The root cause is that you code violates the Single Responsibility/Separation of Concerns principle. It is also a case of the work in constructor code smell.

Your class A pretty much looks like a Data Transfer Object (DTO) Its only responsibility is to carry data. Fetching the data from the database should be done by some framework outside the class and the data should be passed in from the outside.

Timothy Truckle
  • 15,071
  • 2
  • 27
  • 51
  • Actualy it is an action class in struts but i totally agree to your point. Problem is I didn't write that code and I do not have authority to change that. I am only confined to the test class. Anyways thanks for the view :) – sbajpai Nov 14 '17 at 09:59
  • @sbajpai *"I do not have authority to change that."* Then you should raise this issue to you manager. This should be handled as a *failed test*. In worst Case you can use PowerMock but this is only the **last resort**. – Timothy Truckle Nov 14 '17 at 10:11
0

Problem solved. Just needed to add static to the setUp() method in @BeforeClass and mocked the call. Not the right way to do it but I was only allowed to change the test class.

sbajpai
  • 1
  • 3
  • You should still raise that issue to your manager. This coding style will slow down development in the long run because it makes the code highly coupled and thus inflexible, hard to extend and hard to maintain. – Timothy Truckle Nov 14 '17 at 10:15