How to test Spring Data repositories?

前端 未结 8 1816
隐瞒了意图╮
隐瞒了意图╮ 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 16:00

    With Spring Boot + Spring Data it has become quite easy:

    @RunWith(SpringRunner.class)
    @DataJpaTest
    public class MyRepositoryTest {
    
        @Autowired
        MyRepository subject;
    
        @Test
        public void myTest() throws Exception {
            subject.save(new MyEntity());
        }
    }
    

    The solution by @heez brings up the full context, this only bring up what is needed for JPA+Transaction to work. Note that the solution above will bring up a in memory test database given that one can be found on the classpath.

提交回复
热议问题