Uploaded image for project: '[Read Only] - CRISP'
  1. [Read Only] - CRISP
  2. CRISP-77

Simple mock support to override ResourceServiceBroker in unit tests by returning json file data

    XMLWordPrintable

Details

    Description

      It's hard to implement a unit test for code dependent on crisp broker service.
      It would be nice if mock classes are supported to easily override the crisp resource service broker by returning data from a local json file.

      Here's an example in unit test code:

             final URL productJsonFileUrl = getClass().getResource("product-output.json");
      
             // mocking resourceResolver to override #findResources() only
             // and return a json output directly from a classpath resource.
             final Map resourceResolverMap = new HashMap<>();
             resourceResolverMap.put("productResources", new MockJacksonResourceResolverAdapter() {
                 @Override
                 public Resource findResources(String baseAbsPath, Map pathVariables, ExchangeHint exchangeHint)
                         throws ResourceException {
                     return urlToResource(productJsonFileUrl);
                 }
             });
             // you can add more resourceResolver(s)...
      
             // mocking resourceServiceBroker.
             ResourceServiceBroker mockBroker = new MockResourceServiceBroker(resourceResolverMap);
             // register the mocking resourceServiceBroker.
             MockCrispHstServices.setDefaultResourceServiceBroker(mockBroker);
      
             // From now on, CrispHstServices.getDefaultResourceServiceBroker() will return the resourceServiceBroker
             // which was mocked above...
             ResourceServiceBroker broker = CrispHstServices.getDefaultResourceServiceBroker(); // broker == mockBroker
             // ...
      

      Attachments

        Activity

          People

            Unassigned Unassigned
            wko Woonsan Ko (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: