Integration Tests fail to connect, Manual Browser Test succeeds with Java RESTful Web Service with SpringBoot/Jsoup Integration Test










0















I'm trying to run the sample code in this book: Building RESTful Web Services with Spring 5 - Second Edition. You can download the sample code for free. In chapter 7, they have some sample code that includes some integration tests and unit tests all in same directory "chapter7ticketmanagement/src/test/java/com/packtpub/restapp/ticketmanagement/". The unit tests work fine. The integration tests fail with "testUsersJsoup(com.packtpub.restapp.ticketmanagement.JsoupUserTest) Time elapsed: 1.011 sec <<< ERROR!
java.net.ConnectException: Connection refused: connect"



I experimented with changing the port from 8080 to 9090 in "ticket-managementsrcmainresourcesapplication.properties" (I also changed the hardcoded values in the integration test) and this did not help.



So "mvn test" produces the above error.
So does running them in spring tool suite.
I edited the original pom.xml and added the "maven-compiler-plugin" to specify java version 1.8. I'm running windows 10.



So the failing code is in "chapter 7ticket-managementsrctestjavacompacktpubrestappticketmanagementJsoupUserTest.java".



Here is the failing code:



public void testUsersJsoup() throws IOException 

String doc = Jsoup.connect("http://localhost:9090/user").ignoreContentType(true).get().body().text();

_log.info("test doc : " + doc);

JsonParser parser = new JsonParser();
JsonElement userElement = parser.parse(doc);
JsonArray userArray = userElement.getAsJsonArray();

_log.info("test size : " + userArray.size());

assertEquals(4, userArray.size());



HOWEVER, when I type



mvn spring-boot:run


it starts successfully and I point my chrome browser to http://localhost:9090/user (or http://localhost:8080/user if I am using the original code) I see



["userid":100,"username":"David","userid":101,"username":"Peter","userid":102,"username":"John"]


No connection error! The results look correct.



  1. We know this is not a firewall problem because it is just with localhost and it works with chrome.

  2. There is no proxy involved.

  3. I think the ports are correct: I presently have 9090 in the application.properities file, and in the source code and in the browser.

  4. "mvn clean" and then "mvn test" again did not help.









share|improve this question






















  • OK, silly me. I assumed that since I saw the spring ASCII art banner scroll by that the "mvn test" command was also starting up the web server. It is not. When I manually start the web server in another process with "mvn spring-boot:run" all the tests pass. But the new feature of spring is that it can run tomcat or jetty. So the new question is, how do I enhance the pom.xml to automatically start the default web server when running tests? Perhaps the problem is that the failing test is really an integration test and not a unit test?

    – user3477493
    Nov 17 '18 at 20:37















0















I'm trying to run the sample code in this book: Building RESTful Web Services with Spring 5 - Second Edition. You can download the sample code for free. In chapter 7, they have some sample code that includes some integration tests and unit tests all in same directory "chapter7ticketmanagement/src/test/java/com/packtpub/restapp/ticketmanagement/". The unit tests work fine. The integration tests fail with "testUsersJsoup(com.packtpub.restapp.ticketmanagement.JsoupUserTest) Time elapsed: 1.011 sec <<< ERROR!
java.net.ConnectException: Connection refused: connect"



I experimented with changing the port from 8080 to 9090 in "ticket-managementsrcmainresourcesapplication.properties" (I also changed the hardcoded values in the integration test) and this did not help.



So "mvn test" produces the above error.
So does running them in spring tool suite.
I edited the original pom.xml and added the "maven-compiler-plugin" to specify java version 1.8. I'm running windows 10.



So the failing code is in "chapter 7ticket-managementsrctestjavacompacktpubrestappticketmanagementJsoupUserTest.java".



Here is the failing code:



public void testUsersJsoup() throws IOException 

String doc = Jsoup.connect("http://localhost:9090/user").ignoreContentType(true).get().body().text();

_log.info("test doc : " + doc);

JsonParser parser = new JsonParser();
JsonElement userElement = parser.parse(doc);
JsonArray userArray = userElement.getAsJsonArray();

_log.info("test size : " + userArray.size());

assertEquals(4, userArray.size());



HOWEVER, when I type



mvn spring-boot:run


it starts successfully and I point my chrome browser to http://localhost:9090/user (or http://localhost:8080/user if I am using the original code) I see



["userid":100,"username":"David","userid":101,"username":"Peter","userid":102,"username":"John"]


No connection error! The results look correct.



  1. We know this is not a firewall problem because it is just with localhost and it works with chrome.

  2. There is no proxy involved.

  3. I think the ports are correct: I presently have 9090 in the application.properities file, and in the source code and in the browser.

  4. "mvn clean" and then "mvn test" again did not help.









share|improve this question






















  • OK, silly me. I assumed that since I saw the spring ASCII art banner scroll by that the "mvn test" command was also starting up the web server. It is not. When I manually start the web server in another process with "mvn spring-boot:run" all the tests pass. But the new feature of spring is that it can run tomcat or jetty. So the new question is, how do I enhance the pom.xml to automatically start the default web server when running tests? Perhaps the problem is that the failing test is really an integration test and not a unit test?

    – user3477493
    Nov 17 '18 at 20:37













0












0








0








I'm trying to run the sample code in this book: Building RESTful Web Services with Spring 5 - Second Edition. You can download the sample code for free. In chapter 7, they have some sample code that includes some integration tests and unit tests all in same directory "chapter7ticketmanagement/src/test/java/com/packtpub/restapp/ticketmanagement/". The unit tests work fine. The integration tests fail with "testUsersJsoup(com.packtpub.restapp.ticketmanagement.JsoupUserTest) Time elapsed: 1.011 sec <<< ERROR!
java.net.ConnectException: Connection refused: connect"



I experimented with changing the port from 8080 to 9090 in "ticket-managementsrcmainresourcesapplication.properties" (I also changed the hardcoded values in the integration test) and this did not help.



So "mvn test" produces the above error.
So does running them in spring tool suite.
I edited the original pom.xml and added the "maven-compiler-plugin" to specify java version 1.8. I'm running windows 10.



So the failing code is in "chapter 7ticket-managementsrctestjavacompacktpubrestappticketmanagementJsoupUserTest.java".



Here is the failing code:



public void testUsersJsoup() throws IOException 

String doc = Jsoup.connect("http://localhost:9090/user").ignoreContentType(true).get().body().text();

_log.info("test doc : " + doc);

JsonParser parser = new JsonParser();
JsonElement userElement = parser.parse(doc);
JsonArray userArray = userElement.getAsJsonArray();

_log.info("test size : " + userArray.size());

assertEquals(4, userArray.size());



HOWEVER, when I type



mvn spring-boot:run


it starts successfully and I point my chrome browser to http://localhost:9090/user (or http://localhost:8080/user if I am using the original code) I see



["userid":100,"username":"David","userid":101,"username":"Peter","userid":102,"username":"John"]


No connection error! The results look correct.



  1. We know this is not a firewall problem because it is just with localhost and it works with chrome.

  2. There is no proxy involved.

  3. I think the ports are correct: I presently have 9090 in the application.properities file, and in the source code and in the browser.

  4. "mvn clean" and then "mvn test" again did not help.









share|improve this question














I'm trying to run the sample code in this book: Building RESTful Web Services with Spring 5 - Second Edition. You can download the sample code for free. In chapter 7, they have some sample code that includes some integration tests and unit tests all in same directory "chapter7ticketmanagement/src/test/java/com/packtpub/restapp/ticketmanagement/". The unit tests work fine. The integration tests fail with "testUsersJsoup(com.packtpub.restapp.ticketmanagement.JsoupUserTest) Time elapsed: 1.011 sec <<< ERROR!
java.net.ConnectException: Connection refused: connect"



I experimented with changing the port from 8080 to 9090 in "ticket-managementsrcmainresourcesapplication.properties" (I also changed the hardcoded values in the integration test) and this did not help.



So "mvn test" produces the above error.
So does running them in spring tool suite.
I edited the original pom.xml and added the "maven-compiler-plugin" to specify java version 1.8. I'm running windows 10.



So the failing code is in "chapter 7ticket-managementsrctestjavacompacktpubrestappticketmanagementJsoupUserTest.java".



Here is the failing code:



public void testUsersJsoup() throws IOException 

String doc = Jsoup.connect("http://localhost:9090/user").ignoreContentType(true).get().body().text();

_log.info("test doc : " + doc);

JsonParser parser = new JsonParser();
JsonElement userElement = parser.parse(doc);
JsonArray userArray = userElement.getAsJsonArray();

_log.info("test size : " + userArray.size());

assertEquals(4, userArray.size());



HOWEVER, when I type



mvn spring-boot:run


it starts successfully and I point my chrome browser to http://localhost:9090/user (or http://localhost:8080/user if I am using the original code) I see



["userid":100,"username":"David","userid":101,"username":"Peter","userid":102,"username":"John"]


No connection error! The results look correct.



  1. We know this is not a firewall problem because it is just with localhost and it works with chrome.

  2. There is no proxy involved.

  3. I think the ports are correct: I presently have 9090 in the application.properities file, and in the source code and in the browser.

  4. "mvn clean" and then "mvn test" again did not help.






java rest spring-boot integration-testing






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 17:08









user3477493user3477493

119112




119112












  • OK, silly me. I assumed that since I saw the spring ASCII art banner scroll by that the "mvn test" command was also starting up the web server. It is not. When I manually start the web server in another process with "mvn spring-boot:run" all the tests pass. But the new feature of spring is that it can run tomcat or jetty. So the new question is, how do I enhance the pom.xml to automatically start the default web server when running tests? Perhaps the problem is that the failing test is really an integration test and not a unit test?

    – user3477493
    Nov 17 '18 at 20:37

















  • OK, silly me. I assumed that since I saw the spring ASCII art banner scroll by that the "mvn test" command was also starting up the web server. It is not. When I manually start the web server in another process with "mvn spring-boot:run" all the tests pass. But the new feature of spring is that it can run tomcat or jetty. So the new question is, how do I enhance the pom.xml to automatically start the default web server when running tests? Perhaps the problem is that the failing test is really an integration test and not a unit test?

    – user3477493
    Nov 17 '18 at 20:37
















OK, silly me. I assumed that since I saw the spring ASCII art banner scroll by that the "mvn test" command was also starting up the web server. It is not. When I manually start the web server in another process with "mvn spring-boot:run" all the tests pass. But the new feature of spring is that it can run tomcat or jetty. So the new question is, how do I enhance the pom.xml to automatically start the default web server when running tests? Perhaps the problem is that the failing test is really an integration test and not a unit test?

– user3477493
Nov 17 '18 at 20:37





OK, silly me. I assumed that since I saw the spring ASCII art banner scroll by that the "mvn test" command was also starting up the web server. It is not. When I manually start the web server in another process with "mvn spring-boot:run" all the tests pass. But the new feature of spring is that it can run tomcat or jetty. So the new question is, how do I enhance the pom.xml to automatically start the default web server when running tests? Perhaps the problem is that the failing test is really an integration test and not a unit test?

– user3477493
Nov 17 '18 at 20:37












1 Answer
1






active

oldest

votes


















0














There are annotations to start the web app as exemplified here:
Spring Live Lessons Tutorial and they work. See %JAVA_BUILDINGMICROSERVICES%livelessons-securitylivelessons-security-basicsrctestjavabasicBasicSecurityApplicationTests.java.



These annotations have been deprecated, and replaced by newer ones. The problem is that the author of this tutorial is not using theses new annotations correctly: he needs to also use @WebAppConfiguration and this requires some configuration arguments from the @SpringBootTest (which he is missing). Therefor it necessary to manually start the web server with "mvn spring-boot:run" in a separate process and then run the tests.



When I tried to use @WebAppConfiguation, I got other errors. I will document these errors in another post (some day).






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%2f53324621%2fintegration-tests-fail-to-connect-manual-browser-test-succeeds-with-java-restfu%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














    There are annotations to start the web app as exemplified here:
    Spring Live Lessons Tutorial and they work. See %JAVA_BUILDINGMICROSERVICES%livelessons-securitylivelessons-security-basicsrctestjavabasicBasicSecurityApplicationTests.java.



    These annotations have been deprecated, and replaced by newer ones. The problem is that the author of this tutorial is not using theses new annotations correctly: he needs to also use @WebAppConfiguration and this requires some configuration arguments from the @SpringBootTest (which he is missing). Therefor it necessary to manually start the web server with "mvn spring-boot:run" in a separate process and then run the tests.



    When I tried to use @WebAppConfiguation, I got other errors. I will document these errors in another post (some day).






    share|improve this answer



























      0














      There are annotations to start the web app as exemplified here:
      Spring Live Lessons Tutorial and they work. See %JAVA_BUILDINGMICROSERVICES%livelessons-securitylivelessons-security-basicsrctestjavabasicBasicSecurityApplicationTests.java.



      These annotations have been deprecated, and replaced by newer ones. The problem is that the author of this tutorial is not using theses new annotations correctly: he needs to also use @WebAppConfiguration and this requires some configuration arguments from the @SpringBootTest (which he is missing). Therefor it necessary to manually start the web server with "mvn spring-boot:run" in a separate process and then run the tests.



      When I tried to use @WebAppConfiguation, I got other errors. I will document these errors in another post (some day).






      share|improve this answer

























        0












        0








        0







        There are annotations to start the web app as exemplified here:
        Spring Live Lessons Tutorial and they work. See %JAVA_BUILDINGMICROSERVICES%livelessons-securitylivelessons-security-basicsrctestjavabasicBasicSecurityApplicationTests.java.



        These annotations have been deprecated, and replaced by newer ones. The problem is that the author of this tutorial is not using theses new annotations correctly: he needs to also use @WebAppConfiguration and this requires some configuration arguments from the @SpringBootTest (which he is missing). Therefor it necessary to manually start the web server with "mvn spring-boot:run" in a separate process and then run the tests.



        When I tried to use @WebAppConfiguation, I got other errors. I will document these errors in another post (some day).






        share|improve this answer













        There are annotations to start the web app as exemplified here:
        Spring Live Lessons Tutorial and they work. See %JAVA_BUILDINGMICROSERVICES%livelessons-securitylivelessons-security-basicsrctestjavabasicBasicSecurityApplicationTests.java.



        These annotations have been deprecated, and replaced by newer ones. The problem is that the author of this tutorial is not using theses new annotations correctly: he needs to also use @WebAppConfiguration and this requires some configuration arguments from the @SpringBootTest (which he is missing). Therefor it necessary to manually start the web server with "mvn spring-boot:run" in a separate process and then run the tests.



        When I tried to use @WebAppConfiguation, I got other errors. I will document these errors in another post (some day).







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 '18 at 17:09









        user3477493user3477493

        119112




        119112





























            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%2f53324621%2fintegration-tests-fail-to-connect-manual-browser-test-succeeds-with-java-restfu%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







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

            Museum of Modern and Contemporary Art of Trento and Rovereto