Integration Tests fail to connect, Manual Browser Test succeeds with Java RESTful Web Service with SpringBoot/Jsoup Integration Test
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.
- We know this is not a firewall problem because it is just with localhost and it works with chrome.
- There is no proxy involved.
- I think the ports are correct: I presently have 9090 in the application.properities file, and in the source code and in the browser.
- "mvn clean" and then "mvn test" again did not help.
java rest spring-boot integration-testing
add a comment |
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.
- We know this is not a firewall problem because it is just with localhost and it works with chrome.
- There is no proxy involved.
- I think the ports are correct: I presently have 9090 in the application.properities file, and in the source code and in the browser.
- "mvn clean" and then "mvn test" again did not help.
java rest spring-boot integration-testing
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
add a comment |
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.
- We know this is not a firewall problem because it is just with localhost and it works with chrome.
- There is no proxy involved.
- I think the ports are correct: I presently have 9090 in the application.properities file, and in the source code and in the browser.
- "mvn clean" and then "mvn test" again did not help.
java rest spring-boot integration-testing
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.
- We know this is not a firewall problem because it is just with localhost and it works with chrome.
- There is no proxy involved.
- I think the ports are correct: I presently have 9090 in the application.properities file, and in the source code and in the browser.
- "mvn clean" and then "mvn test" again did not help.
java rest spring-boot integration-testing
java rest spring-boot integration-testing
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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).
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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).
add a comment |
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).
add a comment |
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).
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).
answered Nov 20 '18 at 17:09
user3477493user3477493
119112
119112
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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