In class TeacherDao method create() calls AddressDao.create():
public void create(Teacher teacher) {
addressDao.create(teacher.getAddress());
// Teacher is being written to database...
}
I am writing a Junit5 test for TeacherDao class, so AddressDao is replaced with a mock. I call TeacherDao.create() and verify that AddressDao.create() is being interacted:
@Mock
private AddressDao addressDao;
@InjectMocks
@Autowired
private TeacherDao teacherDao;
@Test
void teacherDaoCreateTest() {
teacherDao.create(teacher);
verify(addressDao).create(testAddress);
}
Verification is successful. After that I add @Transactional annotation to TeacherDao.create() method, and test fails. AddressDao mock now has zero interactions, and testAddress is being actually written to the database. I don't totally understand how @Transactional works in-depth and must be missing something important. Why @Transactional rewrites mocks?