Unit Test for method that convert string to type date





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I am pretty new to coding and unit tests.
I am writing a unit test for a method converts string to date.



What do I assert for a positive test



it goes like below



String s1 = "11/11/2018";
Date returnedValue = Class.ConvertToDate(s1);
assert(????);










share|improve this question


















  • 1





    Possible duplicate of New to unit testing, how to write great tests?

    – Alessandro Da Rugna
    Nov 23 '18 at 23:29











  • All tests should be based on expected result. Create expected data first, then assert that returned value is same as expected.

    – Fabio
    Nov 23 '18 at 23:49


















0















I am pretty new to coding and unit tests.
I am writing a unit test for a method converts string to date.



What do I assert for a positive test



it goes like below



String s1 = "11/11/2018";
Date returnedValue = Class.ConvertToDate(s1);
assert(????);










share|improve this question


















  • 1





    Possible duplicate of New to unit testing, how to write great tests?

    – Alessandro Da Rugna
    Nov 23 '18 at 23:29











  • All tests should be based on expected result. Create expected data first, then assert that returned value is same as expected.

    – Fabio
    Nov 23 '18 at 23:49














0












0








0








I am pretty new to coding and unit tests.
I am writing a unit test for a method converts string to date.



What do I assert for a positive test



it goes like below



String s1 = "11/11/2018";
Date returnedValue = Class.ConvertToDate(s1);
assert(????);










share|improve this question














I am pretty new to coding and unit tests.
I am writing a unit test for a method converts string to date.



What do I assert for a positive test



it goes like below



String s1 = "11/11/2018";
Date returnedValue = Class.ConvertToDate(s1);
assert(????);







unit-testing






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 21:24









SursSurs

11




11








  • 1





    Possible duplicate of New to unit testing, how to write great tests?

    – Alessandro Da Rugna
    Nov 23 '18 at 23:29











  • All tests should be based on expected result. Create expected data first, then assert that returned value is same as expected.

    – Fabio
    Nov 23 '18 at 23:49














  • 1





    Possible duplicate of New to unit testing, how to write great tests?

    – Alessandro Da Rugna
    Nov 23 '18 at 23:29











  • All tests should be based on expected result. Create expected data first, then assert that returned value is same as expected.

    – Fabio
    Nov 23 '18 at 23:49








1




1





Possible duplicate of New to unit testing, how to write great tests?

– Alessandro Da Rugna
Nov 23 '18 at 23:29





Possible duplicate of New to unit testing, how to write great tests?

– Alessandro Da Rugna
Nov 23 '18 at 23:29













All tests should be based on expected result. Create expected data first, then assert that returned value is same as expected.

– Fabio
Nov 23 '18 at 23:49





All tests should be based on expected result. Create expected data first, then assert that returned value is same as expected.

– Fabio
Nov 23 '18 at 23:49












1 Answer
1






active

oldest

votes


















0














I am not sure what methods the Date class in your code snippet provides, so I will assume that it has the following methods: getDayNrInMonth which returns the day as an int value starting from 1, getMonthNrInYear which returns the month as an int starting from 1, and getYearNr which returns the year as an int value (lets just ignore that there are different calendarian systems, and that is all for the Gregorian calendar).



The important question you would have to answer first is, what the goal of the test would be. There are several possible goals that you could test for - which means that you probably should end up with writing several tests. Let's assume you want to test that the year part was converted correctly. Then, the assertion could look like follows:



assert(returnedValue.getYearNr() == 2018);


Most likely, however, your test framework will provide something better for you here, possibly a function assertEquals, which you could then use as follows:



assertEquals(returnedValue.getYearNr(), 2018);


If in contrast you want to check that the month is converted correctly, you will immediately realize that the test example you have chosen is not ideal:



assertEquals(returnedValue.getMonthNrInYear(), 11);


As in your example day and month both are 11, this test would not be very reliable. So to test the correct conversion of the day and the month, the input strings should be chosen to have distinguishable values for the two.



While you move on with learning to program and to test, you will find that there are many more aspects that could be considered. The links in the comments can help you. However, hopefully the above points support you in taking the next steps.






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53453078%2funit-test-for-method-that-convert-string-to-type-date%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I am not sure what methods the Date class in your code snippet provides, so I will assume that it has the following methods: getDayNrInMonth which returns the day as an int value starting from 1, getMonthNrInYear which returns the month as an int starting from 1, and getYearNr which returns the year as an int value (lets just ignore that there are different calendarian systems, and that is all for the Gregorian calendar).



    The important question you would have to answer first is, what the goal of the test would be. There are several possible goals that you could test for - which means that you probably should end up with writing several tests. Let's assume you want to test that the year part was converted correctly. Then, the assertion could look like follows:



    assert(returnedValue.getYearNr() == 2018);


    Most likely, however, your test framework will provide something better for you here, possibly a function assertEquals, which you could then use as follows:



    assertEquals(returnedValue.getYearNr(), 2018);


    If in contrast you want to check that the month is converted correctly, you will immediately realize that the test example you have chosen is not ideal:



    assertEquals(returnedValue.getMonthNrInYear(), 11);


    As in your example day and month both are 11, this test would not be very reliable. So to test the correct conversion of the day and the month, the input strings should be chosen to have distinguishable values for the two.



    While you move on with learning to program and to test, you will find that there are many more aspects that could be considered. The links in the comments can help you. However, hopefully the above points support you in taking the next steps.






    share|improve this answer




























      0














      I am not sure what methods the Date class in your code snippet provides, so I will assume that it has the following methods: getDayNrInMonth which returns the day as an int value starting from 1, getMonthNrInYear which returns the month as an int starting from 1, and getYearNr which returns the year as an int value (lets just ignore that there are different calendarian systems, and that is all for the Gregorian calendar).



      The important question you would have to answer first is, what the goal of the test would be. There are several possible goals that you could test for - which means that you probably should end up with writing several tests. Let's assume you want to test that the year part was converted correctly. Then, the assertion could look like follows:



      assert(returnedValue.getYearNr() == 2018);


      Most likely, however, your test framework will provide something better for you here, possibly a function assertEquals, which you could then use as follows:



      assertEquals(returnedValue.getYearNr(), 2018);


      If in contrast you want to check that the month is converted correctly, you will immediately realize that the test example you have chosen is not ideal:



      assertEquals(returnedValue.getMonthNrInYear(), 11);


      As in your example day and month both are 11, this test would not be very reliable. So to test the correct conversion of the day and the month, the input strings should be chosen to have distinguishable values for the two.



      While you move on with learning to program and to test, you will find that there are many more aspects that could be considered. The links in the comments can help you. However, hopefully the above points support you in taking the next steps.






      share|improve this answer


























        0












        0








        0







        I am not sure what methods the Date class in your code snippet provides, so I will assume that it has the following methods: getDayNrInMonth which returns the day as an int value starting from 1, getMonthNrInYear which returns the month as an int starting from 1, and getYearNr which returns the year as an int value (lets just ignore that there are different calendarian systems, and that is all for the Gregorian calendar).



        The important question you would have to answer first is, what the goal of the test would be. There are several possible goals that you could test for - which means that you probably should end up with writing several tests. Let's assume you want to test that the year part was converted correctly. Then, the assertion could look like follows:



        assert(returnedValue.getYearNr() == 2018);


        Most likely, however, your test framework will provide something better for you here, possibly a function assertEquals, which you could then use as follows:



        assertEquals(returnedValue.getYearNr(), 2018);


        If in contrast you want to check that the month is converted correctly, you will immediately realize that the test example you have chosen is not ideal:



        assertEquals(returnedValue.getMonthNrInYear(), 11);


        As in your example day and month both are 11, this test would not be very reliable. So to test the correct conversion of the day and the month, the input strings should be chosen to have distinguishable values for the two.



        While you move on with learning to program and to test, you will find that there are many more aspects that could be considered. The links in the comments can help you. However, hopefully the above points support you in taking the next steps.






        share|improve this answer













        I am not sure what methods the Date class in your code snippet provides, so I will assume that it has the following methods: getDayNrInMonth which returns the day as an int value starting from 1, getMonthNrInYear which returns the month as an int starting from 1, and getYearNr which returns the year as an int value (lets just ignore that there are different calendarian systems, and that is all for the Gregorian calendar).



        The important question you would have to answer first is, what the goal of the test would be. There are several possible goals that you could test for - which means that you probably should end up with writing several tests. Let's assume you want to test that the year part was converted correctly. Then, the assertion could look like follows:



        assert(returnedValue.getYearNr() == 2018);


        Most likely, however, your test framework will provide something better for you here, possibly a function assertEquals, which you could then use as follows:



        assertEquals(returnedValue.getYearNr(), 2018);


        If in contrast you want to check that the month is converted correctly, you will immediately realize that the test example you have chosen is not ideal:



        assertEquals(returnedValue.getMonthNrInYear(), 11);


        As in your example day and month both are 11, this test would not be very reliable. So to test the correct conversion of the day and the month, the input strings should be chosen to have distinguishable values for the two.



        While you move on with learning to program and to test, you will find that there are many more aspects that could be considered. The links in the comments can help you. However, hopefully the above points support you in taking the next steps.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 29 '18 at 10:25









        Dirk HerrmannDirk Herrmann

        2,283523




        2,283523
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53453078%2funit-test-for-method-that-convert-string-to-type-date%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Hercules Kyvelos

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud