Suppose I have this:
@Transactional(rollbackFor = NotificationException.class)
public interface PersonManagerService {
public void addPerson(Person person);
}
and an implementation:
public class PersonManagerServiceImpl implements PersonManagerService {
public OtherService otherService;
public void addPerson(Person person) {
// stuff
}
// getter and setter for otherService
}
How would I go about mocking the otherService dependency while still having the addPerson method hit the database?
My scenario is that I want to test that a particular exception causes a rollback of the saving of the person that is being added. This exception would come from the OtherService class, which I don't want to call the real version of. I am currently using Spring Transaction annotations so I have a test like this:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {various locations})
public class Test() {
@Autowired
PersonManagerService service;
@Test
public void test() {
// how to call setOtherService so it can be a mock?
}
If I try to cast the auto wired bean, I get an IllegalArgumentException because it is a proxy. If I made the Impl not use an interface, I could use CGLIB but I don't want to do that. If I create the impl programmatically, then it's not tied into the transaction flow. What other options do I have?