AttachmentsService ahead of REST API work (ATT-24)

Tags: #<Tag:0x00007f0f1a1e34c8> #<Tag:0x00007f0f1a1e33b0> #<Tag:0x00007f0f1a1e32c0> #<Tag:0x00007f0f1a1e31d0>

For the sake of the test class you can just let Spring autowire private members, as suggested in my Gist above.

1 Like

@mksd Thanks for resolving the doubts .

@mksd I pushed the code changes to the PR, but I keep getting a NullPointerException. I guess the PatientService is not instantiated. Travis Build Details

On which exact line do you get the NPE? This will tell you which object is null.

@mksd PatientService(ps)

Line 35 in AttachmentsServiceTest.java

Please point us to a permalink on GitHub. Always try to pinpoint others to the most precise and accurate information.

I’m looking at the latest version of that file on your repo and it’s got 32 lines…

Looking at the Gist that I provided you with would point me here:

Encounter encounter = es.getEncountersByPatient(patient).get(0);

It could be es or es.getEncountersByPatient(patient) that is null. I let you investigate and find your way to the next step (that’s assuming you are on a version of the file that corresponds to the Gist.)

@mksd Here is the link to the PR. Yes the file corresponds to your Gist. Sorry for the convenience caused, I was referring to the PR.

Ok for all the Spring wiring to happen, you need your test class to extend one of the base context sensitive test classes provided by OpenMRS Core. For now you can do this:

public class AttachmentsServiceTest extends BaseModuleWebContextSensitiveTest { {
...

P.S. This brought my attention on something that was misconfigured in another existing test, see my last commit here. This means that you will have to rebase your working branch:

git pull --rebase upstream master

Where I assume that ‘upstream’ is the OpenMRS repo, of course you should use the appropriate local naming that you have used on your machine.

@mksd Ok I will follow these steps to get started. I debugged the code with breakpoints, which showed me that all following objects are assigned with null.

AttachmentsService as;

PatientService ps;

VisitService vs;

EncounterService es;

Thank you very much.

@mksd I followed your steps and ran the test case. It resulted in the following error.

org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘org.openmrs.module.attachments.AttachmentsServiceTest’: Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.openmrs.api.PatientService

org.openmrs.module.attachments.AttachmentsServiceTest.ps; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [org.openmrs.api.PatientService] is defined: expected single matching bean but found 2: [patientServiceTarget, patientService]

So based on the error received, I renamed the ps to patientService. Then no such error was found. But no patient is returned by the service, only null is returned. Here is the PR

Spring actually finds the bean, there’s even two defined: "patientServiceTarget" and "patientService".

Try this:

@Autowired
@Qualifier("patientService")
protected PatientService ps;

And do the same for other beans that would raise similar errors.

@madushan you should rebase again on master. The latest commit will now allow to do the usual

public class AttachmentsServiceTest extends BaseModuleContextSensitiveTest { {
...

Note how it now should extend BaseModuleContextSensitiveTest (and not anymore BaseModuleWebContextSensitiveTest.)

Sorry about the confusion, if any.

@mksd Thank you very much for the support. I followed your steps except changing BaseModuleContextSensitiveTest. It seems to work fine.

But when I changed the name to BaseModuleContextSensitiveTest from BaseModuleWebContextSensitiveTest I got the following error.

https://pastebin.com/KHHymcbh

I just pulled your branch ATT-24-1 and did that one change and it worked fine.

Somehow the error that you pasted is related to the situation before your merge with master, could you try again?

@mksd I tried it right now, but keep getting the same error :frowning:

How do you do this, do you actually do a

mvn clean install

?

@mksd I didn’t do a mvn clean install. I just ran the test class

@mksd I did a mvn clean install, then there were no errors. The test case passed. But when running single Test class in IntelJ, the same error ocured. Thank you very much for the support as always.

Remember that the bottom line is the success of actual Maven builds, such as those made by Travis CI (this is the last one for example.)
This is why you should always run a clean install with Maven before submitting anything (such as PRs, code reviews… etc).

It can definitely happen that your IDE gets out of sync with what succeeded on the command line with Maven. In Eclipse there is a ‘Maven Update’ button, surely there’s something equivalent with IntelliJ.

1 Like

@mksd @zouchine Thank you very much for the clarification. The test passes now and I pushed the changes.

I then began working on the second part of the issue of modifying the controller to accept the client request. I tried to write a test case with all the search parameters, but it fails with IllegalRequestException. How should I proceed to direct my request to the particular doSearch method in the controller?