我正在尝试进行junit测试以使用JPA保存数据。下面是我的实体类
@Entity
@Table(name="book")
public class test {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="BOOK_REF_ID",nullable=false)
private int bookRefId;
@Column(name="BOOK_CODE",nullable=false)
private String bookCode;
@Column(name="BOOK_NAME",nullable=false)
private String bookDescription;
public int getBookRefId() {
return bookRefId;
}
public void setBookRefId(int bookRefId) {
this.bookRefId = bookRefId;
}
public String getBookCode() {
return bookCode;
}
public void setBookCode(String bookCode) {
this.bookCode = bookCode;
}
public String getBookDescription() {
return bookDescription;
}
public void setBookDescription(String bookDescription) {
this.bookDescription = bookDescription;
}
}
服务类别为
public interface BookService()
{
public Book create(Book book);
}
存储库类为
public interface BookRepository extends
JpaRepository<Book,Integer>
{ }
服务实现类为
public BookServiceImpli implements BookService()
{
@Resource
BookRepository repository;
@Override
public Book create(Book book) {
// TODO Auto-generated method stub
return repository.save(book);
}
}
现在我的考试班是
@RunWith(SpringRunner.class)
@DataJpaTest
@SpringBootTest(classes= {JPAConfig.class})
@AutoConfigureTestDatabase(replace=Replace.NONE)
@TestPropertySource(
locations = "classpath:application.properties")
public class TestBook {
@Autowired
private BookService bookService ;
@Test
public void test() {
Book book = new Book();
book.setBookCode("abc");
book.setBookDescription("safd");
bookService.create(book);
}
应用程序属性包含密码和数据库详细信息,而JPAConfig包含JPA配置详细信息,例如实体扫描数据库详细信息。尝试运行测试用例时,出现类似
的错误 A component required a bean of type
'com.repository.sample.BookRepository' that could not be found.
我没有主要方法。对单元测试不熟悉的人请帮助我解决问题。