i know what you mean ;)
The idea i have in mind is to wrap mockito behavior (that you write) right
into the classes you add into you tiny bundles.
There is only one problem, you usually don't have control over the classes
created and there is no standard way to get hold of the mock instance, so
you can verify.That can be solved.
Whats really going to be neat is how and WHERE you define the mock behavior:
when(mock.doThat()).thenReturn(foo);
and WHERE you do the verification.

This is especially tricky if you really want to do this in your own
tinybundles bundle because what you do in @Configuration happens OUTSIDE and
BEFORE the osgi framework instance. So you can just be declarative, not
passing any instances into the future.

What will be much simpler, and has been done before, to use the @Test phase
itself, where everything happens inside OSGi already. So in short:
@Test public void testMe() {
MyService service = mock(MyService.class);
// add mock behavior
ctx.registerService(MyService.class,service,null);
// execute code under test

// verify mock
}

Once we have boiled out the phases that are typical for such scenarios, i am
sure we can add "native" support for it in Exam to distinguish those phases
in their own callback methods like so:
@PrepareMocks
public void prepare(BundleContext ctx)

@VerifyMocks
//optionally, fine grained service selection:
@Service (filter="&(objectId=MyService.class)(company=foo)")
public void verify(MyService mocked)

anyway, this is syntactic sugar that we should align with a more user
friendly service interaction (like not requiring you to interact with Bundle
Context directly but just inject services into the places you need them to
be.

Having the entire behavior in tiny bundles is not much harder than just
writing the classes that do the mocking (or use dynamic proxy + mockito) but
the thing is how this can interact with you test.
For sure, if you KNOW that you are facing a service that is INFACT a mock,
you can directly use it and treat it like one.
The real thing we need to add in Tinybundles in that case is some simpler
way of adding mock replacements of classes.
I will look into this perhaps today if i get to solve another issue in
2.2.0. If not, there will be plenty of time to think about it as i will be
on a 4 week leave tomorrow.

All in all, Tinybundles codebases is really small, so its worth maybe trying
to add it yourself (?). Dynamic Proxy+Mockito are could combinations to
implement that i think.


On Wed, Jul 27, 2011 at 12:17 AM, Rodolfo Barbeiro <
[email protected]> wrote:

>  Hi Toni,
>
> These examples help.
>
> Answer me one question, you can add behavior mock-classes? (Similar to objects
> created by EasyMock.)
>
> See my test scenario:
>
> My bundle (which will be tested) depends on other bundles. I want to simulate
> these bundles using TinyBundles.(ok?)
> But these bundles need to expose OSGI services and simulate behavior. For
> when my bundle-test invoke the services, they have to respond as previously
> defined.
>
> Do you understand?
>
> Is there a way to do this?
>
>
> Regards, Rodolfo Barbeiro
>
>
> ------------------------------
> Date: Tue, 26 Jul 2011 23:47:37 +0200
> Subject: Re: Pax-Exam: How to create bundle mocks using tinybundles?
> From: [email protected]
> To: [email protected]
>
>
> You can also have a quick look at pure tiny bundles examples:
> http://team.ops4j.org/wiki/display/ops4j/Tinybundles
>
> Or at a very recent use case from Neo4J (using Exam + Tinybundles) :
>
> https://github.com/neo4j/neo4j-osgi/blob/master/examples/src/test/java/org/neo4j/examples/osgi/OSGiTest.java
>
> I am adding dedicated examples for TB in the Exam-Learn Project once 2.2.1
> is out (which will contain a crucial isolation bug fix in NativeContainer)
>
> hope this helps ?
> Toni
>
> On Tue, Jul 26, 2011 at 10:19 PM, Andreas Pieber <[email protected]>wrote:
>
> Please take a look at the pax wicket integration tests. I do it there. On
> mobile so no direct link but it's also on github.com/ops4j in the itest
> project
> Kind regards Andreas
> On Jul 26, 2011 6:58 PM, "Rodolfo Barbeiro" <[email protected]>
> wrote:
> >
> >
> >
> >
> > Hi Everyone! Does anyone have an example or can tell me how I can create
> bundles mocks using tinybundles? Thanks! Rodolfo Barbeiro.
>
> _______________________________________________
> general mailing list
> [email protected]
> http://lists.ops4j.org/mailman/listinfo/general
>
>
>
>
> --
> Toni Menzel Source <http://tonimenzel.com>
>
>
> _______________________________________________ general mailing list
> [email protected] http://lists.ops4j.org/mailman/listinfo/general
>
> _______________________________________________
> general mailing list
> [email protected]
> http://lists.ops4j.org/mailman/listinfo/general
>
>


-- 
Toni Menzel Source <http://tonimenzel.com>
_______________________________________________
general mailing list
[email protected]
http://lists.ops4j.org/mailman/listinfo/general

Reply via email to