You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@felix.apache.org by Georg Henzler <gh...@apache.org> on 2020/04/08 09:40:07 UTC

Better Mockito support for OSGi Metatype Service Config Annotations

Hi all,

if you are interested to make @Mock work better with Metatype config
annotations, feel free to join (and support) the discussion in [1].

Best Regards
Georg

[1] https://github.com/mockito/mockito/issues/1900

Re: Better Mockito support for OSGi Metatype Service Config Annotations

Posted by Georg Henzler <gh...@apache.org>.
Thinking about it again: Also verifications can make sense in order
to check if a config attribute was (or was not) requested by a
certain piece of code - e.g. sometimes the config object is stored
as is as state, then you might check if a method doThis() calls
config.configForThis()

On 2020-04-08 12:04, Georg Henzler wrote:
> True, that's another option but there is no auto-complete for 
> 'myproperty'
> and the code is more verbose than just using
> 
> @Mock
> MyAnnotation annotation
> 
> ...
> 
> when(annotation.myproperty()).thenReturn("myvalue")
> // while all the other default values are automatically returned
> // (that's the part that is not happening today)
> 
> 
> So strictly speaking this is probably not a Mock (because you would
> not verify calls against it), but from a usage point of view it's
> totally natural to write
> 
> @Mock
> MyConfig config
> @Mock
> BundleContext bundleContext
> 
> and then use those with a testActivate() method.
> 
> -Georg
> 
> 
> On 2020-04-08 11:52, Konrad Windszus wrote:
>> Why not using the converter instead? Do you really want to verify
>> interactions with the annotations?
>> 
>> Usually I use
>> 
>> Converter c = Converters.standardConverter();
>> Map<String, String> map = new HashMap<>();
>> map.put("myproperty", "myvalue");
>> MyAnnotation annotation = c.convert(map).to(MyAnnotation.class);
>> 
>> Konrad
>> 
>> 
>>> On 8. Apr 2020, at 11:40, Georg Henzler <gh...@apache.org> wrote:
>>> 
>>> Hi all,
>>> 
>>> if you are interested to make @Mock work better with Metatype config
>>> annotations, feel free to join (and support) the discussion in [1].
>>> 
>>> Best Regards
>>> Georg
>>> 
>>> [1] https://github.com/mockito/mockito/issues/1900

Re: Better Mockito support for OSGi Metatype Service Config Annotations

Posted by Georg Henzler <fe...@ghenzler.de>.
True, that's another option but there is no auto-complete for 
'myproperty'
and the code is more verbose than just using

@Mock
MyAnnotation annotation

...

when(annotation.myproperty()).thenReturn("myvalue")
// while all the other default values are automatically returned
// (that's the part that is not happening today)


So strictly speaking this is probably not a Mock (because you would
not verify calls against it), but from a usage point of view it's
totally natural to write

@Mock
MyConfig config
@Mock
BundleContext bundleContext

and then use those with a testActivate() method.

-Georg


On 2020-04-08 11:52, Konrad Windszus wrote:
> Why not using the converter instead? Do you really want to verify
> interactions with the annotations?
> 
> Usually I use
> 
> Converter c = Converters.standardConverter();
> Map<String, String> map = new HashMap<>();
> map.put("myproperty", "myvalue");
> MyAnnotation annotation = c.convert(map).to(MyAnnotation.class);
> 
> Konrad
> 
> 
>> On 8. Apr 2020, at 11:40, Georg Henzler <gh...@apache.org> wrote:
>> 
>> Hi all,
>> 
>> if you are interested to make @Mock work better with Metatype config
>> annotations, feel free to join (and support) the discussion in [1].
>> 
>> Best Regards
>> Georg
>> 
>> [1] https://github.com/mockito/mockito/issues/1900

Re: Better Mockito support for OSGi Metatype Service Config Annotations

Posted by Konrad Windszus <ko...@gmx.de>.
Why not using the converter instead? Do you really want to verify interactions with the annotations?

Usually I use

Converter c = Converters.standardConverter();
Map<String, String> map = new HashMap<>();
map.put("myproperty", "myvalue");
MyAnnotation annotation = c.convert(map).to(MyAnnotation.class);

Konrad


> On 8. Apr 2020, at 11:40, Georg Henzler <gh...@apache.org> wrote:
> 
> Hi all,
> 
> if you are interested to make @Mock work better with Metatype config
> annotations, feel free to join (and support) the discussion in [1].
> 
> Best Regards
> Georg
> 
> [1] https://github.com/mockito/mockito/issues/1900