鉴于此课程有两个测试:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@TestPropertySource(locations = "classpath:config/application-test.properties")
public class AccountDaoTest {
@Autowired
private UserDao userDao;
@Autowired
private AccountDao accountDao;
@Test
public void getEmail() {
User user = new User();
userDao.save(user);
Account account = new Account();
account.setUserId(user.getId());
account.setEmail("testemail@test.te");
accountDao.save(account);
Email email = accountDao.getEmailByAccountId(account.getId());
assertThat(email, is("testemail@test.te"));
}
@Test
public void deactivate() {
User user = new User();
userDao.save(user);
Account account = new Account();
account.setUserId(user.getId());
account.isActive(true);
accountDao.save(account);
accountDao.deactivateByUserId(user.getId()); // Query: UPDATE Account SET isActive = false WHERE userId = userId
Account account2 = accountDao.findById(account.getId());
assertThat(account2.isActive(), is(false));
}
}
如果我使用@Transactional
(account2.isActive()
仍为true
)注释课程,则第二个失败。两者之间的主要区别在于我正在更新我保存的实体。
这是否意味着我无法对此类实体运行更新?为什么?这是没有意义的,因为我可以插入实体然后获取它们。
我认为这可能是由于事务范围(它是DAO级别),但是第一次测试也会失败,因为在我尝试保存{{{I}时,保存的User
将无法使用1}}。