NoSuchElementException due to DataSets(openmrs-module-reporting)

I am trying to write test case for generateReportingReportDefinition method in DHIS2ReportingServiceImpl.java.

I have written a test case for this method. Here is the exact line which causes the error.(Currently I have commented the line).

When i try to run this test it fails with NoSuchElementException. This exception is caused by this line.

@maurya advised to execute the dataset as mentioned here.

executeDataSet(XML_DATASET_PATH + new TestUtil().getTestDatasetFilename(XML_REPORT_TEST_DATASET));

I have tried it but it there error persists.

How can I get the Datasets which are required here?

Can you share the entire build log or stack trace?

@dkayiwa Following is the error log which I get on mvn clean install

@pralay do you have a pull request i can use to reproduce this locally?

@dkayiwa Please uncomment the line number 171,172,173. here is the PR:

@pralay the problem seems to come from the logic where you iterate the datasets. It assumes that they will always have data which is not always true. That is why you get a NoSuchElementException on trying to call iterator.next() when iterator.hasNext would have returned false.

You can confirm it by replacing that section of code with:

Iterator<Entry<String, DataSet>> iterator = results.getDataSets().entrySet().iterator(); while (iterator.hasNext()) { dataSet = iterator.next().getValue(); if (dataSet.iterator().hasNext()) { dsr = dataSet.iterator().next(); dsrlist = new ArrayList( dsr.getColumnValues().values() ); break; } }

1 Like

Thanks @dkayiwa for looking into it and giving the solution.

Yes, This will solve the testing issue partially but refines the API. I will have to check the method when there are Datasets and when there are no Datasets.

The above mentioned solution answers to the later part. How can I test the method when there is a dataset , i.e. What should be get done to those Datasets?

@pralay i do not know how the business logic of that method is supposed to be. I was simply saying that iterator.next() will throw an exception if iterator.hasNext returns false. :slight_smile:

If that method expects a dataset and it does not exist, then you should get out of it by throwing an exception. Then your unit test can test the behavior of having to throw an exception when an expected dataset does not exist.

1 Like