2

I use maven to build my project and JUnit to test.
When I tried to do test using SystemOutRule, I could not find this class and could not find org.junit.contrib package.

p.s. My JUnit is the latest version, 4.12.

my 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>

my Test class

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();
  }
}

In CDPlayerTest.class, I tried to use SystemOutRule, but my IDE (IntelliJ IDEA) reminds me that this class does not exist, and I could not find it in the "Maven Artifact Search".

My project Structure

Mureinik
  • 297,002
  • 52
  • 306
  • 350
Quentin Liu
  • 141
  • 1
  • 10
  • Why do you have 2 unit dependencies one test and one compile? Remove the last one. – M. Deinum Jul 08 '16 at 09:55
  • @M.Deinum the first one was automatically added by IntelliJ IDEA when I created the project; but IntelliJ IDEA seems to forbid the scope setting cause when I created a JUnit test, the compiler forces me to add a new JUnit dependency. So basically if I deleted the second dependency, the compiler no longer recognises Junit imports and requires me to add the second dependency again! Even though I tried to delete the first dependency, it didn't help. – Quentin Liu Jul 08 '16 at 11:17

1 Answers1

5

SystemOutRule is part of the System Rules project by Stefan Birkner. It is not an official part of JUnit, but is open sourced, freely available and seems to be well maintained.

It's published to Maven Central, and you can get the latest version by adding the following dependency to your pom.xml file:

<dependency>
  <groupId>com.github.stefanbirkner</groupId>
  <artifactId>system-rules</artifactId>
  <version>1.16.0</version>
  <scope>test</scope>
</dependency>
Mureinik
  • 297,002
  • 52
  • 306
  • 350