簡體   English   中英

JUnit:找不到 assertThat() 方法

[英]JUnit: can't find assertThat() method

目前,我正在構建一個 Spring 應用程序,我想使用 JUnit 編寫一些測試,但由於某種原因,IntelliJ 找不到 assertThat() 方法。 這是我的代碼:

package com.mycompany;

import static org.junit.Assert.*;
import static org.hamcrest.MatcherAssert.assertThat;
import com.mycompany.book.Book;
import com.mycompany.book.BookRepository;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.annotation.Rollback;

@DataJpaTest
@AutoConfigureTestDatabase (replace = AutoConfigureTestDatabase.Replace.NONE)
@Rollback(value = false)
public class BookRepositoryTests {
    @Autowired private BookRepository repo;

    @Test
    public void testAddNew(){
        Book book = new Book();
        book.setTitle("The Divine Comedy");
        book.setAuthor("Dante Alighieri");
        book.setGenre("Poetry");

        Book savedBook = repo.save(book);

    }
}

我試圖手動添加依賴項

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

而且我還確保使用以下方法導入它:

import static org.junit.Assert.*;
import static org.hamcrest.MatcherAssert.assertThat;

您正在使用 JUnit 5,因此您不應該為了獲得assertThat而將 JUnit 4 混入其中。 這是一個單獨的依賴項 Hamcrest 的一部分。 JUnit 4 依賴於 Hamcrest,但對於 JUnit 5,該集成已被刪除。 相反,只需自己包含 Hamcrest(而不是 JUnit 4):

<dependency>
    <groupId>org.hamcrest</groupId>
    <artifactId>hamcrest</artifactId>
    <version>2.2</version>
    <scope>test</scope>
</dependency>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM