Created
September 1, 2021 15:23
-
-
Save subieslaw/3c8e9d6570f4a002d3c6d80dceaba923 to your computer and use it in GitHub Desktop.
Code examples for https://www.kodowaniebezcenzury.pl/2021/09/tests-template
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ApartmentServiceTest { | |
| @Test | |
| void shouldFoundExistingApartment() {} | |
| @Test | |
| void shouldCreateAnApartment() {} | |
| @Test | |
| void shouldRecognizeOwnerWasNotFound() {} | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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