JUnit test for tess4J application





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







0















I want to test my method to see if it will read the file correctly. I just can't seem to wrap my head around JUnit Testing. Can someone show me how to correctly write a JUnit test for this code:



import java.io.File;
import net.sourceforge.tess4j.*;
import net.sourceforge.tess4j.util.LoadLibs;

public class ImageTest {

public static String imageService(String filePath) {
File imageFile = new File("tessImage.png");
ITesseract instance = new Tesseract();

//Let tessdata be extracted in case you dont have tessdata folder
File tessDataFolder = LoadLibs.extractTessResources("tessdata");
//Set the tessdata path
instance.setDatapath(tessDataFolder.getAbsolutePath());

instance.setLanguage("eng");
try {
String result = instance.doOCR(imageFile);
return result;
} catch (TesseractException e) {
System.err.println(e.getMessage());
return "this is an error" ;
}
}
}









share|improve this question































    0















    I want to test my method to see if it will read the file correctly. I just can't seem to wrap my head around JUnit Testing. Can someone show me how to correctly write a JUnit test for this code:



    import java.io.File;
    import net.sourceforge.tess4j.*;
    import net.sourceforge.tess4j.util.LoadLibs;

    public class ImageTest {

    public static String imageService(String filePath) {
    File imageFile = new File("tessImage.png");
    ITesseract instance = new Tesseract();

    //Let tessdata be extracted in case you dont have tessdata folder
    File tessDataFolder = LoadLibs.extractTessResources("tessdata");
    //Set the tessdata path
    instance.setDatapath(tessDataFolder.getAbsolutePath());

    instance.setLanguage("eng");
    try {
    String result = instance.doOCR(imageFile);
    return result;
    } catch (TesseractException e) {
    System.err.println(e.getMessage());
    return "this is an error" ;
    }
    }
    }









    share|improve this question



























      0












      0








      0








      I want to test my method to see if it will read the file correctly. I just can't seem to wrap my head around JUnit Testing. Can someone show me how to correctly write a JUnit test for this code:



      import java.io.File;
      import net.sourceforge.tess4j.*;
      import net.sourceforge.tess4j.util.LoadLibs;

      public class ImageTest {

      public static String imageService(String filePath) {
      File imageFile = new File("tessImage.png");
      ITesseract instance = new Tesseract();

      //Let tessdata be extracted in case you dont have tessdata folder
      File tessDataFolder = LoadLibs.extractTessResources("tessdata");
      //Set the tessdata path
      instance.setDatapath(tessDataFolder.getAbsolutePath());

      instance.setLanguage("eng");
      try {
      String result = instance.doOCR(imageFile);
      return result;
      } catch (TesseractException e) {
      System.err.println(e.getMessage());
      return "this is an error" ;
      }
      }
      }









      share|improve this question
















      I want to test my method to see if it will read the file correctly. I just can't seem to wrap my head around JUnit Testing. Can someone show me how to correctly write a JUnit test for this code:



      import java.io.File;
      import net.sourceforge.tess4j.*;
      import net.sourceforge.tess4j.util.LoadLibs;

      public class ImageTest {

      public static String imageService(String filePath) {
      File imageFile = new File("tessImage.png");
      ITesseract instance = new Tesseract();

      //Let tessdata be extracted in case you dont have tessdata folder
      File tessDataFolder = LoadLibs.extractTessResources("tessdata");
      //Set the tessdata path
      instance.setDatapath(tessDataFolder.getAbsolutePath());

      instance.setLanguage("eng");
      try {
      String result = instance.doOCR(imageFile);
      return result;
      } catch (TesseractException e) {
      System.err.println(e.getMessage());
      return "this is an error" ;
      }
      }
      }






      java junit






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 '18 at 8:30









      quant

      1,60711527




      1,60711527










      asked Nov 22 '18 at 7:41









      KBearKBear

      12




      12
























          1 Answer
          1






          active

          oldest

          votes


















          1














          Forword: your exception handling is horrific. Don't return an error message when your caller is expecting the OCR string. Stick to the JAVA style. In case of an error - throw an EXCEPTION!



          Next: you never actually use the "filePath" parameter. This is clearly a bug.



          You first need to ask yourself WHAT to test. Is it the "imageService" method you want to test? Then create a second class and from there, you would test your method. Within this test class, you would take an example file, call your imageService and compare the result with what you would expect. Those kind of comparisons are done with Assert-statements. Please check the jUnit docs for more detail.






          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%2f53426032%2fjunit-test-for-tess4j-application%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














            Forword: your exception handling is horrific. Don't return an error message when your caller is expecting the OCR string. Stick to the JAVA style. In case of an error - throw an EXCEPTION!



            Next: you never actually use the "filePath" parameter. This is clearly a bug.



            You first need to ask yourself WHAT to test. Is it the "imageService" method you want to test? Then create a second class and from there, you would test your method. Within this test class, you would take an example file, call your imageService and compare the result with what you would expect. Those kind of comparisons are done with Assert-statements. Please check the jUnit docs for more detail.






            share|improve this answer




























              1














              Forword: your exception handling is horrific. Don't return an error message when your caller is expecting the OCR string. Stick to the JAVA style. In case of an error - throw an EXCEPTION!



              Next: you never actually use the "filePath" parameter. This is clearly a bug.



              You first need to ask yourself WHAT to test. Is it the "imageService" method you want to test? Then create a second class and from there, you would test your method. Within this test class, you would take an example file, call your imageService and compare the result with what you would expect. Those kind of comparisons are done with Assert-statements. Please check the jUnit docs for more detail.






              share|improve this answer


























                1












                1








                1







                Forword: your exception handling is horrific. Don't return an error message when your caller is expecting the OCR string. Stick to the JAVA style. In case of an error - throw an EXCEPTION!



                Next: you never actually use the "filePath" parameter. This is clearly a bug.



                You first need to ask yourself WHAT to test. Is it the "imageService" method you want to test? Then create a second class and from there, you would test your method. Within this test class, you would take an example file, call your imageService and compare the result with what you would expect. Those kind of comparisons are done with Assert-statements. Please check the jUnit docs for more detail.






                share|improve this answer













                Forword: your exception handling is horrific. Don't return an error message when your caller is expecting the OCR string. Stick to the JAVA style. In case of an error - throw an EXCEPTION!



                Next: you never actually use the "filePath" parameter. This is clearly a bug.



                You first need to ask yourself WHAT to test. Is it the "imageService" method you want to test? Then create a second class and from there, you would test your method. Within this test class, you would take an example file, call your imageService and compare the result with what you would expect. Those kind of comparisons are done with Assert-statements. Please check the jUnit docs for more detail.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 22 '18 at 8:46









                MickMick

                512313




                512313
































                    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%2f53426032%2fjunit-test-for-tess4j-application%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

                    How to pass form data using jquery Ajax to insert data in database?

                    National Museum of Racing and Hall of Fame

                    Guess what letter conforming each word