简体   繁体   English

Spring Unit测试JPA存储库

[英]Spring Unit test JPA repository

I'm new to Spring framework. 我是Spring框架的新手。 I need to write Unit Test for JPA repository. 我需要为JPA存储库编写单元测试。 I'm trying simple repository saveAndFlush() method. 我正在尝试简单的存储库saveAndFlush()方法。 But nothing saves in my repository. 但我的存储库中没有任何东西存 Here is my source code: 这是我的源代码:

TestContext.class TestContext.class

@Configuration 
@PropertySource("classpath:log4j.properties") 
public class TestContext {

    @Bean
    public RoleService roleService() {
        return Mockito.mock(RoleService.class);
    }

    @Bean
    public RightService RightService() {
        return Mockito.mock(RightService.class);
    }

    @Bean
    public RoleRepository RoleRepository() {
        return Mockito.mock(RoleRepository.class); 
    }
}

RoleServiceTest.class RoleServiceTest.class

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {TestContext.class})
@WebAppConfiguration
public class RoleServiceTest {

    @Autowired
    private RoleRepository roleRepository;

    @Test
    public void TestServices() throws Exception {
        RoleDetails first = new RoleDetails();
        first.setId("1");
        first.setDescription("First Description");
        first.setName("First");
        roleRepository.saveAndFlush(new RoleEntity(first));
        roleRepository.save(new RoleEntity(first));
        List<RoleEntity> roles = new ArrayList<RoleEntity>();
        roles = roleRepository.findAll();
        System.out.println(roles);
        assertEquals(1, roles.size());
    }
}

And error: 并且错误:

java.lang.AssertionError: expected:<1> but was:<0>

I'm almost sure that problem occurs because of testContext.Class. 我几乎可以肯定,因为testContext.Class会出现问题。 I used this class to test my controller and it worked well, but now i need to test my database and i don't know how to modify contextConfiguration. 我用这个类测试我的控制器,它运行良好,但现在我需要测试我的数据库,我不知道如何修改contextConfiguration。 I hope somone will help me. 我希望somone会帮助我。 Thanks in advance! 提前致谢!

The problem is from the TestContext indeed. 问题来自TestContext确实。 You try to save your object using a mock object, which is not correct. 您尝试使用模拟对象保存对象,这是不正确的。

The solution is to use the real repository. 解决方案是使用真实的存储库。 For this, you need to follow the next steps: 为此,您需要按照以下步骤操作:

  1. Annotate your RoleRepository with the @Repository annotation and extend the class with JpaRepository(RoleEntity,ID) (where ID is the type you declared the id of the RoleEntity). @Repository注解注释您RoleRepository与JpaRepository(RoleEntity,ID)(其中ID是你声明的RoleEntity的ID类型)扩展类。
  2. Add the RoleRepository to your Context Configuration class (the real one, not a test one). 将RoleRepository添加到Context Configuration类(真实的,不是测试类)。 You can do this by adding @EnableJpaRepositories(value="your.repository.package") . 您可以通过添加@EnableJpaRepositories(value =“your.repository.package”)来完成此操作。
  3. Replace the TestContext.class from your @ContextConfiguration annotation on your RoleServiceTest class with your real Context Configuration class you used to configure your Spring based project. 使用您用于配置基于Spring的项目的真实Context Configuration类,将RoleContext.class替换为RoleServiceTest类上的@ContextConfiguration批注。

I hope my answer helps, if you still need help feel free to ask again! 我希望我的回答有所帮助,如果你还需要帮助,请随时再问!

Your repository is a mock object. 您的存储库是一个模拟对象。 A mock object, by definition, is an object that doesn't do what it should normally do, but does what you tell it to do in the test. 根据定义,模拟对象是一个不执行通常应该执行的操作的对象,而是在测试中执行的操作。

To test the repository, the repository must be a real one. 要测试存储库,存储库必须是真实存储库。 Your context class should thus rather have 因此,您的上下文类应该具有

@Bean
public RoleRepository RoleRepository() {
    return new RoleRepositoryImpl(); // or whatever the class implementing the repository is 
}

If using Spring Boot, creating a web app and you're running off of a main() method within an Application.class you could use: 如果使用Spring Boot,创建​​一个Web应用程序,并且您正在运行Application.class中的main()方法,您可以使用:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
public class MyUnitTest {

    Some someInstance = new Some();

    @Autowired
    private SomeRepository someRepository;
}

@Test
public void testSomeClass() throws Exception {
    Some savedSome = someRepository.save(someInstance);
    assertEquals(1, someRepository.count());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM