目前,我正在构建一个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;
1条答案
按热度按时间zpqajqem1#
您使用的是JUnit 5,因此不应该为了得到
assertThat
而将JUnit 4混合到其中。这是一个单独依赖项Hamcrest的一部分。JUnit 4对Hamcrest有依赖性,但是对于JUnit 5,这种集成已经被删除。相反,您只需包含Hamcrest(而不是JUnit 4):