Hi Ranganath,
On 28 April 2014 18:26, <chittari.va...@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(<org.apache.isis.applib.query.QueryDefault@148cc8c>); > returns a default value > parameter 0 did not match: > <org.apache.isis.applib.query.QueryDefault@148cc8c>, because was > <org.apache.isis.applib.query.QueryDefault@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 >