Java Spring Junit测试实体未保存到存储库

Java Spring Junit测试实体未保存到存储库,java,spring,junit,Java,Spring,Junit,我正在尝试测试一个列出所有实体的服务方法。方法如下所示: @Test public void listAllProfiles() { Profile sampleProfile = Profile.createWithDefaultValues(); profileRepository.save(sampleProfile); ProfileService profileService = new ProfileService(profileRepository, new Mode

我正在尝试测试一个列出所有实体的服务方法。方法如下所示:

@Test
public void listAllProfiles() {
  Profile sampleProfile = Profile.createWithDefaultValues();
  profileRepository.save(sampleProfile);

  ProfileService profileService = new ProfileService(profileRepository, new ModelMapper());
  List<ProfileDto> profiles = profileService.listAllProfiles();
  ProfileDto lastProfile = profiles.get(profiles.size() - 1);

  assertEquals(sampleProfile.getId(), lastProfile.getId());
}
 List<Profile> profilesList = Arrays.asList(new Profile("Profile 1"), new Profile("Proiile 2"));
 given(profileRepository.findAll()).willAnswer((Answer<List>) invocation -> profilesList);
测试失败导致IndexOutOfBoundsException:索引-1超出长度0的界限

我发现sampleProfile在测试期间可能没有保存。当我记录profileRepository.findAll.size时,该值始终为0

我的测试出了什么问题?

如果你想测试你的ProfileService,你必须模拟profileRepository,因为如果你不这样做,那么你实际上是在做一个集成测试

单元测试是关于测试小单元的,如果这些单元有任何依赖关系,您必须手动或使用框架模拟它们,这是Java世界中最常见的一种

如果您的服务正在使用存储库获取您必须模拟该调用的所有配置文件,如下所示:

@Test
public void listAllProfiles() {
  Profile sampleProfile = Profile.createWithDefaultValues();
  profileRepository.save(sampleProfile);

  ProfileService profileService = new ProfileService(profileRepository, new ModelMapper());
  List<ProfileDto> profiles = profileService.listAllProfiles();
  ProfileDto lastProfile = profiles.get(profiles.size() - 1);

  assertEquals(sampleProfile.getId(), lastProfile.getId());
}
 List<Profile> profilesList = Arrays.asList(new Profile("Profile 1"), new Profile("Proiile 2"));
 given(profileRepository.findAll()).willAnswer((Answer<List>) invocation -> profilesList);
因此,您不应该在数据库中保存任何内容实际上,在进行单元测试时,您根本不应该与数据库进行交互,您只需模拟您在服务上使用的存储库。这里是我几个月前写的一篇文章,我实际上解决了完全相同的问题。

如果你想测试你的ProfileService,你必须模拟profileRepository,因为如果你不这样做,那么你实际上是在做集成测试

单元测试是关于测试小单元的,如果这些单元有任何依赖关系,您必须手动或使用框架模拟它们,这是Java世界中最常见的一种

如果您的服务正在使用存储库获取您必须模拟该调用的所有配置文件,如下所示:

@Test
public void listAllProfiles() {
  Profile sampleProfile = Profile.createWithDefaultValues();
  profileRepository.save(sampleProfile);

  ProfileService profileService = new ProfileService(profileRepository, new ModelMapper());
  List<ProfileDto> profiles = profileService.listAllProfiles();
  ProfileDto lastProfile = profiles.get(profiles.size() - 1);

  assertEquals(sampleProfile.getId(), lastProfile.getId());
}
 List<Profile> profilesList = Arrays.asList(new Profile("Profile 1"), new Profile("Proiile 2"));
 given(profileRepository.findAll()).willAnswer((Answer<List>) invocation -> profilesList);

因此,您不应该在数据库中保存任何内容实际上,在进行单元测试时,您根本不应该与数据库进行交互,您只需模拟您在服务上使用的存储库。这里是我几个月前写的一篇文章,我实际上解决了完全相同的问题。

显示完整的测试类代码,您是否使用内存中的db进行集成测试/显示完整的测试类代码,您是否使用内存中的db进行集成测试/感谢您的回答和对单元测试的解释。你说得对。我试试看。工作做得很好。谢谢你分享你的代码库!感谢您的回答和对单元测试的解释。你说得对。我试试看。工作做得很好。谢谢你分享你的代码库!