You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@felix.apache.org by Konrad Windszus <ko...@gmx.de> on 2017/01/30 12:37:27 UTC

Best practice how to generate instances of Component Property types in unit tests

When you use the Component Property Types being introduced with OSGi DS 1.4, you need to define those as annotation classes. 

For unit tests it is very common, that you need to dynamically generate several instances of those annotations (when you want to test calling the activate() method with different configurations).
Are there any good code examples or recommended libraries which ease to do that?

I found several responses in http://stackoverflow.com/questions/16299717/how-to-create-an-instance-of-an-annotation but none of them seem like the perfect match.
If you could share how you did that in other projects that would be great,
Konrad


Re: Best practice how to generate instances of Component Property types in unit tests

Posted by Neil Bartlett <nj...@gmail.com>.
When the Converter specification is released (part of R7) you will be able to use that quite easily, in fact that will be the recommended way. For example to convert a map type:

	MyAnnotationType config = converter.convert(map).to(MyAnnotationType.class);

A converter implementation is available as a snapshot from the Felix project.

Regards,
Neil


> On 30 Jan 2017, at 12:37, Konrad Windszus <ko...@gmx.de> wrote:
> 
> When you use the Component Property Types being introduced with OSGi DS 1.4, you need to define those as annotation classes. 
> 
> For unit tests it is very common, that you need to dynamically generate several instances of those annotations (when you want to test calling the activate() method with different configurations).
> Are there any good code examples or recommended libraries which ease to do that?
> 
> I found several responses in http://stackoverflow.com/questions/16299717/how-to-create-an-instance-of-an-annotation but none of them seem like the perfect match.
> If you could share how you did that in other projects that would be great,
> Konrad
> 


Re: Best practice how to generate instances of Component Property types in unit tests

Posted by David Bosschaert <da...@gmail.com>.
Hi Konrad,

Not really in a unit test but the Converter implements a similar mechanism
where it just uses a Java proxy to implement them [1]. Note the use of
method.getDefaultValue() to get the default value out of the annotation.

Cheers,

David

[1]
https://svn.apache.org/viewvc/felix/trunk/converter/converter/src/main/java/org/apache/felix/converter/impl/ConvertingImpl.java?revision=1779874&view=markup#l444

On 30 January 2017 at 12:37, Konrad Windszus <ko...@gmx.de> wrote:

> When you use the Component Property Types being introduced with OSGi DS
> 1.4, you need to define those as annotation classes.
>
> For unit tests it is very common, that you need to dynamically generate
> several instances of those annotations (when you want to test calling the
> activate() method with different configurations).
> Are there any good code examples or recommended libraries which ease to do
> that?
>
> I found several responses in http://stackoverflow.com/
> questions/16299717/how-to-create-an-instance-of-an-annotation but none of
> them seem like the perfect match.
> If you could share how you did that in other projects that would be great,
> Konrad
>
>

RE: Best practice how to generate instances of Component Property types in unit tests

Posted by Stefan Seifert <ss...@pro-vision.de>.
if you are using osgi-mock 2.x you can just pass in a map or a list of key/values and they are mapped automatically to an annotation instance and injected within the unit test.

stefan

[1] https://sling.apache.org/documentation/development/osgi-mock.html


>-----Original Message-----
>From: Konrad Windszus [mailto:konrad_w@gmx.de]
>Sent: Monday, January 30, 2017 1:37 PM
>To: dev@felix.apache.org
>Subject: Best practice how to generate instances of Component Property
>types in unit tests
>
>When you use the Component Property Types being introduced with OSGi DS
>1.4, you need to define those as annotation classes.
>
>For unit tests it is very common, that you need to dynamically generate
>several instances of those annotations (when you want to test calling the
>activate() method with different configurations).
>Are there any good code examples or recommended libraries which ease to do
>that?
>
>I found several responses in
>http://stackoverflow.com/questions/16299717/how-to-create-an-instance-of-
>an-annotation but none of them seem like the perfect match.
>If you could share how you did that in other projects that would be great,
>Konrad
>



Re: Best practice how to generate instances of Component Property types in unit tests

Posted by Carsten Ziegeler <cz...@apache.org>.
Konrad Windszus wrote
> When you use the Component Property Types being introduced with OSGi DS 1.4, you need to define those as annotation classes. 
> 
> For unit tests it is very common, that you need to dynamically generate several instances of those annotations (when you want to test calling the activate() method with different configurations).
> Are there any good code examples or recommended libraries which ease to do that?
> 
> I found several responses in http://stackoverflow.com/questions/16299717/how-to-create-an-instance-of-an-annotation but none of them seem like the perfect match.
> If you could share how you did that in other projects that would be great,

Mockito works perfectly with mocking the annotation

 Carsten

-- 
Carsten Ziegeler
Adobe Research Switzerland
cziegeler@apache.org