การทดสอบ Repository มีสองแง่มุมให้พิจารณา ประการแรก คุณต้องทดสอบตัว Repository เองเพื่อพิสูจน์ว่ามันทำงานได้อย่างถูกต้อง และคุณยังต้องทดสอบโค้ดที่ใช้ Repository ในการจัดเก็บ Aggregates ที่สร้างขึ้นใหม่และค้นหา Aggregates ที่มีอยู่เดิมด้วย สำหรับการทดสอบแบบแรก คุณจำเป็นต้องใช้ implementation ที่มีคุณภาพระดับ production จริง มิฉะนั้นคุณจะไม่มีทางรู้ได้เลยว่าโค้ด production ของคุณจะทำงานได้หรือไม่ ส่วนการทดสอบแบบที่สอง คุณจะใช้ production implementation หรือจะใช้ in-memory implementation แทนก็ได้ ผมจะพูดถึงการทดสอบ production implementation ในตอนนี้ก่อน แล้วค่อยเลื่อนเรื่องการทดสอบแบบ in-memory ไปอีกสักครู่

ลองมาดู test ของ Coherence implementation ของ Prod-uctRepository ที่นำเสนอไปก่อนหน้านี้กัน:

public class CoherenceProductRepositoryTest extends DomainTest {

    private ProductRepository productRepository;
    private TenantId tenantId;

    public CoherenceProductRepositoryTest() {
        super();
    }
    ...
    @Override
    protected void setUp() throws Exception {
        this.setProductRepository(new CoherenceProductRepository());
        this.tenantId = new TenantId("01234567");
        super.setUp();
    }

    @Override
    protected void tearDown() throws Exception {
        Collection<Product> products =
            this.productRepository()
                    .allProductsOfTenant(tenantId);

        this.productRepository().removeAll(products);
    }

    protected ProductRepository productRepository() {
        return this.productRepository;
    }

    protected void setProductRepository(
            ProductRepository aProductRepository) {
        this.productRepository = aProductRepository;
    }
}

มีการดำเนินการ setup และ tear-down ทั่วไปเพื่อเตรียมความพร้อมก่อนการทดสอบแต่ละครั้งและทำความสะอาดหลังการทดสอบเสร็จสิ้น ในการ setup เราเริ่มจากการสร้าง instance ของ class Coherence-ProductRepository แล้วจึงสร้าง fake instance ของ TenantId ขึ้นมา

ในการ tear down เราจะลบ Product instances ทั้งหมดที่อาจถูกเพิ่มเข้าไปใน backing cache จากการทดสอบแต่ละครั้ง สำหรับ Coherence แล้ว นี่เป็นขั้นตอนการทำความสะอาดที่สำคัญทีเดียว เพราะถ้าคุณไม่ลบ cached instances เหล่านั้นออก มันจะยังตกค้างอยู่ในการทดสอบครั้งต่อๆ ไป ซึ่งอาจทำให้ assertion บางอย่าง เช่น การนับจำนวน instance ที่ถูก persist ล้มเหลวได้

ต่อไป เรามาทดสอบพฤติกรรมของ Repository กัน:

public class CoherenceProductRepositoryTest extends DomainTest {
    ...
    public void testSaveAndFindOneProduct() throws Exception {

        Product product =
            new Product(
                    tenantId,
                    this.productRepository().nextIdentity(),
                    "My Product",
                    "This is the description of my product.");

        this.productRepository().save(product);

        Product readProduct =
            this.productRepository()
                .productOfId(tenantId, product.productId());

        assertNotNull(readProduct);
        assertEquals(readProduct.tenantId(), tenantId);
        assertEquals(readProduct.productId(), product.productId());
        assertEquals(readProduct.name(), product.name());
        assertEquals(readProduct.description(), product.description());
    }
    ...
}

ตามที่ชื่อของ test method ระบุไว้ ในที่นี้เราจะ save Product ตัวหนึ่งแล้วพยายามค้นหามันกลับมา งานแรกคือการ instantiate Product แล้ว save ลงใน Repository ถ้า infrastructure ไม่ throw exception ใดๆ ออกมา เราก็อาจเดาได้ว่า Product ถูก save อย่างถูกต้องแล้ว แต่มันมีวิธีเดียวเท่านั้นที่จะรู้ได้อย่างแน่นอน นั่นคือเราต้องค้นหา instance นั้นกลับมาแล้วเปรียบเทียบกับต้นฉบับ ในการค้นหา instance เราจะส่ง identity ที่ไม่ซ้ำกันในระดับโลกของมันให้กับ method productOfId() ถ้าค้นหา instance เจอ เราก็จะสามารถ assert ได้สำเร็จว่ามันไม่ใช่ null ว่า tenantId ของมันตรงกัน productId ตรงกัน name ตรงกัน และ description ตรงกับที่เก็บไว้ตอนแรก

ต่อไป เรามาทดสอบการ save และการค้นหาหลาย instances กัน:

public class CoherenceProductRepositoryTest extends DomainTest {
    ...
    public void testSaveAndFindMultipleProducts() throws Exception {

        Product product1 =
            new Product(
                    tenantId,
                    this.productRepository().nextIdentity(),
                    "My Product 1",
                    "This is the description of my first product.");

        Product product2 =
            new Product(
                    tenantId,
                    this.productRepository().nextIdentity(),
                    "My Product 2",
                    "This is the description of my second product.");

        Product product3 =
            new Product(
                    tenantId,
                    this.productRepository().nextIdentity(),
                    "My Product 3",
                    "This is the description of my third product.");

        this.productRepository()
            .saveAll(Arrays.asList(product1, product2, product3));

        assertNotNull(this.productRepository()
            .productOfId(tenant, product1.productId()));
        assertNotNull(this.productRepository()
            .productOfId(tenant, product2.productId()));
        assertNotNull(this.productRepository()
            .productOfId(tenant, product3.productId()));

        Collection<Product> allProducts =
            this.productRepository().allProductsOfTenant(tenant);

        assertEquals(allProducts.size(), 3);
    }
    ...
}

อันดับแรก เราสร้าง Product instances ขึ้นมาสามตัว แล้ว save ทั้งหมดพร้อมกันในครั้งเดียวโดยใช้ saveAll() จากนั้นเราก็ใช้ productOfId() อีกครั้งเพื่อค้นหาแต่ละ instance หากทั้งสาม instance ไม่เป็น null เราก็มั่นใจได้ว่าทั้งสาม instance ถูก persist อย่างถูกต้องแล้ว

Cowboy Logic

AJ: “พี่สาวผมเล่าว่า สามีของเธอบอกให้เธอขายของทั้งหมดในห้องเก็บของของเขาทิ้งเมื่อเขาตาย พี่สาวถามเขาว่าทำไม เขาตอบว่าเขาไม่อยากให้ไอ้คนงี่เง่าคนไหนได้ของๆ เขาไปตอนที่เธอแต่งงานใหม่ พี่สาวเลยบอกให้เขาไม่ต้องห่วง เพราะเธอคงไม่แต่งงานกับคนงี่เง่าอีกคนหรอก”

Image

ยังมี Repository method อยู่อีกตัวหนึ่ง นั่นคือ allProductsOfTenant() ที่ยังไม่ได้รับการทดสอบ เนื่องจาก cache ของ Repository ว่างเปล่าอย่างสิ้นเชิงตั้งแต่การทดสอบเริ่มต้น เราจึงควรจะอ่าน Product instances ออกมาได้ทั้งสามตัว ดังนั้นเราจึงพยายามค้นหาพวกมันทั้งหมด Collection ที่ return กลับมาไม่ควรเป็น null อย่างเด็ดขาด ต่อให้คุณไม่พบสิ่งที่คาดหวังก็ตาม เพราะฉะนั้นขั้นตอนสุดท้ายในการทดสอบคือการ assert ว่าจำนวน Product instances ที่คาดหวังทั้งหมด นั่นคือสามตัว ถูกค้นพบจริง

ตอนนี้เราได้ test ที่สาธิตให้เห็นว่า client ใช้ Repository อย่างไรและพิสูจน์ความถูกต้องของมันแล้ว เราก็สามารถมาดูกันได้ว่าเราจะทดสอบ client ที่ใช้ Repositories ได้อย่างเหมาะสมที่สุดได้อย่างไร