我正在构建一个JEE EAR应用程序,并且开始使用EasyMock为我的服务正在使用的存储库创建模拟。但是我不确定结果是否正确。特别是在我测试无效方法的地方进行了两次测试。

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);
}


这些测试都通过了。但是,如果我注释掉删除/更新部分,它们仍然会通过。

@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);
}


我这样做对吗?我的猜测是否定的,如果是,您如何正确测试?

谢谢!

最佳答案

对于更新,您正在记录返回originalVenue。由于您正在修改其名称,因此确实会有所不同。在所有测试中,只有一个originalVenue实例。

对于删除,您正在录制venue1,然后录制null。因此,如果您两次拨打findById,您将收到venue1null。测试删除后,您可以仅模拟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);
}

07-28 09:18