方法:findAll(),save(O o),findOne(long id)的接口 在我的spring jpa集成测试中找不到存储库。
package source;
import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import category.IntegrationTest;
import domain.BankAccount;
import presentation.MessageContext;
import repository.BankAccountRepository;
@RunWith(SpringRunner.class)
@Category(IntegrationTest.class)
@DataJpaTest
public class BankAccountTest {
@Autowired
private BankAccountRepository repository;
@Test
public void should_find_no_customers_if_repository_is_empty() {
Iterable<BankAccount> accounts = repository.findAll();
assertNull(accounts);
}
@Test
public void shuldCreateBankAccount() {
BankAccount accounts = new BankAccount(100);
repository.save(accounts);
BankAccount accountr = repository.findOne(1l);
assertNotNull(accountr);
}
}
存储库界面如下所示
package repository;
import org.springframework.data.repository.Repository;
import domain.BankAccount;
public interface BankAccountRepository extends Repository<BankAccount, Long> {}
我还实现了一个配置文件
package repository;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@Configuration
@EnableJpaRepositories
public class PersistentContext {}
类是这样放置的 Component structure
我必须编译代码而不出错?
答案 0 :(得分:0)
interface Repository是空接口,它是spring数据存储库的通用接口。没有任何方法,请参阅Repository API 。
如果你需要方法findAll(),save(o o),findOne(long id)你需要使用CrudRepository - 它是接口Repository的子接口,或者你可以使用JpaRepository - 它的扩展名CrudRepository。