作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我想知道在使用 CDI 时如何指定在测试期间应该使用哪个模拟实现。
我知道我可以用@Alternative 标记一个模拟实现,但是我需要在 beans.xml 中定义它。我想要多个模拟(针对多个对象)。假设我有一个 OrderService 和一个 EmailService。我正在编写一些不希望 EmailService 发送电子邮件的验收测试。然后是另一组测试,它们是系统测试 - 我确实想发送电子邮件,但不应创建真正的订单。
理想的解决方案是为每个测试方法调用指定备选方案,如下所示:
@BeforeMethod
public void before(){
// in this test suite we're using mock email service
addAlternative(MockEmailService.class);
}
@Test
public void testMyStuff(){
issueAnOrder(); // and verify that it worked. no emails sent!
}
这可能吗?
最佳答案
CDI-Unit 内置了对模拟的支持 http://jglue.org/cdi-unit/只需在您的测试类中将模拟指定为生产者字段即可。
class Starship {
@Inject
Engine engine; //This will be mocked
void start() {
engine.start();
}
}
@RunWith(CdiRunner.class)
class TestStarship {
@Inject
Starship starship;
@Produces
@Mock // Mockito will create a mock for us.
Engine engine;
@Test
public void testStart() {
starship.start();
// Verify that the mocks start method is called at least once.
Mockito.verify(engine, Mockito.atLeastOnce()).start();
}
}
关于java - CDI @Alternative - 根据测试用例选择备选方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13615018/
注意:使用 jQuery 1.3.2(是的,我正在升级,但为此仍然需要 1.3.2) 寻找更好的方法来实现这一点 HTML 元素(这来自自定义 PHP 框架,更改起来非常麻烦): ... 1
我有一项服务可以提供大多数时间对用户可见的 UI。 当我遇到问题时,我正在尝试新的应用程序架构。 MyModelviewModel viewModel = ViewModelProviders.of(
在 C# 中,我有以下代码: string[] exampleStrings = Console.ReadLine().Split(' '); // C# int[] example
我是一名优秀的程序员,十分优秀!