using json unit test files in a java programHow do I test a private function or a class that has private methods, fields or inner classes?Is Java “pass-by-reference” or “pass-by-value”?How do I efficiently iterate over each entry in a Java Map?Unit Testing C CodeWhat's the best strategy for unit-testing database-driven applications?How do I read / convert an InputStream into a String in Java?When to use LinkedList over ArrayList in Java?How do I generate random integers within a specific range in Java?How do I convert a String to an int in Java?Creating a memory leak with Java

Confusion regarding control system of Mars Rover?

Is there a pattern for handling conflicting function parameters?

What is the point of impeaching Trump?

How to level a picture frame hung on a single nail?

Does the US Armed Forces refuse to recruit anyone with an IQ less than 83?

Bothered by watching coworkers slacking off

Can I cast Death Ward on additional creatures without causing previous castings to end?

What does a textbook look like while you are writing it?

Re-entering the UK after overstaying in 2008

When Vesuvan Shapeshifter copies turn face up replacement effects, why do they work?

Does the 'java' command compile Java programs?

Why aren't faces sharp in my f/1.8 portraits even though I'm carefully using center-point autofocus?

Is the "spacetime" the same thing as the mathematical 4th dimension?

Why most footers have a background color as a divider of section?

Decision Variable Value from a Set (Gurobi)

Isn't the detector always measuring, and thus always collapsing the state?

Why such a singular place for bird watching?

What is the difference between increasing volume and increasing gain?

Can anyone give me the reason why music is taught this way?

How dangerous are my worn rims?

The answer is a girl's name (my future granddaughter) - can anyone help?

Drawing Maps; flat distortion

Why has Speaker Pelosi been so hesitant to impeach President Trump?

Booting Ubuntu from USB drive on MSI motherboard -- EVERYTHING fails



using json unit test files in a java program


How do I test a private function or a class that has private methods, fields or inner classes?Is Java “pass-by-reference” or “pass-by-value”?How do I efficiently iterate over each entry in a Java Map?Unit Testing C CodeWhat's the best strategy for unit-testing database-driven applications?How do I read / convert an InputStream into a String in Java?When to use LinkedList over ArrayList in Java?How do I generate random integers within a specific range in Java?How do I convert a String to an int in Java?Creating a memory leak with Java






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;









0















I am trying to unit test some code. I have 2 fields in my input data that may or may not be in the data, so I want to test all 4 scenarios (ab, a not b, b not a, neither a nor b). I created 4 different json files to use as mocked input, covering each scenario. I could have created one file with neither field, and then just added the required field into the mock data for each of the 4 tests. That approach to me, seems like the test has too much control over the test data. I prefer the 4 files approach, but one of my coworkers is complaining about 4 files being almost exactly the same.



I know this is asking for opinion, but which seems like a better approach? Have all 4 files, or have each test manipulate the data before the assertion?










share|improve this question


























  • It kinda depends. If the files are super big, then just one. If they are smaller, then it depends on if it takes longer to change a value, or add a value. 4 files will take longer to load though.

    – JustAFellowCoder
    Mar 28 at 20:58












  • My vote is no files -- If this were me I'd want the test to just inject the json directly, so I don't have to keep the contents of the files in sync with my test over time. I might consider using files if the data was really large. If your class under test requires you provide a file, it's probably doing too much anyway, and should be split up.

    – Gus
    Mar 28 at 21:01











  • Also consider cost of maintenance. If those two fields turn into four and you need to test all combinations, you have to supply 16 files.

    – Not a JD
    Mar 28 at 21:09











  • my 4 files are fairly small (60 lines each)

    – Mark
    Mar 28 at 21:37











  • 60 lines, not much of a problem either way won't make a difference. Completely arbitrary. Just different ways you'll have to code it.

    – JustAFellowCoder
    Mar 29 at 0:43

















0















I am trying to unit test some code. I have 2 fields in my input data that may or may not be in the data, so I want to test all 4 scenarios (ab, a not b, b not a, neither a nor b). I created 4 different json files to use as mocked input, covering each scenario. I could have created one file with neither field, and then just added the required field into the mock data for each of the 4 tests. That approach to me, seems like the test has too much control over the test data. I prefer the 4 files approach, but one of my coworkers is complaining about 4 files being almost exactly the same.



I know this is asking for opinion, but which seems like a better approach? Have all 4 files, or have each test manipulate the data before the assertion?










share|improve this question


























  • It kinda depends. If the files are super big, then just one. If they are smaller, then it depends on if it takes longer to change a value, or add a value. 4 files will take longer to load though.

    – JustAFellowCoder
    Mar 28 at 20:58












  • My vote is no files -- If this were me I'd want the test to just inject the json directly, so I don't have to keep the contents of the files in sync with my test over time. I might consider using files if the data was really large. If your class under test requires you provide a file, it's probably doing too much anyway, and should be split up.

    – Gus
    Mar 28 at 21:01











  • Also consider cost of maintenance. If those two fields turn into four and you need to test all combinations, you have to supply 16 files.

    – Not a JD
    Mar 28 at 21:09











  • my 4 files are fairly small (60 lines each)

    – Mark
    Mar 28 at 21:37











  • 60 lines, not much of a problem either way won't make a difference. Completely arbitrary. Just different ways you'll have to code it.

    – JustAFellowCoder
    Mar 29 at 0:43













0












0








0








I am trying to unit test some code. I have 2 fields in my input data that may or may not be in the data, so I want to test all 4 scenarios (ab, a not b, b not a, neither a nor b). I created 4 different json files to use as mocked input, covering each scenario. I could have created one file with neither field, and then just added the required field into the mock data for each of the 4 tests. That approach to me, seems like the test has too much control over the test data. I prefer the 4 files approach, but one of my coworkers is complaining about 4 files being almost exactly the same.



I know this is asking for opinion, but which seems like a better approach? Have all 4 files, or have each test manipulate the data before the assertion?










share|improve this question
















I am trying to unit test some code. I have 2 fields in my input data that may or may not be in the data, so I want to test all 4 scenarios (ab, a not b, b not a, neither a nor b). I created 4 different json files to use as mocked input, covering each scenario. I could have created one file with neither field, and then just added the required field into the mock data for each of the 4 tests. That approach to me, seems like the test has too much control over the test data. I prefer the 4 files approach, but one of my coworkers is complaining about 4 files being almost exactly the same.



I know this is asking for opinion, but which seems like a better approach? Have all 4 files, or have each test manipulate the data before the assertion?







java unit-testing






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 29 at 8:43









Krishnan Mahadevan

9,9183 gold badges19 silver badges47 bronze badges




9,9183 gold badges19 silver badges47 bronze badges










asked Mar 28 at 20:53









MarkMark

8061 gold badge12 silver badges41 bronze badges




8061 gold badge12 silver badges41 bronze badges















  • It kinda depends. If the files are super big, then just one. If they are smaller, then it depends on if it takes longer to change a value, or add a value. 4 files will take longer to load though.

    – JustAFellowCoder
    Mar 28 at 20:58












  • My vote is no files -- If this were me I'd want the test to just inject the json directly, so I don't have to keep the contents of the files in sync with my test over time. I might consider using files if the data was really large. If your class under test requires you provide a file, it's probably doing too much anyway, and should be split up.

    – Gus
    Mar 28 at 21:01











  • Also consider cost of maintenance. If those two fields turn into four and you need to test all combinations, you have to supply 16 files.

    – Not a JD
    Mar 28 at 21:09











  • my 4 files are fairly small (60 lines each)

    – Mark
    Mar 28 at 21:37











  • 60 lines, not much of a problem either way won't make a difference. Completely arbitrary. Just different ways you'll have to code it.

    – JustAFellowCoder
    Mar 29 at 0:43

















  • It kinda depends. If the files are super big, then just one. If they are smaller, then it depends on if it takes longer to change a value, or add a value. 4 files will take longer to load though.

    – JustAFellowCoder
    Mar 28 at 20:58












  • My vote is no files -- If this were me I'd want the test to just inject the json directly, so I don't have to keep the contents of the files in sync with my test over time. I might consider using files if the data was really large. If your class under test requires you provide a file, it's probably doing too much anyway, and should be split up.

    – Gus
    Mar 28 at 21:01











  • Also consider cost of maintenance. If those two fields turn into four and you need to test all combinations, you have to supply 16 files.

    – Not a JD
    Mar 28 at 21:09











  • my 4 files are fairly small (60 lines each)

    – Mark
    Mar 28 at 21:37











  • 60 lines, not much of a problem either way won't make a difference. Completely arbitrary. Just different ways you'll have to code it.

    – JustAFellowCoder
    Mar 29 at 0:43
















It kinda depends. If the files are super big, then just one. If they are smaller, then it depends on if it takes longer to change a value, or add a value. 4 files will take longer to load though.

– JustAFellowCoder
Mar 28 at 20:58






It kinda depends. If the files are super big, then just one. If they are smaller, then it depends on if it takes longer to change a value, or add a value. 4 files will take longer to load though.

– JustAFellowCoder
Mar 28 at 20:58














My vote is no files -- If this were me I'd want the test to just inject the json directly, so I don't have to keep the contents of the files in sync with my test over time. I might consider using files if the data was really large. If your class under test requires you provide a file, it's probably doing too much anyway, and should be split up.

– Gus
Mar 28 at 21:01





My vote is no files -- If this were me I'd want the test to just inject the json directly, so I don't have to keep the contents of the files in sync with my test over time. I might consider using files if the data was really large. If your class under test requires you provide a file, it's probably doing too much anyway, and should be split up.

– Gus
Mar 28 at 21:01













Also consider cost of maintenance. If those two fields turn into four and you need to test all combinations, you have to supply 16 files.

– Not a JD
Mar 28 at 21:09





Also consider cost of maintenance. If those two fields turn into four and you need to test all combinations, you have to supply 16 files.

– Not a JD
Mar 28 at 21:09













my 4 files are fairly small (60 lines each)

– Mark
Mar 28 at 21:37





my 4 files are fairly small (60 lines each)

– Mark
Mar 28 at 21:37













60 lines, not much of a problem either way won't make a difference. Completely arbitrary. Just different ways you'll have to code it.

– JustAFellowCoder
Mar 29 at 0:43





60 lines, not much of a problem either way won't make a difference. Completely arbitrary. Just different ways you'll have to code it.

– JustAFellowCoder
Mar 29 at 0:43












1 Answer
1






active

oldest

votes


















1
















Like for me, your coworker is right.



Create two files: a.json-part and b.json-part. Let's a.json-part contains



"a": .....


and b.json-part contains similar content for b (NB: not a JSON, just a part!)



So, for your tests you can build test data by simple concatenation of parts like



String ab_json = "" + readAsText("a.json-part") + "," + readAsText("b.json-part") + "";
String a_not_b_json = "" + readAsText("a.json-part") + "";


and so on.



In this case you can combine your fields as you want and can be sure you didn't miss any file in case if some field is changed.






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/4.0/"u003ecc by-sa 4.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%2f55406692%2fusing-json-unit-test-files-in-a-java-program%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









    1
















    Like for me, your coworker is right.



    Create two files: a.json-part and b.json-part. Let's a.json-part contains



    "a": .....


    and b.json-part contains similar content for b (NB: not a JSON, just a part!)



    So, for your tests you can build test data by simple concatenation of parts like



    String ab_json = "" + readAsText("a.json-part") + "," + readAsText("b.json-part") + "";
    String a_not_b_json = "" + readAsText("a.json-part") + "";


    and so on.



    In this case you can combine your fields as you want and can be sure you didn't miss any file in case if some field is changed.






    share|improve this answer





























      1
















      Like for me, your coworker is right.



      Create two files: a.json-part and b.json-part. Let's a.json-part contains



      "a": .....


      and b.json-part contains similar content for b (NB: not a JSON, just a part!)



      So, for your tests you can build test data by simple concatenation of parts like



      String ab_json = "" + readAsText("a.json-part") + "," + readAsText("b.json-part") + "";
      String a_not_b_json = "" + readAsText("a.json-part") + "";


      and so on.



      In this case you can combine your fields as you want and can be sure you didn't miss any file in case if some field is changed.






      share|improve this answer



























        1














        1










        1









        Like for me, your coworker is right.



        Create two files: a.json-part and b.json-part. Let's a.json-part contains



        "a": .....


        and b.json-part contains similar content for b (NB: not a JSON, just a part!)



        So, for your tests you can build test data by simple concatenation of parts like



        String ab_json = "" + readAsText("a.json-part") + "," + readAsText("b.json-part") + "";
        String a_not_b_json = "" + readAsText("a.json-part") + "";


        and so on.



        In this case you can combine your fields as you want and can be sure you didn't miss any file in case if some field is changed.






        share|improve this answer













        Like for me, your coworker is right.



        Create two files: a.json-part and b.json-part. Let's a.json-part contains



        "a": .....


        and b.json-part contains similar content for b (NB: not a JSON, just a part!)



        So, for your tests you can build test data by simple concatenation of parts like



        String ab_json = "" + readAsText("a.json-part") + "," + readAsText("b.json-part") + "";
        String a_not_b_json = "" + readAsText("a.json-part") + "";


        and so on.



        In this case you can combine your fields as you want and can be sure you didn't miss any file in case if some field is changed.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 28 at 23:07









        Bor LazeBor Laze

        2,1868 silver badges18 bronze badges




        2,1868 silver badges18 bronze badges

































            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%2f55406692%2fusing-json-unit-test-files-in-a-java-program%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







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현