当我进行测试时,为什么我的实体没有更新?

时间:2016-04-15 11:53:48

标签: java spring hibernate testing transactional

鉴于此课程有两个测试:

@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));
    }

}

如果我使用@Transactionalaccount2.isActive()仍为true)注释课程,则第二个失败。两者之间的主要区别在于我正在更新我保存的实体。

这是否意味着我无法对此类实体运行更新?为什么?这是没有意义的,因为我可以插入实体然后获取它们。

我认为这可能是由于事务范围(它是DAO级别),但是第一次测试也会失败,因为在我尝试保存{{{I}时,保存的User将无法使用1}}。

0 个答案:

没有答案