我正在使用Spring 4.3.8.RELEASE和JUnit 4.12以及Mockito 1.10.18。我有一个发布活动的服务......
@Service("organizationService")
@Transactional
public class OrganizationServiceImpl implements OrganizationService, ApplicationEventPublisherAware
publisher.publishEvent(new ZincOrganizationEvent(id));
@Override
public void setApplicationEventPublisher(ApplicationEventPublisher publisher)
{
this.publisher = publisher;
}
...
@Override
public void save(Organization organization)
{
...
publisher.publishEvent(new ThirdPartyEvent(organization.getId()));
我的问题是,如何在JUnit测试中验证事件是否已实际发布?
@Test
public void testUpdate()
{
m_orgSvc.save(org);
// Want to verify event publishing here
答案 0 :(得分:0)
如果您想测试是否忘记在publishEvent
内拨打OrganizationServiceImpl
方法,可以使用以下内容:
class OrganizationServiceImplTest {
private OrganizationServiceImpl organizationService;
private ApplicationEventPublisher eventPublisher;
@Before
public void setUp() {
eventPublisher = mock(ApplicationEventPublisher.class);
organizationService = new OrganizationServiceImpl();
organizationService.setApplicationEventPublisher(eventPublisher)
}
@Test
public void testSave() {
/* ... */
organizationService.save(organization);
verify(eventPublisher).publishEvent(any(ThirdPartyEvent.class));
}
}
上面的测试用例将验证是否存在publishEvent
方法的调用。
关于:
我的问题是,如何在JUnit测试中验证事件是否已实际发布?
如果要验证实际发送,则必须测试ApplicationEventPublisher
实施,并且可能没有模拟。
答案 1 :(得分:0)
我更喜欢相反的方法,那就是 integration test-ey :
ApplicationListener
ConfigurableApplicationContext
使用这种方法,您正在测试某人已经收到某个事件,这已经发布了一个事件。
这是基本身份验证测试的代码。在其他情况下,我测试是否发生了登录事件
@Test
public void testX509Authentication() throws Exception
{
ApplicationListener<UserLoginEvent> loginListener = mock(ApplicationListener.class);
configurableApplicationContext.addApplicationListener(loginListener);
getMockMvc().perform(get("/").with(x509(getDemoCrt())))//
.andExpect(status().is3xxRedirection())//
.andExpect(redirectedUrlPattern("/secure/**"));
getErrorCollector().checkSucceeds(() -> {
verify(loginListener, atLeastOnce()).onApplicationEvent(any(UserLoginEvent.class));
return null;
});
}
我的建议是释放Mockito的功能以深入验证事件参数。就我而言,我将代码扩展到: