0

Hi i am trying to mock dao layer my application has some class hire achy like this Application->parser->dao(interface)->dao implementation class

my problem is when i am mocking dao interface or daoimp class using mockito in my test case they are not working simply test case going to db how to do make our test case to use these mocked objects

    @RunWith(MockitoJUnitRunner.class)  
    public class CsvDataLoadServiceImplTest {

    @Mock private MeteringDataDao meteringDataDao;

    List<Object> persistedList;
    Object meteringData;
    List<Object> s=new ArrayList<Object>();

    @SuppressWarnings({ "rawtypes", "unchecked" })
    @Before public void setup(){
        Mockito.doAnswer(new Answer<List<Object>>() {
            @Override
            public List<Object> answer(InvocationOnMock invocation) throws Throwable {
                Object[] args = invocation.getArguments();
                System.out.println("persist all");
                if(persistedList == null){

                    persistedList = (List)args[0];

                }
                else
                    persistedList.addAll((List)args[0]);
                return null;
            }}).when(meteringDataDao).persistAll(anyList());

        Mockito.doAnswer(new Answer<Object>() {
            @Override
            public Object answer(InvocationOnMock invocation) {
                Object[] args = invocation.getArguments();
                if(meteringData == null)

                    meteringData = (List)args[0];

                return true;
            }}).when(meteringDataDao).persist(anyObject());
    }

    @After public void tearDown(){
        persistedList = null;
        meteringData = null;

    }

    @Test
    public void testDataAccuricy(){

        CsvDataLoadService csvDataLoadService =new CsvDataLoadServiceImpl();
        csvDataLoadService.loadRackspaceCsvData();
    }
}
ArK
  • 20,698
  • 67
  • 109
  • 136
  • If you can't change the system under test, you may try using [one of these techniques](http://stackoverflow.com/a/27552765/1426891) to get your mock into your class. – Jeff Bowman Jul 08 '15 at 16:41

2 Answers2

1
CsvDataLoadService csvDataLoadService = new CsvDataLoadServiceImpl();

You're constructing an instance of the service, but this instance doesn't use the DAO you mocked. It uses another one. You need something like

CsvDataLoadService csvDataLoadService = 
    new CsvDataLoadServiceImpl(meteringDataDao);
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
0

Your question is a little badly phrased, so sorry if I appear to have misunderstood you.

@JBNizet answer is correct, you're not using the mocked object, but rather than changing the code for a test you should simply change the test. This is always a best practice when writing your tests.

You haven't assigned the mocked object to the service, so I'm assuming that the MeteringDataDao object is actually instantiated inside the class you're attempting to test?

If so, then you will need something more powerful than Mockito (Unless you want to reproduce the capabilities of a more powerful already existing library). I would suggest PowerMockito, which I have used in the past for something exactly like this.

A good example of using powermockito to mock the constructor of a class can be seen in a few answers on SO already, so I'll just link to them rather than try and re-explain their already clear answers:

Community
  • 1
  • 1
Seb
  • 959
  • 16
  • 29
  • "rather than changing the code for a test you should simply change the test": Though you should avoid putting _testing code_ in a _production class_, that's very different than improving the flexibility of your class by accepting your dependencies. I wouldn't say it's "always a best practice" to exclude refactoring for testing, particularly at the expense of dependency injection. – Jeff Bowman Jul 08 '15 at 16:38
  • Yea, that's true. I was thinking more of just scenarios where you are trying to write a test but can't get it to work properly so you start changing the code to suit your test better, at the sake of the reasons why you wrote it in its original state. I should have been more clear. – Seb Jul 09 '15 at 08:47