Hi guys,
I am currently trying to implement a AOP Before Advice (https://wiki.openmrs.org/display/docs/OpenMRS+AOP).
The final goal is to ensure that a visit cannot be closed if there is no diagnosis entered. But for now, this advice is just a dumb class that throws a APIException
any time the VisitService.endVisit(visit)
method is called.
public class BeforeEndVisitAdvice implements MethodBeforeAdvice{
protected static final Log log = LogFactory.getLog(BeforeEndVisitAdvice.class);
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
if ( method.getName().equals("endVisit") && (target instanceof VisitService) ) {
log.error("Dumb 'before advice' that just returns an APIException");
throw new APIException();
}
return;
}
}
This works just fine at runtime: the APIException
is thrown.
Now the test looks like this for now:
public class BeforeEndVisitAdviceTest extends BaseModuleContextSensitiveTest {
protected final Log log = LogFactory.getLog(BeforeEndVisitAdvice.class);
@Autowired
private VisitService visitService;
@Autowired
private PatientService patientService;
@Autowired
private LocationService locationService;
@Autowired
private AdtService adtService;
@Before
public void before() {
}
@Test(expected=APIException.class)
@Verifies(value = "should fail ending a visit that has no diagnosis", method = "before(Method, Object[], Object)")
public void shouldFailEndVisitWithNoDiagnosis() {
VisitDomainWrapper activeVisitWrapper = adtService.getActiveVisit(patientService.getPatient(2), locationService.getLocation(1));
List<Diagnosis> diags = activeVisitWrapper.getUniqueDiagnoses(true, true);
visitService.endVisit(activeVisitWrapper.getVisit(), new Date());
}
}
But when running the tests (from Eclipse or Maven) no APIException
is returned.
The BeforeEndVisitAdvice.before(...)
method is never called.
I have had a look at AuthorizationAdviceTest.java and did not see anything particular there that I may have forgotten. Looks like it should just work out of the box.
Do I need to initialize/start/configure the advice somewhere when running tests? What did I miss?