AttachmentsService ahead of REST API work (ATT-24)

@mksd I have been trying to write a test case for ATT-24. Screenshot from 2018-03-22 15-18-19

at runtime it produces the following error

What I need to be doing is to search for existing attachments, how can I overcome this? Feel free to help.

How do you instantiate the service?

I guess this is because you haven’t given it a Spring bean configuration. See an example here from another module.

1 Like

@mksd Do you mean that I have to include PatientService bean in moduleApplicationContext.xml ?

Ah no sorry I thought you couldn’t instantiate AttachmentService (but you will have to do what I pointed you to for AttachmentService though.)

Can you pastebin the whole stack trace of your error? I can’t do much with those screenshots…

@mksd Here is the stack trace https://pastebin.com/AMxEhaU2

@mksd Here is the test case https://pastebin.com/CHgjrBQp

Can you push this as an intermediary commit on your fork/branch? I’ll clone that quickly.

@mksd ok I will post the link here.

@mksd Here is the link to my branch.

I guess the issue is a minor one, but I find hard to figure it out. Your support so far is highly appreciated.

Just autowire all Spring beans that will let you, this could be your next iteration.

My Gist will currently fail to autowire AttachmentsService, to solve this see my remark about defining the Spring bean above on this thread.

1 Like

@mksd ok I will follow these steps. Thank you very much for helping me out.

Also note how one would do this:

@Autowired
private AttachmentsService as;

And not this:

@Autowired
private AttachmentsServiceImpl as;

The point is that Spring can provide multiple implementations of the same API, and you want to consume APIs in your code, not specific implementations.

1 Like

@mksd So, should I include PatientService, VisitService along with AttachmentService in the TestingApplicationContext.xml or AttachmentService only ?

In moduleApplicationContext.xml (not TestingApplicationContext.xml).

Only the new bean that you intend to Spring-define, the only new bean is AttachmentServiceImpl for the API AttachmentService. All other services’ implementations are already defined and provided by OpenMRS Core.

1 Like

@mksd Is this all right ?

See above on this thread:

You need to reproduce this kind of configuration in Attachments for the new AttachmentsService.

By the way pay attention to the fact that the name will be plural it will be AttachmentsService (not AttachmentService)… etc.

1 Like

@mksd Thanks for the example, It is a bit complex, there is a lot to be modified. I am using @autowired to AttachmentsService, but what I really need is AttachmentServiceImpl. So what I should be adding is a bean of AttachmentServiceImpl , Right ?

You add a bean definition for AttachmentsServiceImpl so that you can let Spring autowire into AttachmentsService's API.

You should read more about Spring and dependency injection. There are millions of tutorial and examples online. This for example, I just Googled for 20 secs.

1 Like

Thank you very much for the motivation. I will read them and resume work. Thank you very much

@mksd When using @autowired, normally a setter is used such as following.

@Autowired

public void setWriter(IWriter writer) {
    this.writer = writer;
}

But in our case we declared the dependency with out assignment

@Autowired

IWriter writer

Is a setter not essential?