Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我应该在我的spring项目中使用什么样的测试(我做得对吗)?_Spring_Spring Boot_Testing_Junit_Mockito - Fatal编程技术网

我应该在我的spring项目中使用什么样的测试(我做得对吗)?

我应该在我的spring项目中使用什么样的测试(我做得对吗)?,spring,spring-boot,testing,junit,mockito,Spring,Spring Boot,Testing,Junit,Mockito,我的项目是一个简单的商店管理系统,连接到MySQL数据库 我只有像这样的JUnit测试(这个测试写得正确吗?) 我应该添加Mockito测试吗?将来我应该添加什么样的测试?我会避免使用Mockito测试您的JPA历史记录 最好使用真实的测试数据来创建内存中的数据库(可以旋转和拆卸) 如果您使用的是@DataJpaTest注释,那么您应该使用sql为数据库种子,请查看以下内容。您可以使用sql文件并摆脱TestEntityManager 看看这个。您可以将测试数据添加到资源目录中的XML文件中。H

我的项目是一个简单的商店管理系统,连接到MySQL数据库

我只有像这样的JUnit测试(这个测试写得正确吗?)


我应该添加Mockito测试吗?将来我应该添加什么样的测试?

我会避免使用Mockito测试您的JPA历史记录

最好使用真实的测试数据来创建内存中的数据库(可以旋转和拆卸)

如果您使用的是@DataJpaTest注释,那么您应该使用sql为数据库种子,请查看以下内容。您可以使用sql文件并摆脱TestEntityManager


看看这个。您可以将测试数据添加到资源目录中的XML文件中。

Hi。感谢您展示了正确的方法来测试我的项目!我一定会在我的测试中使用它。
@RunWith(SpringRunner.class)
@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
public class EmployeeRepositoryIntegrationTest
{
    @Autowired
    private TestEntityManager entityManager;

    @Autowired
    private EmployeeRepository employeeRepository;

    @Test
    public void whenFindByLastName_thenReturnEmployee()
    {
        User employee = new User("UserName","password","John",
                        "Smith","Adress Test","123123123","CityTest");

        entityManager.persist(employee);
        entityManager.flush();

        User result = userRepository.findUserByLastName(employee.getLastName()).get(0);
        assertThat(result.getLastName()).isEqualTo(employee.getLastName());
    }
}