您好我想在h2中对我的数据库进行集成测试。但是我的方法有些问题。 我的集成测试代码:
@RunWith(SpringRunner.class)
@DataJpaTest
public class Team_database_integration_test {
@MockBean
private TeamRepository teamRepository;
@Autowired
private TestEntityManager testEntityManager;
@Test
public void testDb() {
Team team = new Team(1L, "teamName", "teamDescription", "krakow", 7);
testEntityManager.persist(team);
testEntityManager.flush();
Assert.assertEquals(1L, teamRepository.findById(team.getId()));
}
}
我的错误:
java.lang.AssertionError:
Expected :1
Actual :Optional.empty
我认为原因在于我的服务方法findbyid:
public Optional<TeamDto> findTeamById(Long id) {
Assert.notNull(id, "ID must exist ");
return teamRepository
.findById(id)
.map(p -> modelMapper.map(p, TeamDto.class));
}