-1

I am looking for a solution for testing these methods especially for date util classes. I am adding my two methods that I want to write a test for. How can I call the method at the end and how to doReturn the XMLGregorianCalendar when it has nested returns inside like the method I added?

public static XMLGregorianCalendar getCalendar() throws DatatypeConfigurationException {

    DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    XMLGregorianCalendar gdateFormatted =
            DatatypeFactory.newInstance().newXMLGregorianCalendar(format.format(DateUtils.currentDate()));
    return gdateFormatted;
}

public static String getDateNowYyMm() throws DatatypeConfigurationException {
    DateFormat format = new SimpleDateFormat("yyMM");
    XMLGregorianCalendar gdateFormatted =
            DatatypeFactory.newInstance().newXMLGregorianCalendar(format.format(DateUtils.currentDate()));
    return gdateFormatted.toString();
}

My problem has been resolved, my test method is below:

    public void getCalendarTest() throws DatatypeConfigurationException {
        XMLGregorianCalendar gdateFormatted = DateUtils.getCalendar();
        Assert.assertNotNull(gdateFormatted);
    }

I couldn't succeed the part of XMLGregorianCalendar.

xprop
  • 1
  • 2
  • What does "how can I call the method at the and and how to doReturn the XMLGregorianCalendar when it has nested returns inside" mean? And I don't see any `void` methods! – tgdavies Mar 27 '23 at 11:37
  • @tgdavies I edited the syntax spells – xprop Mar 27 '23 at 11:39
  • 1
    Sorry, I still don't understand your question. – tgdavies Mar 27 '23 at 11:44
  • @tgdavies I added my test, I am just stuck at XMLGregorianCalendar part – xprop Mar 27 '23 at 12:45
  • 1
    You are using terribly flawed date-time classes. These were years ago supplanted by the modern *java.time* classes defined in JSR 310. – Basil Bourque Mar 27 '23 at 15:57
  • 1
    I strongly recommend you don’t use `DateFormat` and `SimpleDateFormat` since they are notorious troublemakers. Unless you have a very specific need you may also not want to use `XMLGregorianCalendar`. Also to construct a string of `2303` for year 23 month 3 you are first constructing a `XMLGregorianCalendar` of year 2303 without a defined month. While it may work, it’s certainly confusing and ill-advised. Use classes from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/index.html) that @BasilBourque already mentioned. – Ole V.V. Mar 27 '23 at 18:11
  • The nice solution: Use java.time and its `Clock` class in your production code. For testing inject a `Clock` the time of which you control. See [How to test date created with LocalDateTime.now()](https://stackoverflow.com/questions/39527752/how-to-test-date-created-with-localdatetime-now). The hacky solution: read the time before and after calling the method under test. Test that the time returned is neither before the former time read nor after the latter one. – Ole V.V. Mar 27 '23 at 19:07
  • 1
    Why are you focused on the legacy class `XMLGregorianCalendar`? One of your methods generates text in standard ISO 8601 format for the current moment — this you can do simply by `Instant.now().toString()`. In another method, you generate text for just the year and month — for this use the `YearMonth` class to represent, well, the year and the month alone. So I see no need for *any* of your code presented here. I suggest you study [*The Java Tutorials*](https://docs.oracle.com/javase/tutorial/datetime/TOC.html) by Oracle, free-of-charge. – Basil Bourque Mar 27 '23 at 20:03
  • 1
    I still don't understand your question. `getCalendarTest` should simply call `DateUtils.getCalendar()` and check that the result is as expected (given that the date will not exactly match any other measure of 'now'). And what is the point of `privateConstructorTest`. Having said that, take the advice to use a `Clock`, and don't use static utility classes, they make testing of their clients difficult. – tgdavies Mar 27 '23 at 20:45

1 Answers1

1

Don't see how your code is related to void methods at all, but if your functions return current date either as XMLGregorianCalendar or String and you are asking how to unit test them in the condition where current date is always changing then I'd pass the DateUtils via a constructor and mock the currentDate() function to return some specific date which could easily be unit tested.

Edit:

I just realised your methods are static. Consider making them non-static and wrapping them into a class which can easily be tested. You can create a static singleton of this class in non-test code if you'd like the benefits of a global static helper.

Tarmo
  • 3,851
  • 2
  • 24
  • 41