我正在构建一个JEE EAR应用程序,我开始使用EasyMock为我的服务正在使用的存储库创建模拟。但我不太确定结果是否正确。两个测试,特别是我正在测试void方法。
public class VenueServiceTest extends ServiceTest {
private VenueService venueService = new VenueServiceImpl();
private VenueRepository mockVenueRepository; // interface that the service is using
private List<Venue> venues;
private Venue venue1;
private Venue venue2;
@Override
public void setupMock() {
venues = MockUtils.getMockedVenues();
venue1 = venues.get(0);
venue2 = venues.get(1);
mockVenueRepository = createMock(VenueRepository.class);
venueService.setRepository(mockVenueRepository);
}
@Override
public void testUpdate() {
Venue originalVenue = new Venue(0L, "The Venue");
String originalName = originalVenue.getName();
mockVenueRepository.update(originalVenue);
expect(mockVenueRepository.findById(originalVenue.getId())).andReturn(originalVenue);
replay(mockVenueRepository);
originalVenue.setName("Another Venue");
venueService.updateEntity(originalVenue);
Venue newVenue = venueService.getEntity(originalVenue.getId());
String newName = newVenue.getName();
assertFalse("Venue names should not be equal", originalName.equals(newName));
verify(mockVenueRepository);
}
@Override
public void testDelete() {
expect(mockVenueRepository.findById(venue1.getId())).andReturn(venue1);
mockVenueRepository.remove(venue1);
expect(mockVenueRepository.findById(venue1.getId())).andReturn(null);
replay(mockVenueRepository);
assertNotNull(venueService.getEntity(venue1.getId()));
venueService.deleteEntity(venue1);
assertNull(venueService.getEntity(venue1.getId()));
verify(mockVenueRepository);
}
这两个测试通过。但如果我注释掉delet / update部分,他们仍然会通过。
@Override
public void testUpdate() {
Venue originalVenue = new Venue(0L, "The Venue");
String originalName = originalVenue.getName();
// mockVenueRepository.update(originalVenue);
expect(mockVenueRepository.findById(originalVenue.getId())).andReturn(originalVenue);
replay(mockVenueRepository);
originalVenue.setName("Another Venue");
// venueService.updateEntity(originalVenue);
Venue newVenue = venueService.getEntity(originalVenue.getId());
String newName = newVenue.getName();
assertFalse("Venue names should not be equal", originalName.equals(newName));
verify(mockVenueRepository);
}
@Override
public void testDelete() {
expect(mockVenueRepository.findById(venue1.getId())).andReturn(venue1);
// mockVenueRepository.remove(venue1);
expect(mockVenueRepository.findById(venue1.getId())).andReturn(null);
replay(mockVenueRepository);
assertNotNull(venueService.getEntity(venue1.getId()));
// venueService.deleteEntity(venue1);
assertNull(venueService.getEntity(venue1.getId()));
verify(mockVenueRepository);
}
我这样做了吗?我的猜测是否定的,如果是这样,你如何正确测试?
谢谢!
答案 0 :(得分:1)
对于更新,您正在录制以返回originalVenue
。由于您正在修改其名称,因此确实会有所不同。在所有测试中只有originalVenue
的一个实例。
要进行删除,您正在录制venue1
,然后录制null
。因此,如果您拨打findById
两次,则会收到venue1
和null
。测试删除,您只能模拟remove
方法以确保它被调用。调用getEntity
会让您测试与删除无关的getEntity
。
基本上,为了测试更新和删除,我会做
@Override
public void testUpdate() {
Venue originalVenue = new Venue(0L, "The Venue");
String originalName = originalVenue.getName();
mockVenueRepository.update(originalVenue);
replay(mockVenueRepository);
originalVenue.setName("Another Venue");
venueService.updateEntity(originalVenue);
verify(mockVenueRepository);
}
@Override
public void testDelete() {
mockVenueRepository.remove(venue1);
replay(mockVenueRepository);
venueService.deleteEntity(venue1);
verify(mockVenueRepository);
}