2.15 自动注入被测试类

作者: 孙兴斌 | 来源:发表于2016-12-27 17:41 被阅读26次

@Tested标注被测试类,在运行测试方法时,如果该实例仍然为null,JMockit会自动组装相关mock对象,进行初始化。在组装被测试类过程中,相关mock对象必须使用@Injectable标记,非mock对象除了使用@Injectable标记,还需要有明确初始值。

public class SomeTest {
    @Tested CodeUnderTest tested;
    @Injectable Dependency dep1;
    @Injectable AnotherDependency dep2;
    @Injectable int someIntegralProperty = 123;

    @Test
    public void someTestMethod(@Injectable("true") boolean flag) {
        tested.exerciseCodeUnderTest();
    }
}

注入先根据类型匹配,再根据参数名称匹配

相关文章

网友评论

    本文标题:2.15 自动注入被测试类

    本文链接:https://www.haomeiwen.com/subject/bydevttx.html