无法加载Maven,JUnit和Dagger - Dagger模块

时间:2014-12-04 18:41:42

标签: maven junit dagger

注意:我只通过命令行构建/测试。

我遇到了使用Dagger的问题,并且能够在一个非常小的测试项目中重现相同的问题。在单元测试中尝试使用Dagger时,运行'mvn clean test'时出现以下错误:

sanity(com.mycompany.app.AppTest): Module adapter for class com.mycompany.app.AppTest$TestModule could not be loaded. Please ensure that code generation was run for this module.

在应用程序中,Dagger正在编译/构建就好了,注入效果很好。唯一的问题是单元测试,我觉得单元测试没有从dagger编译器中获取结果,但我不确定如何测试/修复它。

的pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>my-app</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

    <build>
      <pluginManagement>
          <plugins>
              <plugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-compiler-plugin</artifactId>
                  <configuration>
                      <source>1.7</source>
                      <target>1.7</target>
                      <compilerArgument>-proc:none</compilerArgument>
                  </configuration>
              </plugin>
          </plugins>
      </pluginManagement>
  </build>

  <dependencies>

    <dependency>
        <groupId>com.squareup.dagger</groupId>
        <artifactId>dagger</artifactId>
        <version>1.2.2</version>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>com.squareup.dagger</groupId>
        <artifactId>dagger-compiler</artifactId>
        <version>1.2.2</version>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.8.2</version>
        <scope>test</scope>
    </dependency>

  </dependencies>
</project>

AppTest.java

package com.audible.hushpuppy.dagger;

import org.junit.Before;
import org.junit.Test;

import dagger.Module;
import dagger.ObjectGraph;

import static org.junit.Assert.assertNull;

public class AppTest {

    @Module(injects = AppTest.class)
    public class TestModule{
    }

    @Before
    public void setUp() throws Exception {
        ObjectGraph.create(new TestModule());
    }

    @Test
    public void sanity() throws Exception {
        assertNull(null);
    }
}

1 个答案:

答案 0 :(得分:1)

啊哈 - 显然注释处理在之前的提交中完全关闭了,我完全错过了pom中的这一行:

<compilerArgument>-proc:none</compilerArgument>