Mockito vc JMockit和依赖注入
问题描述:
我发现这篇文章很有趣http://www.jayway.com/2012/02/25/mockito-and-dependency-injection/它说Mockito支持依赖注入通过使用构造函数参数,setter方法和字段注入。我想知道JMockit是否也一样,到目前为止我还没有发现没有人使用JMockit和依赖注入。Mockito vc JMockit和依赖注入
答
JMockit支持通过参数和属性进行依赖注入。测试类必须包含一个或多个声明为@Injectable的模拟属性或模拟参数。您想要测试的业务对象需要使用注解@Tested进行声明。 @Tested注解自动创建类的实例并注入模拟的依赖关系。
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(官方文档)