我发现这篇文章很有趣http://www.jayway.com/2012/02/25/mockito-and-dependency-injection/它说 Mockito 通过使用构造函数参数、setter 方法和字段注入(inject)来支持依赖注入(inject)。我想知道 JMockit 是否也这样做,到目前为止我还没有发现有人使用 JMockit 和依赖注入(inject)。
最佳答案
JMockit 支持通过参数和属性进行依赖注入(inject)。测试类必须包含一个或多个声明为 @Injectable 的模拟属性或模拟参数。您想要测试的业务对象需要使用注释@Tested 进行声明。 @Tested 注释会自动创建类的实例并注入(inject)模拟的依赖项。
public class SomeTest {
@Tested CodeUnderTest tested;
@Injectable Dependency dep1;
@Injectable AnotherDependency dep2;
@Injectable int someIntegralProperty = 123;
@Test
public void someTestMethod(@Injectable("true") boolean flag, @Injectable("Mary") String name)
{
// Record expectations on mocked types, if needed.
tested.exerciseCodeUnderTest();
// Verify expectations on mocked types, if required.
}
}
您可以在这里找到更多详细信息: http://jmockit.github.io/tutorial/BehaviorBasedTesting.html#tested (官方文档)
关于java - Mockito 与 JMockit 以及依赖注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26755599/