LoginSignup
2
1

More than 5 years have passed since last update.

com.google.testing.compile を使ったテスト

Posted at

アノテーションプロセッサーを使用したクラスのテストをjunitを利用して行いたいと思い最初はAptina Unitを使っていましたが、大人の事情により、compile-testingを使用しないといけなくなりましたので、使い方を忘れないようにここに書いております。

compile-testingのgithub
https://github.com/google/compile-testing

(´-`).。oO(英語を見てひるむ。)

他に参考にしたページはこちら
http://blog.64p.org/entry/2015/03/26/055347

http://hisaichi5518.hatenablog.jp/entry/2017/09/21/213423

http://blog.duck8823.com/entry/2016/05/07/095947

(´-`).。oO(安心の日本語)

使い方

pom.xmlには、こう書きます。

<dependency>
  <groupId>com.google.testing.compile</groupId>
  <artifactId>compile-testing</artifactId>
  <version>0.12</version>
  <scope>test</scope>
</dependency>

(´-`).。oO(ここでやめたくなる。)

テストコード

プロセッサーが出力したソースを確認するために書いたテストコードのサンプルです。



import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.*;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import javax.tools.JavaFileObject;

import org.junit.Test;

import com.google.common.io.Resources;
import com.google.testing.compile.Compilation;
import com.google.testing.compile.JavaFileObjects;

import static com.google.testing.compile.Compiler.javac;
import static com.google.common.truth.Truth.assert_;
import static com.google.testing.compile.JavaSourceSubjectFactory.javaSource;

public class MyProcessorTest {
    @Test
    public void test_プロセッサーの出力確認(){
        MyProcessor processor = new MyProcessor();

        // assert
        assert_().about(javaSource())
            .that(JavaFileObjects.forResource(Resources.getResource("HelloWorld.java")))
            .processedWith(processor)
            .compilesWithoutError()
            .and()
            .generatesSources(JavaFileObjects.forSourceString("foo.bar.baz.Blah", "package foo.bar.baz;\n"
                    + "\n"
                    + "import java.lang.String;\n"
                    + "import javax.annotation.Generated;\n"
                    + "\n"
                    + "@Generated({\"me.geso.sample.hello.MyProcessor\"})\n"
                    + "public class Blah {\n"
                    + "  public String hello() {\n"
                    + "    return \"hello\";\n"
                    + "  }\n"
                    + "}"));


    }
}

generatesSources()でfoo.bar.baz.Blahが次のパラーメーターと一致していることを確認できる。

エラーを確認したい時のテスト

    @Test
    public void test_process_エラー() throws Exception {
        File stub = new File(System.getProperty("user.dir") + "/src/test/java/jp/co/processor/MyProErrStub.java");

        assert_().about(JavaSourceSubjectFactory.javaSource())
            .that(JavaFileObjects.forResource(stub.toURI().toURL()))
            .processedWith(new MyProcessor())
            .failsToCompile();
    }
  • failsToCompile()で、エラーとなることの確認ができる。
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1