0

We have a business requirement where we query set of assets through a particular path and moving the expired assets from one folder to archive folder, how can we write JUnit test case for below code,

@Reference
CommonConfigService commonConfigService ;
ResourceResolver resourceResolver ;
@Reference
QueryBuilder querybuilder;

private void queryForAssets()
{
    Session session = resourceResolver .adaptTo(Session .class);
    Map<String, String> map = new HashMap<String,String>();
    map.put("path", "myPath");
    map.put("type" ,"dam:Asset");
    map.put("property" ,"prism:ExpirationDate");
    final Query query = queryBuilder.createQuery(PredicateGroup.create(map),session);
    final SearchResults resultSet = query.getResult();
    for(Hit hit: resultSet.getHits())
    {
        //business logic,...iterating through each node path and reading the expiry date properties//
        moveAssetToDestination();
    }
}

In the above code , the last function moveAssetToDestination() will move all the expired assets from one folder to archive folder. So basically there are no methods which are exactly returning any value. Is it possible to write any JUnit for above code?

1 Answer 1

1

Yes, to mock void methods, you typically can use Mockito's verify statements to verify that expected actions were invoked. Take a look at this article for some examples: https://www.baeldung.com/mockito-void-methods

So if you look at what moveAssetToDestination() does, it likely calls methods on some sort of object. You can construct a test where that object on which the methods are called is a mock, and then you can use verify statements and argument catchers to validate that the method was invoked with expected parameters.

Or in the queryForAssets method you would make queryBuilder be a mock object using the @Mock annotation and @RunWith(MockitoJUnitRunner.class). See http://www.wemblog.com/2016/12/how-to-write-tests-in-aem.html for an example of that. Then you can set things up so that when its createQuery method is called it returns a mock Query object. Then you could validate with a verify statement to verify that the getResult method is invoked on the mock object when you execute the code. Similar things could be done for whatever objects are acted upon within the moveAssetToDestination method.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.