如何编写Java注释处理器自动单元测试? [英] How to write automated unit tests for java annotation processor?

查看:186
本文介绍了如何编写Java注释处理器自动单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用java注释处理器试验。我能写使用JavaCompiler的集成测试(其实我使用山核桃的时刻)。我可以运行编译过程,并分析输出。问题:大约半秒,即使没有在我的注解处理器的任何code单一的测试运行。这是太长时间在TDD风格使用它。

I'm experimenting with java annotation processors. I'm able to write integration tests using the "JavaCompiler" (in fact I'm using "hickory" at the moment). I can run the compile process and analyse the output. The Problem: a single test runs for about half a second even without any code in my annotation processor. This is way too long to using it in TDD style.

嘲讽走的依赖性看起来很辛苦,我(我会模拟出了整个javax.lang.model.element中的包)。有人成功编写注释处理器(Java 6中)的单元测试?如果不是......这将是你的做法?

Mocking away the dependencies seems very hard for me (I would have to mock out the entire "javax.lang.model.element" package). Have someone succeed to write unit tests for an annotation processor (Java 6)? If not ... what would be your approach?

推荐答案

您说得对嘲讽注释处理API(像EasyMock的一个模拟库)是痛苦的。我试过这种方法,并迅速打破了pretty。你不得不安装许多方法调用的预期。测试变得难以维护。

You're right mocking the annotation processing API (with a mock library like easymock) is painful. I tried this approach and it broke down pretty rapidly. You have to setup to many method call expectations. The tests become unmaintainable.

A 基于状态的测试方法为我工作得相当好。我不得不实施的<一个零件href=\"https://bitbucket.org/blob79/quickcheck/src/c8e12cc026fd6295966bf9ff5d20919961ff4149/quickcheck-src-generator/src/test/java/net/java/quickcheck/srcgenerator/Model.java?at=default\"相对=nofollow> javax.lang.model。* API,我需要为我的测试。 (这只是&LT; 350线code的)

A state-based test approach worked for me reasonably well. I had to implement the parts of the javax.lang.model.* API I needed for my tests. (That were only < 350 lines of code.)

这是一个测试启动javax.lang.model对象的一部分。安装后的模式应该是在相同的状态作为Java编译执行

This is the part of a test to initiate the javax.lang.model objects. After the setup the model should be in the same state as the Java compiler implementation.

DeclaredType typeArgument = declaredType(classElement("returnTypeName"));
DeclaredType validReturnType = declaredType(interfaceElement(GENERATOR_TYPE_NAME), typeArgument);
TypeParameterElement typeParameter = typeParameterElement();
ExecutableElement methodExecutableElement = Model.methodExecutableElement(name, validReturnType, typeParameter);

静态工厂方法在类模式实施javax.lang.model。*类定义。例如的declaredType 。 (所有不支持的操作会抛出异常。)

The static factory methods are defined in the class Model implementing the javax.lang.model.* classes. For example declaredType. (All unsupported operations will throw exceptions.)

public static DeclaredType declaredType(final Element element, final TypeMirror... argumentTypes) {
    return new DeclaredType(){
        @Override public Element asElement() {
            return element;
        }
        @Override public List<? extends TypeMirror> getTypeArguments() {
            return Arrays.asList(argumentTypes);
        }
        @Override public String toString() {
            return format("DeclareTypeModel[element=%s, argumentTypes=%s]",
                    element, Arrays.toString(argumentTypes));
        }
        @Override public <R, P> R accept(TypeVisitor<R, P> v, P p) {
            return v.visitDeclared(this, p);
        }
        @Override public boolean equals(Object obj) { throw new UnsupportedOperationException(); }
        @Override public int hashCode() { throw new UnsupportedOperationException(); }

        @Override public TypeKind getKind() { throw new UnsupportedOperationException(); }
        @Override public TypeMirror getEnclosingType() { throw new UnsupportedOperationException(); }
    };
}

测试的其余部分验证类的行为测试。

The rest of the test verifies the behavior of the class under test.

Method actual = new Method(environment(), methodExecutableElement);
Method expected = new Method(..);
assertEquals(expected, actual);

您可以看看在<一个href=\"https://bitbucket.org/blob79/quickcheck/src/c8e12cc026fd6295966bf9ff5d20919961ff4149/quickcheck-src-generator/src/test/java/net/java/quickcheck/srcgenerator?at=default\"相对=nofollow>来源$ C ​​$的快速检查@Samples和@Iterables源$ C ​​$ C发电机测试Ç的。 (该code不是最佳的,但该方法类有许多参数和参数类并不将其自己的测试,但作为方法测试的一部分进行测试。应该说明的方法仍然。)

You can have a look at the source code of the Quickcheck @Samples and @Iterables source code generator tests. (The code is not optimal, yet. The Method class has to many parameters and the Parameter class is not tested in its own test but as part of the Method test. It should illustrate the approach nevertheless.)

维耶尔Glück的!

这篇关于如何编写Java注释处理器自动单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆