You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@jclouds.apache.org by Xianyi Ye <ye...@sina.com> on 2014/12/01 07:04:31 UTC

How to mock up novaApi object?

Hi all,

 

I have a question about how to do JClouds mock test.

My target codes are showing as below.

 

ComputeServiceContext context = ContextBuilder.newBuilder(provider)

                .endpoint(endpoint.get())

                .credentials(identity.get(),
password.get()).modules(modules)

                .buildView(ComputeServiceContext.class);

RestContext<NovaApi, NovaAsyncApi> nova = context.unwrap();

compute = context.getComputeService();

novaApi = nova.getApi();

 

 

And I knew that I can mock ComputeService with following codes.

 

ComputeServiceContext context =
ContextBuilder.newBuilder("stub").buildView(ComputeServiceContext.class) ;

compute = context.getComputeService();

 

But how can I mock novaApi? 

 

When I tried to call following codes, it always said to me
"java.lang.ClassCastException: org.jclouds.internal.ContextImpl cannot be
cast to org.jclouds.rest.RestContext"

RestContext<NovaApi, NovaAsyncApi> nova = context.unwrap();

 

Is there anyone aware of any solution?

 

Thanks in advance,

Xianyi Ye


Re: How to mock up novaApi object?

Posted by Ignasi Barrera <ig...@gmail.com>.
The "stub" provider is not intended to be used with the "underlying"
provider specific api, as it does not have one.

The "unwrap" method provides access to the "provider specific api". In
OpenStack based clouds it provides the Nova api, in EC2 based clouds,
it provides the EC2 api, etc. Note that you can't get an EC2 api out
from a context created for an OpenStack cloud.

For this reason, you just can't get a provider specific API from the
"stub" provider, as it doesn't have one. It is just meant to be used
as an abstraction, but nothing more.

If you have to mock the Nova API you'll have to mock it manually with
EasyMock, Mockito or any other mocking framework.



On 1 December 2014 at 07:04, Xianyi Ye <ye...@sina.com> wrote:
> Hi all,
>
>
>
> I have a question about how to do JClouds mock test.
>
> My target codes are showing as below.
>
>
>
> ComputeServiceContext context = ContextBuilder.newBuilder(provider)
>
>                 .endpoint(endpoint.get())
>
>                 .credentials(identity.get(),
> password.get()).modules(modules)
>
>                 .buildView(ComputeServiceContext.class);
>
> RestContext<NovaApi, NovaAsyncApi> nova = context.unwrap();
>
> compute = context.getComputeService();
>
> novaApi = nova.getApi();
>
>
>
>
>
> And I knew that I can mock ComputeService with following codes.
>
>
>
> ComputeServiceContext context =
> ContextBuilder.newBuilder("stub").buildView(ComputeServiceContext.class) ;
>
> compute = context.getComputeService();
>
>
>
> But how can I mock novaApi?
>
>
>
> When I tried to call following codes, it always said to me
> “java.lang.ClassCastException: org.jclouds.internal.ContextImpl cannot be
> cast to org.jclouds.rest.RestContext”
>
> RestContext<NovaApi, NovaAsyncApi> nova = context.unwrap();
>
>
>
> Is there anyone aware of any solution?
>
>
>
> Thanks in advance,
>
> Xianyi Ye