在我们的项目中,我有模块scout.client、scout.server、scout.shared和后端。
后端不依赖于scout.server和scout.shared,但scout.server对后端具有依赖关系。

在后端项目中,我拥有所有业务逻辑,并调用所有外部服务。
我的问题是当我尝试测试使用后端服务的童子军服务时。
因为scout为模拟bean提供了一些很好的工具,所以我们将后端的服务定义为bean:
BEANS.getBeanManager().registerClass(CarService.class);
BEANS.getBeanManager().registerClass(PartnerService.class);CarService.class和PartnerService.class都在后端。
当我尝试编写一些测试时,我在test中添加@BeanMock
@BeanMock
private IPartnerService partnerService;我得到了模仿,但是每次返回每个函数都是空的,即使我写了
doReturn(PartnerBuilder.standardPartnerListWithOneElement()).when(this.partnerService)
.getPartners(any(Set.class));如果我在测试中调试,在用调试器调用此测试之前,我可以获得:
partnerService.getPartners(...) -> return a list of person 什么是正确的,但是当被测试的类调用该服务时,它返回null。
据我所知,这可能是由于接口@ApplicationScoped上缺少注释。如果没有这一点,就不能保证只创建一个bean,而when语句会对该bean的另一个副本作出反应……?
我无法在界面上添加注释,因为后端没有监视模块的依赖关系。
我怎么处理这种案子?
被测试的课程是:
public class UtilityPartner {
/**
* Method return service bean for getting partners by ids.
*
* @return
*/
private static IPartnerService getPartnerService() {
return BEANS.get(IPartnerService.class);
}
public static String getPartnerName(final Long partnerId) {
if (partnerId == null) {
return "";
}
final List<Partner> partners =
(List<Partner>) getPartnerService().getPartners(Sets.newHashSet(partnerId));
if (partners == null || partners.isEmpty()) {
return "";
}
final Partner partner = partners.get(0);
return LookupUtil.createLookupDescription(partner.getId(), partner.getName());
}
}考试课程是:
@RunWith(ServerTestRunner.class)
@RunWithSubject("anonymous")
@RunWithServerSession(ServerSession.class)
public class TestUtilityPartner {
@BeanMock
private IPartnerService partnerService;
@Before
public void init() {
doReturn(PartnerBuilder.standardPartnerListWithOneElement()).when(this.partnerService).getPartners(any(Set.class));
}
@Test
public void getPartnerName() {
final String name = UtilityPartner.getPartnerName(10L);
Assert.assertEquals("My name", name); // NAME IS ""
}
}发布于 2016-03-22 06:27:38
我认为您应该在Bean管理器中注册模拟实例(参见Scout文档中的bean注册 )。您应该使用一个小订单(-建议进行10 000次测试),以便您的模拟赢得生产性注册。最好的方法是使用TestingUtility类注册/注销模拟。不要忘记调用unregisterBean()方法(在带有@After注释的方法中):
import java.util.Collections;
import org.eclipse.scout.rt.platform.BeanMetaData;
import org.eclipse.scout.rt.platform.IBean;
import org.eclipse.scout.rt.testing.shared.TestingUtility;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
public class TestUtilityPartner {
private IBean<?> beanRegistration;
@Before
public void init() {
partnerService = Mockito.mock(IPartnerService.class);
// Register the mock using the Bean meta information:
BeanMetaData beanData = new BeanMetaData(IPartnerService.class)
.withInitialInstance(partnerService)
.withApplicationScoped(true);
this.beanRegistration = TestingUtility.registerBean(beanData);
// Mockito behavior:
Mockito.doReturn(Collections.singletonList(new Partner(34L, "John Smith")))
.when(partnerService).getPartners(Mockito.any(Set.class));
}
@After
public void after() {
// Unregister the mocked services:
TestingUtility.unregisterBean(this.beanRegistration);
}
@Test
public void getPartnerName() {
String name = UtilityPartner.getPartnerName(10L);
Assert.assertEquals("10 - John Smith", name);
}
}我不知道@BeanMock (org.eclipse.scout.rt.testing.platform.mock.BeanMock)在做什么,但根据朱迪丝·海鸥的回答的说法,这是行不通的:
在这里使用
@BeanMock没有帮助,因为您没有使用应用程序作用域服务: 在init方法中,您要更改本地字段partnerService。但是,在您的测试中,您将调用UtilityPartner.getPartnerService,它正在创建一个新实例(使用BEANS.get(IPartnerService.class))。 为了方便地模拟应用程序作用域bean,@BeanMock更有用。
发布于 2016-04-05 14:45:43
在这里使用@BeanMock没有帮助,因为您没有使用应用程序作用域服务:
在init方法中,您要更改本地字段partnerService。但是,在您的测试中,您将调用UtilityPartner.getPartnerService,它正在创建一个新实例(使用BEANS.get(IPartnerService.class))。
为了方便地模拟应用程序作用域bean,@BeanMock更有用。
您始终可以手动注册bean 如Jmini所示。测试结束后,请不要忘记重新注册bean!
我们建议使用org.eclipse.scout.rt.testing.shared.TestingUtility.registerBean(BeanMetaData),它自动添加测试顺序并删除@TunnelToServer注释。
https://stackoverflow.com/questions/36132254
复制相似问题