AbstractClass Junit with Autowired annotation



我有一个抽象类,里面使用@Autowired注释。 我正在尝试使用MockitoJUnitRunner编写junit。

@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不起作用,"upsService"没有被嘲笑。 结果,当实际调用upsService.getUsersProfile时,我得到了NullpointerException。

基本上我们不会为抽象类编写 Junits,因为我们无法为它们创建对象,如果是普通的具体类而不是 以下代码

mock=Mockito.mock(AbstractAdminSearchService.class,
Mockito.CALLS_REAL_METHODS);

@InjectMocks
private AbstractAdminSearchService mock;

然后所有模拟都将插入到真实对象中

最新更新