0

I want to implement a Java method to test data:

public List<DashboardDTO> testDate() {

        String sourceDate = "2012-02-29";
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date myDate;

        List<DashboardDTO> list = new ArrayList<>();

        try {
            myDate = format.parse(sourceDate);

            for (int i = 0; i <= 10; i++) {

                DashboardDTO obj = new DashboardDTO();
                obj.setAmount(400);
                obj.setDate(myDate);
                obj.setNumber_of_transactions(33);
                list.add(obj);
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return list;
    }

How I can generate test Date in the loop? I want to specify a range of random Dates.

Peter Penzov
  • 1,126
  • 134
  • 430
  • 808
  • Just to clarify, you want a method to produce random dates? – Haris Bouchlis Feb 01 '19 at 08:18
  • Yes. I want to update my method. – Peter Penzov Feb 01 '19 at 08:19
  • why would you want to test with random data? if it's random, you have no control over what the outcome should be, which is what you really need for tests. – Stultuske Feb 01 '19 at 08:19
  • 1
    check out the answers of this question https://stackoverflow.com/questions/3985392/generate-random-date-of-birth – Haris Bouchlis Feb 01 '19 at 08:19
  • Here is a very nice post on generating random Dates in java : https://www.logicbig.com/how-to/code-snippets/jcode-java-random-random-dates.html – nullPointer Feb 01 '19 at 08:24
  • @Stultuske Probably not the first thing for a newbie to learn, but there is always *property based* testing versus *example based* testing. Using random values is fine, as long as you know what you are doing (and you are keeping your seeds around, to enable yourself to repro a failure). Seriously: the people coming from Haskell using quick check, they look at java unit tests and our approach of putting down 5 examples manually, sigh, and quickly walk away from such amateurism. – GhostCat Feb 01 '19 at 08:26
  • @GhostCat Can you show me complete working example please? – Peter Penzov Feb 01 '19 at 08:45
  • I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `LocalDate` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Feb 01 '19 at 08:47
  • @OleV.V. Can you show me complete working example please? – Peter Penzov Feb 01 '19 at 08:48
  • [Complete example of generating a random date using `LocalDate` here](https://stackoverflow.com/a/42617990/5772882) (it’s one of the answers to the linked original question). – Ole V.V. Feb 01 '19 at 08:51
  • You were just given a link to another question that explains how you can approach your problem. Please understand that this here isn't coding school were people spend their free time acting as tutor and working with you through your education. In other words: carefully read that duplicated question. Then, start trying yourself. Write code that follows the guidance. And then, if that really doesnt work out, consider editing this question to include that code you tried ... and drop a comment so that we re-open the question. Anything else is NOT how this community works. – GhostCat Feb 01 '19 at 08:52

0 Answers0