I am new to mock testing and was wondering how I would go about mocking the following with mockito.
uDAO = (UserDAO) dao;
where dao is defined as
protected DAO<T, Long> dao;
Thanks!
I am new to mock testing and was wondering how I would go about mocking the following with mockito.
uDAO = (UserDAO) dao;
where dao is defined as
protected DAO<T, Long> dao;
Thanks!
To inject mocks for private and protected fields (which do not have a public setter method) you have to use reflections. Using reflections you can set a mocked object in place of that protected field. Below is a sample of relections code.
Field hack = <PUT YOU CLASS NAME>.class.getDeclaredField("dao");
hack.setAccessible(true);
hack.set(<Object of the class where you are injecting the mock>,<the mock object>);
To create the mocked object you can use the regular PowerMockito.mock
or Mockito.mock
functions.
For more info on reflections have a look at the below links:
Is it possible in Java to access private fields via reflection http://www.java2s.com/Code/Java/Reflection/Setprivatefieldvalue.htm