繁体   English   中英

具有自动装配注释的AbstractClass Junit

[英]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。

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

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

采用

@InjectMocks
private AbstractAdminSearchService mock;

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM