Skip to content

Instantly share code, notes, and snippets.

@subieslaw
Created September 1, 2021 15:23
Show Gist options
  • Save subieslaw/3c8e9d6570f4a002d3c6d80dceaba923 to your computer and use it in GitHub Desktop.
Save subieslaw/3c8e9d6570f4a002d3c6d80dceaba923 to your computer and use it in GitHub Desktop.
class ApartmentServiceTest {
@Test
void shouldFoundExistingApartment() {}
@Test
void shouldCreateAnApartment() {}
@Test
void shouldRecognizeOwnerWasNotFound() {}
}
public class ApartmentService {
private static final ApartmentId NO_ID = ApartmentId.nullObject();
public ApartmentId add(OwnerId ownerId, ApartmentDto dto) {
if (ownerRepository.exists(ownerId)) {
Address address = new Address(dto.getStreet(), dto.getHouseNumber(), dto.getApartmentNumber(), dto.getCity(), dto.getCountry());
Optional<Apartment> found = apartmentRepository.findBy(address);
if (found.isPresent()) {
Apartment apartment = found.get();
eventRegistry.publish(new ApartmentAlreadyCreated(apartment.getId(), dto.getStreet(), dto.getHouseNumber(), dto.getApartmentNumber(), dto.getCity(), dto.getCountry()));
return apartment.getId();
} else {
Apartment apartment = new Apartment(ownerId, address);
return apartmentRepository.save(apartment);
}
} else {
eventRegistry.publish(new OwnerNotFound(ownerId));
}
return NO_ID;
}
class ApartmentServiceTest {
@Test
void shouldDoSomething() {
// GIVEN
givenAssumptionOne();
givenAssumptionTwo();
// WHEN
Result actual = systemUnderTest.doSomething();
// THEN
thenTrueIsFirst();
thenTrueIsSecond();
}
@Test
void shouldFoundExistingApartment() {
OwnerId ownerId = givenExistingOwner();
ApartmentDto apartmentDto = givenValidExistingApartmentDto();
ApartmentId id = service.add(ownerId, apartmentDto);
thenReturnedIdOfExistingApartment(id, ownerId, apartmentDto);
thenRecognizedApartmentAlreadyExists(ownerId, apartmentDto);
thenApartmentWasNotCreated(apartmentDto);
}
@Test
void shouldCreateAnApartment() {
OwnerId ownerId = givenExistingOwner();
ApartmentDto apartmentDto = givenValidNotExistingApartmentDto();
ApartmentId id = service.add(ownerId, apartmentDto);
thenApartmentWasCreated(id, ownerId, apartmentDto);
}
@Test
void shouldRecognizeOwnerWasNotFound() {
OwnerId ownerId = givenNotExistingOwner();
ApartmentDto apartmentDto = givenValidNotExistingApartmentDto();
ApartmentId id = service.add(ownerId, apartmentDto);
thenInvalidApartmentIdReturned(id);
thenRecognizedOwnerWasNotFound(ownerId);
thenApartmentWasNotCreated(apartmentDto);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment