You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@isis.apache.org by ch...@wipro.com on 2014/04/28 19:26:03 UTC

DomainObjectContainer in Junits

Hi ,

Is it possible in Junits to lookup the DomainObjectContainer instance to set it in my isis service to query, persist and merge entities.

Actually my Usecase is that I have a Isis service whose method(upload) calls a component which unmarshall input excel file and performs bulk insert and bulk update into the database. For persisting this component requires DomainObjectContainer. When this application is run in jetty server, DomainObjectContainer instance is obtained by injecting it in my service class. But in Junits how can I obtain it.

I tried with @Mock annotation for DomainObjectContainer and JunitRuleMockery2, but it seems that it requires some expectations before calling service upload. So it failed asking for expectations to be set on DomainObjectContainer as it is used in querying, persisting.
Error is :

java.lang.AssertionError: unexpected invocation: mockContainer.firstMatch(<or...@789144>)
no expectations specified: did you...
- forget to start an expectation with a cardinality clause?
- call a mocked method to specify the parameter of an expectation?
what happened before this: nothing!

Then I set the expectation on mockContainer for the method firstMatch:
oneOf(mockContainer).firstMatch(new QueryDefault<MOrganisation>(
                            MOrganisation.class, "fetch_orgnaisation_by_id", "orgId", consumptionProfile.getSiteId()));

I got error:
java.lang.AssertionError: unexpected invocation: mockContainer.firstMatch(<or...@a8c488>)
expectations:
  expected once, never invoked: mockContainer.firstMatch(<or...@148cc8c>); returns a default value
      parameter 0 did not match: <or...@148cc8c>, because was <or...@a8c488>
what happened before this: nothing!
.. at org.apache.isis.applib.DomainObjectContainer$$EnhancerByCGLIB$$34b7a821.firstMatch(<generated>)
      at com.wipro.wess.upload.excel.dto.ExcelSheet.lookupOrganisation(ExcelSheet.java:273)
      at com.wipro.wess.upload.excel.dto.ExcelConsumptionProfile.process(ExcelConsumptionProfile.java:128)

Error says it's never invoked. In fact its invoked at ExcelSheet.lookupOrganisation(ExcelSheet.java:273)

I really don't understand this JunitMockery framework. I did not find the documentation part regarding this.

So I wanted this to do without mockery framework. In this case I need DomainObjectContainer instance.

Please help.



BR
Ranganath Varma


The information contained in this electronic message and any attachments to this message are intended for the exclusive use of the addressee(s) and may contain proprietary, confidential or privileged information. If you are not the intended recipient, you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately and destroy all copies of this message and any attachments. 

WARNING: Computer viruses can be transmitted via email. The recipient should check this email and any attachments for the presence of viruses. The company accepts no liability for any damage caused by any virus transmitted by this email.

www.wipro.com

Re: DomainObjectContainer in Junits

Posted by GESCONSULTOR <o....@gesconsultor.com>.
Hi Ranganath.

Seems that you need to define integration tests.

You can find documentation abou Isis support on [1].
If you prefer, BDD style tests are also supported. see [2].

Also there are examples on the Isis QuickStart project [3].

[1] http://isis.apache.org/core/integtestsupport.html

[2] https://isis.apache.org/core/specsupport-and-integtestsupport.html

[3] https://github.com/apache/isis/tree/master/example/application/quickstart_wicket_restful_jdo/integtests/src/test/java/integration/tests

Don't hesitate to contact again if needed.

HTH,

Oscar

> El 28/04/2014, a las 19:26, <ch...@wipro.com> escribió:
> 
> Ranganath

Re: DomainObjectContainer in Junits

Posted by Dan Haywood <da...@haywood-associates.co.uk>.
Hi Ranganath,



On 28 April 2014 18:26, <ch...@wipro.com> wrote:

> Hi ,
>
> Is it possible in Junits to lookup the DomainObjectContainer instance to
> set it in my isis service to query, persist and merge entities.
>
>
No.  If you want a "real" DomainObjectContainer, then (as Oscar suggests)
write an integration test rather than a unit test.




> Actually my Usecase is that I have a Isis service whose method(upload)
> calls a component which unmarshall input excel file and performs bulk
> insert and bulk update into the database. For persisting this component
> requires DomainObjectContainer. When this application is run in jetty
> server, DomainObjectContainer instance is obtained by injecting it in my
> service class. But in Junits how can I obtain it.
>
> I tried with @Mock annotation for DomainObjectContainer and
> JunitRuleMockery2, but it seems that it requires some expectations before
> calling service upload. So it failed asking for expectations to be set on
> DomainObjectContainer as it is used in querying, persisting.
> Error is :
>
> java.lang.AssertionError: unexpected invocation:
> mockContainer.firstMatch(<org.apache.isis.applib.query.QueryDefault@789144
> >)
> no expectations specified: did you...
> - forget to start an expectation with a cardinality clause?
> - call a mocked method to specify the parameter of an expectation?
> what happened before this: nothing!
>
> Then I set the expectation on mockContainer for the method firstMatch:
> oneOf(mockContainer).firstMatch(new QueryDefault<MOrganisation>(
>                             MOrganisation.class,
> "fetch_orgnaisation_by_id", "orgId", consumptionProfile.getSiteId()));
>
> I got error:
> java.lang.AssertionError: unexpected invocation:
> mockContainer.firstMatch(<org.apache.isis.applib.query.QueryDefault@a8c488
> >)
> expectations:
>   expected once, never invoked:
> mockContainer.firstMatch(<or...@148cc8c>);
> returns a default value
>       parameter 0 did not match:
> <or...@148cc8c>, because was
> <or...@a8c488>
> what happened before this: nothing!
> .. at
> org.apache.isis.applib.DomainObjectContainer$$EnhancerByCGLIB$$34b7a821.firstMatch(<generated>)
>       at
> com.wipro.wess.upload.excel.dto.ExcelSheet.lookupOrganisation(ExcelSheet.java:273)
>       at
> com.wipro.wess.upload.excel.dto.ExcelConsumptionProfile.process(ExcelConsumptionProfile.java:128)
>
> Error says it's never invoked. In fact its invoked at
> ExcelSheet.lookupOrganisation(ExcelSheet.java:273)
>
>
That's not quite what the error message says.  It says that it's not
invoked with the particular QueryDefault parameter you specified
("parameter 0 did not match").  You should use a Hamcrest matcher in your
expectation:

oneOf(mockContainer).firstMatch(with(queryDefaultMatching(MOrganisation.class,
"fetch_orgnaisation_by_id", "orgId", consumptionProfile.getSiteId()));

where "queryDefaultMatching(..)" returns an instance of
Matcher<QueryDefault>.

That said, this awkwardness is because the ExcelSheet is probably doing too
much. I'd change your design by introducing an OrganisationRepository, and
inject that into ExcelSheet.  Then you can write a much simpler test that
mocks out OrganisationRepository rather than DomainObjectContainer.  Then
write a separate unit (or perhaps better: integration) test for
OrganisationRepository.




> I really don't understand this JunitMockery framework. I did not find the
> documentation part regarding this.
>
>
The "JunitMockery" framework is just a few helper classes around the JMock
library [1].  The difficulties you are having are with JMock expectations;
so check out the JMock documentation, which is pretty good.  It's a
worthwhile investment learning how to mock.

HTH
Dan


[1] http://jmock.org/





> So I wanted this to do without mockery framework. In this case I need
> DomainObjectContainer instance.
>
> Please help.
>
>
>
> BR
> Ranganath Varma
>
>
> The information contained in this electronic message and any attachments
> to this message are intended for the exclusive use of the addressee(s) and
> may contain proprietary, confidential or privileged information. If you are
> not the intended recipient, you should not disseminate, distribute or copy
> this e-mail. Please notify the sender immediately and destroy all copies of
> this message and any attachments.
>
> WARNING: Computer viruses can be transmitted via email. The recipient
> should check this email and any attachments for the presence of viruses.
> The company accepts no liability for any damage caused by any virus
> transmitted by this email.
>
> www.wipro.com
>