@RunWith(SpringJUnit4ClassRunner.class)
@Transactional
@ContextConfiguration(classes = SimpleConfiguration.class)
public class CustomerSyncRepositoryTest {
@Autowired
CustomerSyncRepository customerSyncRepository;
@Autowired
CustomerRepository customerRepository;
@Autowired
MemberShipRepository memberShipRepository;
@Test
public void collectDataTest() {
CustomerSync customerSyncOne = new CustomerSync();
customerSyncOne.setMembershipNumber("1");
customerSyncRepository.save(customerSyncOne);
Membership membership = new Membership();
membership.setName("钻石");
membership = memberShipRepository.save(membership);
Customer customerOne = new Customer();
customerOne.setMembershipNumber("1");
customerOne.setMembership(membership);
customerRepository.save(customerOne);
Customer customerTwo = new Customer();
customerTwo.setMembershipNumber("2");
customerTwo.setMembership(membership);
customerRepository.save(customerTwo);
customerSyncRepository.collectMissingSyncData();
assertEquals(2, customerSyncRepository.findAll().size());
}
}
there should be 2 records in db, but it seems that the new record it is never inserted into DB. And when I test it by starting up the application, there are 2 records in db. Is there any configruation I miss in unit test? here is what customerSyncRepository.collectMissingSyncData() do.
public interface CustomerSyncRepository extends JpaRepository<CustomerSync, String> {
@Modifying
@Query(nativeQuery = true, value = "insert into customer_sync(membershipNumber,membership, cnName, enName, phoneNum, password, email, firstname, lastname, active, otherType, otherNum ) SELECT " +
"a.membershipNumber, " +
"b.name, " +
"a.cnName, " +
"a.enName, " +
"a.phoneNum ," +
"a.password, " +
"a.email, " +
"a.firstname, " +
"a.lastname, " +
"a.active, " +
"a.otherType, " +
"a.otherNum " +
"FROM " +
"customer a " +
"JOIN membership b ON (a.membership_id = b.id) " +
"WHERE " +
"a.membershipNumber NOT IN (select membershipNumber from customer_sync) ")
void collectMissingSyncData();
List<CustomerSync> findBySync(boolean isSync);
}