我正在使用EasyMock,经过调试后,我发现问题出在A.java>从dbconnection对象调用getResults(val)时。
dbconnection对象为null,我的问题是如何在超类中模拟对象?
以下是简单的代码:
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");
}