title: java.lang.NullPointerException in Unit Testing spring service class
body: <p>In a simple mvc application in spring boot , I want to do unit testing of a class of service layer called LibraryService (interface) and LibraryServiceImpl.</p>
  
  <pre><code>public interface LibraryService {
  
      /*
       * Returns media library object given the library object id.
       */
      public MediaLibraryDetail getLibraryById(ObjectId libraryId);
  }
  </code></pre>
  
  <p>Below we see it's implementation</p>
  
  <pre><code>@Service
  public class LibraryServiceImpl implements LibraryService {
  
      @Autowired
      private LibraryDAOImpl libDao;
  
  
      @Override
      public MediaLibraryDetail getLibraryById(ObjectId libraryId) {
          return libDao.getLibraryById(libraryId);
      }
  }
  </code></pre>
  
  <p>We can see it has dependency on a class <code>MediaLibraryDetail</code>. Also, <code>ObjectId</code> is another class which is type of its parameter <code>libraryId</code>.</p>
  
  <p>I want to do unit testing in spring boot for method <code>getLibraryById()</code>.</p>
  
  <p>Here's is my code:</p>
  
  <pre><code>@RunWith(MockitoJUnitRunner.class)
  public class LibraryServiceImplTest {
  
      @Mock
      private LibraryDAO libDao = new LibraryDAOImpl();
      @Mock
      private ObjectId libraryId;
      @InjectMocks
      private  LibraryService libraryService =new LibraryServiceImpl();
  
  
      @Test
      public void getLibraryByIdTest(){
  
      MediaLibraryDetail mediaLibraryDetail =new MediaLibraryDetail();
      mediaLibraryDetail.setCollectionName("abc");
  
      when(libDao.getLibraryById(libraryId)).thenReturn(mediaLibraryDetail);
      assertSame(mediaLibraryDetail,libraryService.getLibraryById(libraryId));
  
      }
  }
  </code></pre>
  
  <p>I am getting this error <code>java.lang.NullPointerException</code> on this last line <code>assertSame(mediaLibraryDetail,libraryService.getLibraryById(libraryId));</code></p>
  
  <p>Where am I wrong ?</p>