How to test Spring Data repositories?

前端 未结 8 1818
隐瞒了意图╮
隐瞒了意图╮ 2020-11-29 15:41

I want a repository (say, UserRepository) created with the help of Spring Data. I am new to spring-data (but not to spring) and I use this tutorial. My choice o

8条回答
  •  青春惊慌失措
    2020-11-29 15:41

    In the last version of spring boot 2.1.1.RELEASE, it is simple as :

    @RunWith(SpringRunner.class)
    @SpringBootTest(classes = SampleApplication.class)
    public class CustomerRepositoryIntegrationTest {
    
        @Autowired
        CustomerRepository repository;
    
        @Test
        public void myTest() throws Exception {
    
            Customer customer = new Customer();
            customer.setId(100l);
            customer.setFirstName("John");
            customer.setLastName("Wick");
    
            repository.save(customer);
    
            List queryResult = repository.findByLastName("Wick");
    
            assertFalse(queryResult.isEmpty());
            assertNotNull(queryResult.get(0));
        }
    }
    

    Complete code:

    https://github.com/jrichardsz/spring-boot-templates/blob/master/003-hql-database-with-integration-test/src/test/java/test/CustomerRepositoryIntegrationTest.java

提交回复
热议问题