-1

I am creating a dataset in yml format to initialize a table in a postgresql database

- id : 5
    description : "test5"
    is_deleted : false
    last_redaction_date : 2021-12-13 15:09:52.715570
    persist_date : 2022-03-02 15:09:52.715570
    title : "test5"
    user_id : 15

How can I set the current date in the required format to the persist_date variable so as not to manually enter the date each time?

@AutoConfigureMockMvc
@SpringBootTest(classes = JmApplication.class)
public class TestQuestionResourceController extends AbstractClassForDRRiderMockMVCTests {

    @Autowired
    private MockMvc mvc;

      @Test
      @DataSet(cleanBefore = true,
                value = {
                        "dataset/testQuestionResourceController/question_different_date.yml",
                   
                },
                strategy = SeedStrategy.CLEAN_INSERT
        )
        public void getQuestionSortedByWeightForTheWeek() throws Exception {
David Conrad
  • 15,432
  • 2
  • 42
  • 54
  • Which part are you stuck on, how to get the current date and time, how to format it, or how to update the yaml? – David Conrad Mar 14 '22 at 16:33
  • how to get the current date and time, how to format it – adil_screamyy Mar 14 '22 at 16:35
  • It seems this is more to do with modifying a [tag:dbunit] `@DataSet` on the fly than to do with formatting dates or writing yaml, although formatting dates may be a sub-task. See [Is there any way I can specify the current system time for my testcase?](https://www.dbunit.org/faq.html#systemTimeInTest) and maybe [how to customizely replace dbunit's dataset Object](https://stackoverflow.com/questions/30100467/how-to-customizely-replace-dbunits-dataset-object). I'm not really familiar with dbunit but perhaps others are. – David Conrad Mar 15 '22 at 17:03

1 Answers1

0

You can get the current date and time from ZonedDateTime:

ZonedDateTime now = ZonedDateTime.now();

And then you can format the time using DateTimeFormatter's ISO_LOCAL_DATE and ISO_LOCAL_TIME:

String currentDateTime = DateTimeFormatter.ISO_LOCAL_DATE.format(now) +
    DateTimeFormatter.ISO_LOCAL_TIME.format(now);

Which produces "2022-03-14 12:40:32.8071372".

David Conrad
  • 15,432
  • 2
  • 42
  • 54