도메인 개체에 대한 삽입을 수행하는 테스트 사례가 있습니다. 이제 domain 객체 내에서 "deploymentType"필드 중 하나가 설정되지 않은 경우 postgres에 프로덕션으로 채울 기본값이 있습니다.봄 트랜잭션 테스트 케이스 내에서 트랜잭션을 관리합니다.
null로 설정된 deploymentType으로 삽입 할 때 기본값을 테스트하는 스프링 단위 테스트에서 이것을 테스트하고 싶습니다.
내 테스트 케이스 AbstractTransactionalTestNGSpringContextTests을 연장
@Transactional(propagation = Propagation.NESTED)
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
다음 단위 테스트 케이스가 주석되었다.
@Test
public void createCustomerWithSite() {
this.customerService.createCustomerSite(TestData.makeCustomerSite(this.customer, "test-alias"));
final List<CustomerSite> list = this.customerService.findCustomerSites(this.customer.getId());
assertThat(list.size(), is(1));
final CustomerSite cs = list.get(0);
assertThat(cs.getClusterDeploymentType(), is(ClusterDeploymentType.PRODUCTION));
}
지금 테스트가 트랜잭션이기 때문에이 발생하지 않으며 내가 볼 도메인 개체를 다시 얻을 때 따라서 "deploymentType는"null이며, 테스트가 실패 결코 커밋합니다.
그래서 내가 단위 테스트에서 db 동작을 테스트하고 싶을 때, 나는 트랜잭션 중간에 testManager에 접근 할 필요가 있다고 생각한다. 새 Transaction을 시작하고 db에서 도메인 객체를 가져온 다음 삽입하는 동안 db가 기본값을 설정했는지 확인하십시오.
처럼 : 내가 거래하는 단위 테스트에서 트랜잭션 관리자에 접근하려면 어떻게
this.customerService.createCustomerSite(TestData.makeCustomerSite(this.customer, "test-alias"));
TransactionManager tm = getTransactionManager();
tm.commit(); // the default type will be insterted in db and be visible in next transaction.
tm.beginTransaction();
final List<CustomerSite> list = this.customerService.findCustomerSites(this.customer.getId());
assertThat(list.size(), is(1));
final CustomerSite cs = list.get(0);
assertThat(cs.getClusterDeploymentType(), is(ClusterDeploymentType.PRODUCTION));
. 이것이 올바른 방법일까요?
테스트 중에 db에 커밋을하고 있기 때문에 이것은 단위 테스트가 아닌 통합 테스트의 기능입니다. – Spock