Domain sadece bir arayüzü yani portu biliyor.
Örnek
Elimizde şöyle bir arayüz olsun
public interface StudentRepository {Student save(Student student);Optional<Student> retrieveStudentWithEmail(ContactInfo contactInfo);Publisher<Student> saveReactive(Student student);}
Bu arayüz için domain tarafında test yazalım
public class StudentRepositoryTest { StudentRepository studentRepository; @Test public void shouldCreateStudent() { Student expected = ...; Student actual = studentRepository.save(expected); ... } @Test public void shouldUpdateExistingStudent() { Student expected = randomExistingStudent(); Student actual = studentRepository.save(expected); ... } }
Şimdi bellekte repository gerçekleştirimi yani adapter için test yazalım
public class StudentRepositoryInMemoryIT extends StudentRepositoryTest { @BeforeEach public void setup() { super.studentRepository = new StudentRepositoryInMemory(); } }
Şimdi de gerçek veri tabanı gerçekleştirimi yani adapter için test yazalım. Burada SpringBoot kullanılıyor
@Testcontainers @ContextConfiguration(classes = {PersistenceConfig.class}) @DataJpaTest @AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) public class StudentRepositoryJpaIT extends StudentRepositoryTest { @Autowired public StudentRepository studentRepository; @Container public static PostgreSQLContainer container = new PostgreSQLContainer("postgres:latest") .withDatabaseName("students_db") .withUsername("sa") .withPassword("sa"); @DynamicPropertySource public static void overrideProperties(DynamicPropertyRegistry registry){ registry.add("spring.datasource.url", container::getJdbcUrl); registry.add("spring.datasource.username", container::getUsername); registry.add("spring.datasource.password", container::getPassword); registry.add("spring.datasource.driver-class-name", container::getDriverClassName); } @BeforeEach public void setup() { super.studentRepository = studentRepository; } }
Hiç yorum yok:
Yorum Gönder