2

I have application written in spring boot. I am using testNG and mockito for unit testing.

I am bit confuse with working of unit tests.

Following is my test class

class StudentServiceTest {
    @mock
    StudentDAO studentDAO;

    @InjectMocks
    StudentService studentService;

    @BeforeMethod
    public void initMock() {
        studentService = new StudentService();
        MockitoAnnotations.initMocks(this);
    }

    @Test(dataprovider.....)
    public void shouldxxxxx(int id......) {
       when(studentDAO.findOne(id)).thenReturn(Student);
       assert......
    }
}

When I run above test. It works fine.

I have following doubts.

  1. Does it involves spring. If not then how @mock and other code works
  2. Should we involve spring.If not/yes why?
  3. I have used new key word to initialize service. Is it good. As in spring unit test documentation they have said that

You can simply instantiate objects using the new operator without even involving Spring. You can also use mock objects instead of real dependencies

If I not instantiate service with new keyword then it show error "Cannot instantiate @InjectMocks".

If I autowired service then it requires spring container and it I run even single test, it takes too much time to run. And If not autowired and use new keyword then it runs very fast.

  1. Does above code is clean? Please do suggest best practice to write unit test in spring boot with testNg and mockito.
silly questions
  • 357
  • 6
  • 14

1 Answers1

1
  1. Does it involves spring. If not then how @mock and other code works

    No I guess since you are mocking every thing.

  2. Should we involve spring?

    No, unless you wanted to use spring managed beans.

  3. I have used new key word to initialize service.

    Even it't not required to instantiate service using new keyword. Make sure your initMock() method annotated with org.junit.Before annotation and init mocks with MockitoAnnotations.initMocks(this);

    If you taken care this you should not see Cannot instantiate @InjectMocks error

  4. Does above code is clean?

    of course, if you taken care 3 bullet point it will be clean code.

Your test class should be as fallows.

class StudentServiceTest {
   @mock
   StudentDAO studentDAO;

   @InjectMocks
   StudentService studentService;

   @org.junit.Before
   public void initMock() { 
      MockitoAnnotations.initMocks(this);
   }

   @Test(dataprovider.....)
   public void shouldxxxxx(int id......) {
      when(studentDAO.findOne(id)).thenReturn(Student);
      assert......
   }
}
Lovababu Padala
  • 2,415
  • 2
  • 20
  • 28
  • Thank you. One doubt, If I am using testNg then why @Before from junit? I tried with that but studentDAO and studentService is null hence NPE – silly questions Nov 04 '15 at 13:41
  • I am not too familiar with TestNG, are you sure that initMock() method is executing before actual test when you use @BeforMethod annotation? – Lovababu Padala Nov 04 '15 at 14:02
  • Yup. It always execute before each methode hence no need to explicit cleanup. Buy the way that is clear for me. But will you please explain how this @mock and other code works without spring container as i am using spring? – silly questions Nov 04 '15 at 14:06
  • Basically you are mocking the real objects, mockito framework create the stub for the fields marked as @ Mock, and @ InitMock inject these stubs as dependencies. you can't print mock object reference, since it is not actually instantiated. this link may help you to understand mock http://stackoverflow.com/questions/2665812/what-is-mocking – Lovababu Padala Nov 04 '15 at 14:28