2017-07-05 80 views
1

我從JUnit測試開始。當測試失敗時,JUnit測試結果顯示爲0,但測試正常時顯示爲1

我想寫簡單的測試,並得到了問題。

當測試是確定,它被示出在結果: 試驗運行:,失敗:,錯誤:0,跳過:0,所經過的時間:0.008小號

@SpringBootTest 
public class SampleTest { 

    @Test 
    public void Test() { 
     assertEquals(1,1, "Fail"); 
     // assertEquals(1,0, "Fail"); Should fail and display message 
    } 
} 

當我改變一個1比0,所以應該會失敗: 測試運行:,失敗:,錯誤:0,跳過:0,經過時間:0.008小號 它也不會拋出的錯誤信息,應該拋出。

我做錯了什麼,或者它不應該是這樣嗎?試圖谷歌,但無法找到類似的問題。

我正在使用JUnit5。從運行IntelliJ IDEA的測試 - > Maven項目 - >生命週期 - >測試

UPDATE:

我試圖從的IntelliJ運行 - 顯示所有郵件等 奇怪的是,它不是從正常工作Maven測試。

UPDATE 2: 這可能是關於pom.xml配置的東西嗎?

<?xml version="1.0" encoding="UTF-8"?> 
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" 
     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.project</groupId> 
    <artifactId>project</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <packaging>jar</packaging> 

    <name>project</name> 
    <description>project - server</description> 

    <parent> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-parent</artifactId> 
     <version>2.0.0.BUILD-SNAPSHOT</version> 
     <relativePath/> <!-- lookup parent from repository --> 
    </parent> 

    <properties> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
     <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 
     <java.version>1.8</java.version> 
     <junit-jupiter-api-version>5.0.0-M5</junit-jupiter-api-version> 
     <junit.jupiter.version>5.0.0-M5</junit.jupiter.version> 
     <junit.platform.version>1.0.0-M5</junit.platform.version> 
    </properties> 

    <dependencies> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-security</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>io.jsonwebtoken</groupId> 
      <artifactId>jjwt</artifactId> 
      <version>0.7.0</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.session</groupId> 
      <artifactId>spring-session</artifactId> 
      <version>1.3.1.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-social-facebook</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-thymeleaf</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-validation</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-web</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-websocket</artifactId> 
     </dependency> 

     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-actuator</artifactId> 
      <version>LATEST</version> 
     </dependency> 

     <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.datatype/jackson-datatype-jsr310 --> 
     <dependency> 
      <groupId>com.fasterxml.jackson.datatype</groupId> 
      <artifactId>jackson-datatype-jsr310</artifactId> 
      <version>LATEST</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-devtools</artifactId> 
      <scope>runtime</scope> 
     </dependency> 
     <!-- JPA Data (We are going to use Repositories, Entities, Hibernate, etc...) --> 

     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-data-jpa</artifactId> 
     </dependency> 

     <!-- Use MySQL Connector-J --> 

     <dependency> 
      <groupId>mysql</groupId> 
      <artifactId>mysql-connector-java</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-test</artifactId> 
      <scope>test</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.apache.commons</groupId> 
      <artifactId>commons-lang3</artifactId> 
      <version>3.5</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-configuration-processor</artifactId> 
      <optional>true</optional> 
     </dependency> 
     <!-- https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple --> 
     <dependency> 
      <groupId>com.googlecode.json-simple</groupId> 
      <artifactId>json-simple</artifactId> 
      <version>1.1.1</version> 
     </dependency> 
     <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-io --> 
     <dependency> 
      <groupId>org.apache.commons</groupId> 
      <artifactId>commons-io</artifactId> 
      <version>1.3.2</version> 
     </dependency> 
     <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.datatype/jackson-datatype-hibernate5 --> 
     <dependency> 
      <groupId>com.fasterxml.jackson.datatype</groupId> 
      <artifactId>jackson-datatype-hibernate5</artifactId> 
      <version>2.9.0.pr1</version> 
     </dependency> 

     <!-- Swagger --> 
     <dependency> 
      <groupId>io.springfox</groupId> 
      <artifactId>springfox-swagger2</artifactId> 
      <version>2.7.0</version> 
     </dependency> 
     <dependency> 
      <groupId>io.springfox</groupId> 
      <artifactId>springfox-swagger-ui</artifactId> 
      <version>2.7.0</version> 
      <scope>compile</scope> 
     </dependency> 

     <!-- Guava --> 
     <dependency> 
      <groupId>com.google.guava</groupId> 
      <artifactId>guava</artifactId> 
      <version>22.0</version> 
     </dependency> 


     <dependency> 
      <groupId>org.junit.jupiter</groupId> 
      <artifactId>junit-jupiter-api</artifactId> 
      <version>${junit-jupiter-api-version}</version> 
      <type>jar</type> 
     </dependency> 

     <dependency> 
      <groupId>org.junit.jupiter</groupId> 
      <artifactId>junit-jupiter-engine</artifactId> 
      <version>${junit.jupiter.version}</version> 
      <scope>test</scope> 
     </dependency> 


     <dependency> 
      <groupId>org.junit.platform</groupId> 
      <artifactId>junit-platform-runner</artifactId> 
      <version>${junit.platform.version}</version> 
      <scope>test</scope> 
      <exclusions> 
       <exclusion> 
        <groupId>junit</groupId> 
        <artifactId>junit</artifactId> 
       </exclusion> 
      </exclusions> 
     </dependency> 


    </dependencies> 

    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.springframework.boot</groupId> 
       <artifactId>spring-boot-maven-plugin</artifactId> 
      </plugin> 
      <plugin> 
       <artifactId>maven-surefire-plugin</artifactId> 
       <version>2.20</version> 
       <dependencies> 
        <dependency> 
         <groupId>org.junit.platform</groupId> 
         <artifactId>junit-platform-surefire-provider</artifactId> 
         <version>1.0.0-M4</version> 
        </dependency> 
       </dependencies> 
      </plugin> 
     </plugins> 

    </build> 

    <repositories> 
     <repository> 
      <id>spring-snapshots</id> 
      <name>Spring Snapshots</name> 
      <url>https://repo.spring.io/snapshot</url> 
      <snapshots> 
       <enabled>true</enabled> 
      </snapshots> 
     </repository> 
     <repository> 
      <id>spring-milestones</id> 
      <name>Spring Milestones</name> 
      <url>https://repo.spring.io/milestone</url> 
      <snapshots> 
       <enabled>false</enabled> 
      </snapshots> 
     </repository> 

    </repositories> 

    <pluginRepositories> 
     <pluginRepository> 
      <id>spring-snapshots</id> 
      <name>Spring Snapshots</name> 
      <url>https://repo.spring.io/snapshot</url> 
      <snapshots> 
       <enabled>true</enabled> 
      </snapshots> 
     </pluginRepository> 
     <pluginRepository> 
      <id>spring-milestones</id> 
      <name>Spring Milestones</name> 
      <url>https://repo.spring.io/milestone</url> 
      <snapshots> 
       <enabled>false</enabled> 
      </snapshots> 
     </pluginRepository> 
     <pluginRepository> 
      <id>surefire-snapshots</id> 
      <name>Surefire Snapshots</name> 
      <url>https://repository.apache.org/snapshots/org/apache/maven/plugins/maven-surefire-plugin/</url> 
      <snapshots> 
       <enabled>true</enabled> 
      </snapshots> 
     </pluginRepository> 
    </pluginRepositories> 


</project> 
+0

你的意思是你改變了對'的assertEquals(0,0, 「失敗」)產生的OutOfMemoryError的報告; ? –

+0

我只是將其更改爲assertEquals(0,1,「失敗」); –

+0

嘗試使用包裝類型作爲assertEquals(新的整數(0),新的整數(1),「失敗」);並檢查 –

回答

2
  1. 的Maven Surefire插件2.20不支持。有關詳細信息,請參閱https://github.com/junit-team/junit5/issues/809。回滾到2.19.1

  2. 將JUnit(木星和平臺)版本對齊整個pom.xml。有M4和M5的混合。

當我改變一個1比0,所以應該失敗:測試運行:0,失敗:0,錯誤:0,跳過:0,經過時間:0.008 s時,也不會引發應該拋出的錯誤信息。

測試運行:0是因爲Maven的Surefire插件2.20