2016-07-08 117 views
2

我使用maven構建我的項目和JUnit來測試。
當我試圖使用SystemOutRule做測試時,我無法找到這個類,也找不到org.junit.contrib包。找不到org.junit.contrib包

p.s.我的JUnit是最新版本4.12。

我的Maven的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/maven-v4_0_0.xsd"  

<modelVersion>4.0.0</modelVersion> 
<groupId>com.Quentin.test</groupId> 
<artifactId>test</artifactId> 
<packaging>war</packaging> 
<version>1.0-SNAPSHOT</version> 
<name>test Maven Webapp</name> 
<url>http://maven.apache.org</url> 

<parent> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-parent</artifactId> 
    <version>1.3.6.RELEASE</version> 
</parent> 
<dependencies> 
    <dependency> 
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId> 
     <version>4.12</version> 
     <scope>test</scope> 
    </dependency> 

    <dependency> 
     <groupId>com.jayway.jsonpath</groupId> 
     <artifactId>json-path</artifactId> 
     <scope>test</scope> 
    </dependency> 

    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-context</artifactId> 
     <version>4.3.1.RELEASE</version> 
     <scope>runtime</scope> 
    </dependency> 

    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-web</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId> 
     <scope>test</scope> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-beans</artifactId> 
     <version>4.3.1.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-beans</artifactId> 
     <version>4.3.1.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-beans</artifactId> 
     <version>4.3.1.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-beans</artifactId> 
     <version>4.3.1.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-beans</artifactId> 
     <version>4.3.1.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-beans</artifactId> 
     <version>4.3.1.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId> 
    </dependency> 
</dependencies> 

<build> 
    <finalName>test</finalName> 
    <plugins> 
     <plugin> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-maven-plugin</artifactId> 
     </plugin> 
    </plugins> 
</build> 

<repositories> 
    <repository> 
     <id>spring-snapshots</id> 
     <url>http://repo.spring.io/snapshot</url> 
     <snapshots><enabled>true</enabled></snapshots> 
    </repository> 
    <repository> 
     <id>spring-milestones</id> 
     <url>http://repo.spring.io/milestone</url> 
     <snapshots><enabled>true</enabled></snapshots> 
    </repository> 
</repositories> 

<pluginRepositories> 
    <pluginRepository> 
     <id>spring-snapshots</id> 
     <url>http://repo.spring.io/snapshot</url> 
    </pluginRepository> 
    <pluginRepository> 
     <id>spring-milestones</id> 
     <url>http://repo.spring.io/milestone</url> 
    </pluginRepository> 
</pluginRepositories> 

我的測試類

import org.junit.Rule; 
import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.test.context.ContextConfiguration; 
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 

import static org.junit.Assert.assertNotNull; 

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(classes = CDPlayerConfig.class) 
public class CDPlayerTest { 

    @Rule 
    public final SystemOutRule log = new SystemOutRule.enableLog(); 

    @Autowired 
    private MediaPlayer player; 

    @Autowired 
    private CompactDisc disc; 

    @Test 
    public void checkNotNull() { 
     assertNotNull(player); 
     assertNotNull(disc); 
    } 

    @Test 
    public void play() { 
     player.play(); 
    } 
} 

在CDPlayerTest.class,我試圖用SystemOutRule,但我的IDE(IntelliJ IDEA的)讓我想起了這個類不存在,我在「Maven Artifact Search」中找不到它。

My project Structure

+0

?刪除最後一個。 –

+0

@ M.Deinum在創建項目時第一個由IntelliJ IDEA自動添加;但是當我創建一個JUnit測試時,IntelliJ IDEA似乎禁止範圍設置原因,編譯器迫使我添加一個新的JUnit依賴項。所以基本上,如果我刪除了第二個依賴項,編譯器不再識別Junit導入,並要求我再次添加第二個依賴項!儘管我試圖刪除第一個依賴項,但它並沒有幫助。 –

回答

4

SystemOutRuleSystem Rules項目由斯特凡·伯克納的一部分。它不是JUnit的官方組成部分,但開源,免費提供,似乎保持良好。

它發佈到Maven的中央,你可以得到通過添加以下依賴你pom.xml文件的最新版本:爲什麼你有2個單元相依一個測試,一個編譯

<dependency> 
    <groupId>com.github.stefanbirkner</groupId> 
    <artifactId>system-rules</artifactId> 
    <version>1.16.0</version> 
    <scope>test</scope> 
</dependency> 
+1

謝謝soooooo多!!! –