e 13/10/2019 à 14:08, Mathieu Lirzin a écrit :
Le 08/10/2019 à 13:12, Jacques Le Roux a écrit :
Hi Mathieu

I forgot to mention that I put this method in GroovyUtil.java.

It was hastily done because doing so I rapidly thought that "Anyway it would not add 
much, just a cover function"

It works with a complete/correct version:

     public static Map testGroovy(Delegator delegator, LocalDispatcher dispatcher, 
Map<String, Object> serviceCtx,
             String serviceName) throws GenericEntityException, 
GenericServiceException {
         GenericValue userLogin = EntityQuery.use(delegator)
                 .from("UserLogin")
                 .where("userLoginId", "system")
                 .cache()
                 .queryOne();
         serviceCtx.put("userLogin", userLogin);
         return dispatcher.runSync(serviceName, serviceCtx);
     }

I still wonder if it of much use, ie compare:

     void testSendOrderChangeNotification() {
         Map serviceCtx = [
             orderId: 'TEST_DEMO10090',
             sendTo: 'test_em...@example.com',
         ]
         Map serviceResult = GroovyUtil.testGroovy(delegator, dispatcher, 
serviceCtx, 'sendOrderChangeNotification');
         assert ServiceUtil.isSuccess(serviceResult)
         assert serviceResult.emailType.equals("PRDS_ODR_CHANGE")
     }

with

     void testSendOrderChangeNotification() {
         Map serviceCtx = [
             orderId: 'TEST_DEMO10090',
             sendTo: 'test_em...@example.com',
             userLogin: 
EntityQuery.use(delegator).from('UserLogin').where('userLoginId', 
'system').cache().queryOne()
         ]
         Map serviceResult = dispatcher.runSync('sendOrderChangeNotification', 
serviceCtx)
         assert ServiceUtil.isSuccess(serviceResult)
         assert serviceResult.emailType.equals("PRDS_ODR_CHANGE")
     }

It's shorter and can be used for most Groovy tests. But you have to
pass the delegator, dispatcher and serviceCtx which are else
transparent. Not sure it's of better use.

What do you think?
I agree that that having to pass the delegator, dispatcher and
serviceCtx is not ideal and tend make the test less clear.

In order to avoid repetitive code a nice first helper method would be
for example one for retrieving the default userLogin like what is done
in ‘QuoteTests.groovy’

     // Retrieves a particular login record.
     GenericValue getUserLogin(String userLoginId) {
         GenericValue userLogin = EntityQuery.use(delegator)
                 .from('UserLogin').where(userLoginId: userLoginId).queryOne()
         assert userLogin
         return userLogin
     }

We could even add a default login user.

     // Retrieves the default login record.
     GenericValue getUserLogin() {
         return getUserLogin('system');
     }

I guess we should add such method directly in the ‘OFBizTestCase’ class
to be able to reuse it in all test cases and avoid having to pass the
‘delegator’ and ‘dispatcher’ as method arguments.

The creation of the service input map of your example would look like
this:

      void testSendOrderChangeNotification() {
          Map serviceCtx = [
              orderId: 'TEST_DEMO10090',
              sendTo: 'test_em...@example.com',
              userLogin: getUserLogin()
          ]
          Map serviceResult = dispatcher.runSync('sendOrderChangeNotification', 
serviceCtx)
          assert ServiceUtil.isSuccess(serviceResult)
          assert serviceResult.emailType.equals("PRDS_ODR_CHANGE")
      }

In any case I think that finding the generic and reusable helper methods
can be done incrementally.

What do you think?

Thanks Mathieu,

That sounds like a good idea to me. I have created OFBIZ-11247 for that

Jacques

Reply via email to