I have an abstract class which uses @Autowired annotation inside it. I am trying to write the junit using MockitoJUnitRunner.
@RunWith(MockitoJUnitRunner.class)
public class AbstractAdminSearchServiceTest {
@Mock
private IUPSService upsService;
Map<String,String> map;
@Before
public void setUp() {
map=new HashMap<>();
}
@Test
public void testSearchAdministratorsForIndividualNotification(){
AbstractAdminSearchService
mock=Mockito.mock(AbstractAdminSearchService.class,
Mockito.CALLS_REAL_METHODS);
when(upsService.getUsersProfile(buildUserIds(),new String[]
{})).thenReturn(map);
mock.searchAdministratorsForIndividualNotification(buildSolrUsers(),
"");
}
@Mock is not working and 'upsService' is not getting mocked. As a result when actually upsService.getUsersProfile is called,i am getting NullpointerException.
Basically we will not write Junits for abstract classes, Because we can't create object for them,If it is a normal concrete class instead of below code
mock=Mockito.mock(AbstractAdminSearchService.class,
Mockito.CALLS_REAL_METHODS);
use
@InjectMocks
private AbstractAdminSearchService mock;
and then all mocks will be inserted into real object
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.