2017-05-28 79 views
1

我已經在IntelliJ中創建了新的彈簧引導項目,並且我希望使用來自簡單單元測試的彈簧引導上下文分開測試,所以我添加了maven failsafe插件。我的配置是這樣的:IllegalStateException在通過maven failsafe插件運行spring引導測試時。

 <!--RUNNING UNIT TESTS--> 
     <plugin> 
      <artifactId>maven-surefire-plugin</artifactId> 
      <version>2.20</version> 
      <configuration> 
       <excludes> 
        <exclude>**/*IT.java</exclude> 
       </excludes> 
      </configuration> 
     </plugin> 

     <!--RUNNING INTEGRATION TESTS--> 
     <plugin> 
      <artifactId>maven-failsafe-plugin</artifactId> 
      <version>2.20</version> 
      <configuration> 
       <includes> 
        <include>**/*IT.java</include> 
       </includes> 
      </configuration> 
      <executions> 
       <execution> 
        <goals> 
         <goal>integration-test</goal> 
         <goal>verify</goal> 
        </goals> 
       </execution> 
      </executions> 
     </plugin> 

    </plugins> 
</build> 

我在的IntelliJ測試類改名全自動生成的匹配模式和測試看起來是這樣的:

@RunWith(SpringRunner.class) 
@SpringBootTest 
public class ErpegApplicationTestIT { 

    @Test 
    public void contextLoads() { 
    } 

} 

的問題是,當我在InttelliJ運行這個測試,一切正常。但我跑得mvn verify後我有:

[ERROR] initializationError(com.tbawor.ErpegApplicationTestIT) Time elapsed: 0.005 s <<< ERROR! 
java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @S 
pringBootTest(classes=...) with your test 

是否與類命名的問題嗎?我應該採取不同的方法來區分這些測試嗎?

無論如何感謝您的幫助。

+0

首先你的配置[Maven的萬無一失,插件(http://maven.apache.org/surefire/maven-surefire-plugin/test-mojo。 html#includes)和[maven-failsafe-plugin](http://maven.apache.org/surefire/maven-failsafe-plugin/integration-test-mojo.html#includes)是不需要的,因爲這些是默認值(約定超過配置)。你是否按照錯誤信息中的建議? – khmarbaise

回答

0

如果有人會遇到這個問題,我找到了一個解決方案。你只需要簡單修改的​​pom.xml:

  <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-failsafe-plugin</artifactId> 
      <version>2.20</version> 
      <configuration> 
       <additionalClasspathElements> 
        <additionalClasspathElement>${basedir}/target/classes</additionalClasspathElement> 
       </additionalClasspathElements> 
      </configuration> 
      <executions> 
       <execution> 
        <goals> 
         <goal>integration-test</goal> 
         <goal>verify</goal> 
        </goals> 
       </execution> 
      </executions> 
     </plugin> 
相關問題