1

I am using EasyMock, after debug, I found that the issue is in A.java > while calling getResults(val) from dbconnection object.

dbconnection object is null, my question is how to Mock the objects in super class ?

Here is the simple code:

A.java

public class A {

    @Autowired
    DataBaseConnection dbconnection;

    public Result methodInA(String val)() {
        result = new Result();
        result = dbconnection.getResults(val);  
        return result;
    };
}

B.java

public class B extends A {

    public Fresult getfinalResult(String value) {

        // Fresult class contains 'Result' class object and some other variables
        Fresult fresult = new Fresult();
        // some logic here...and I was able to exeduted this by creating mocks in my test class...    
        Result result = methodInA(val); 
        fresult.setResult(result);
        return fresult;
    }   
}

BTest.java

@Before
public void setUp(){
   A objectA = new A();
   dbconnection = createMock( DataBaseConnection.class );
   setField( objectA, "dbconnection", dbconnection );
   expect( dbconnection.getResults( EasyMock.anyObject( String.class ) ) ).andReturn( 'Dummy Result object' ).anyTimes();
}

@Test
public void methodName() {
    B classBobject = new B();
    replayAll();
    Fresult fresult  = classBobject.getfinalResult("some value");
    verifyAll();
    assertEquals("Somthing", "Somtthing");
}
Siva
  • 3,188
  • 7
  • 26
  • 34
  • 1
    Nothing in your question tells me otherwise. Find out what is `null` and you'll have your answer. We can't find that for you because you haven't provided an [MCVE]. – Savior Apr 13 '16 at 21:16
  • 1
    I don't think you understand how Stack Overflow works. We're not here to help you. We're here to generate a repository of knowledge. Currently, your question (and its answers) is only helpful to you since you haven't shown any of the details that might cause the NPE. What is `classBobject`? You haven't been specific about which line throws the NPE. Other than the `replayAll` and `verifyAll`, where is EasyMock involved? Please provide more details, an [MCVE]. – Savior Apr 13 '16 at 21:20
  • I'd be willing to reopen this if you can provide several things: where the NPE is occurring (enough code to reproduce or at least infer), and why you believe the variable you're dereferencing is in fact initialized. The latter is important since I don't want to leave you feeling like you didn't get the answer you were looking for. Ping me with @Makoto if you do update this question. – Makoto Apr 13 '16 at 21:22
  • I have clearly mentioned..while calling the method from the super class (in class B.java > methodInA(val)) we are getting the null pointer. And this is the issue in my project, so I made it simple and tried to present easily. Ok Let me update. – Siva Apr 13 '16 at 21:24
  • 1
    Is it the call to `methodInA(val);` that throws it or is it something inside it? You've shown 2 lines of the latter and neither can cause an NPE. The former is impossible (more or less). – Savior Apr 13 '16 at 21:25
  • its not going inside the method methodInA(val);, when the flow reaches its causing the exception. – Siva Apr 13 '16 at 21:26
  • We need more information. From the way your test *might* be set up, it could easily be the case that your mocked object is somehow `null`. – Makoto Apr 13 '16 at 21:27
  • Friend, I have updated the code. I found the issue is at super class only, the dbconnection in the super class is null, can you help me how to mock the object in the super class, hope my code is clear now. I made it very simple as far as I know. – Siva Apr 13 '16 at 22:44
  • 1
    Just do the same thing you did for `objectA` with `classObject`, `setField( classObject, "dbconnection", dbconnection );` assuming `setField` uses some reflection magic to access and set fields of the given instance by name. – Savior Apr 14 '16 at 00:25

0 Answers0