Reputation: 1433
I have a private method called getSubject
in a class which i have just implemented. I am trying to carry out a unit test on the private method but my issue is that the private method getSubject
is calling another method getSubjectOracle()
(note:getSubjectOracle
is a public method in a jar file) which returns a String
subject. A pseoudocode is shown below:
public class Service{
private oracleDao
//setter for oracle dao avilable
private String getSubject(String id,Stringountry){
String subject = oracleDao.getSubjectOracle(String id,String country)
return subject;
}
}
Any idea how i can mock the return of the method oracleDao.getSubjectOracle(String id,String country)
in order to carry unit test for method getSubject(String id, String country)
pls?
I have search online of helpful resouces but could not get any.
Thanks in advance.
Upvotes: 0
Views: 1845
Reputation: 280112
If you are trying to test Service
, then you have to mock oracleDao
as well and make its getSubjectOracle()
method return the String you want.
I'm assuming you aren't testing getSubject()
but a method that calls getSubject()
.
Upvotes: 1
Reputation: 1840
One way could be to write a setter for oracleDao. There you can set a mock instead of the real thing. For example write your own oracleDao that does what you want. In the @Before method you would inject the mock oracleDao.
All this is nicer with a framework like Mockito. It would look like this:
@Mock
YourDaoThing mock;
@Before
public setUp(){
MockitoAnnotation.initMocks(this);
service = new Service();
service.setDao(mock);
}
@Test
public testGetSubject(){
String someString = "whatever";
when(mock.getSubjectOracle(id,country)).thenReturn(someString)
assertEquals(expect, service.callToTheMethodYouTest())
}
Upvotes: 1