Operation timed out error when running spring-boot:run - spring

I am trying to run through the Spring Quickstart Guide.
I have the DemoApplication.kt file updated to
package com.example.demo
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
#SpringBootApplication
#RestController
class DemoApplication
fun main(args: Array<String>) {
runApplication<DemoApplication>(*args)
}
#GetMapping("/hello")
fun hello(#RequestParam(value = "name", defaultValue = "World") name: String?): String? {
return String.format("Hello %s!", name)
}
When I run ./mvnw spring-boot:run it times out with Exception in thread "main" java.net.ConnectException: Operation timed out.
Stack Trace
at java.base/sun.nio.ch.Net.connect(Net.java:579)
at java.base/sun.nio.ch.Net.connect(Net.java:568)
at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:588)
at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327)
at java.base/java.net.Socket.connect(Socket.java:639)
at java.base/sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:304)
at java.base/sun.security.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:174)
at java.base/sun.net.NetworkClient.doConnect(NetworkClient.java:183)
at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:532)
at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:637)
at java.base/sun.net.www.protocol.https.HttpsClient.<init>(HttpsClient.java:266)
at java.base/sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:380)
at java.base/sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:193)
at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1245)
at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1131)
at java.base/sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:179)
at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1668)
at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1592)
at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:224)
at org.apache.maven.wrapper.DefaultDownloader.downloadInternal(DefaultDownloader.java:106)
at org.apache.maven.wrapper.DefaultDownloader.download(DefaultDownloader.java:93)
at org.apache.maven.wrapper.Installer.createDist(Installer.java:86)
at org.apache.maven.wrapper.WrapperExecutor.execute(WrapperExecutor.java:155)
at org.apache.maven.wrapper.MavenWrapperMain.main(MavenWrapperMain.java:72)
Any idea what I need to do differently? Thanks.

The Spring App you wrote wouldn't normally make an outbound connection that could fail - that was what was confusing about the original question.
Now that you have provided the stack trace it is clearer what is going on.
What's happening is that you are compiling and running the app using Maven and Maven needs some more artefacts for your application so it is automatically downloading them for you org.apache.maven.wrapper.DefaultDownloader.download(....
So if you have to use a proxy to reach the Internet, then you need to give these details to Maven. Try this guide: https://maven.apache.org/guides/mini/guide-proxies.html

Related

TestNG error from Eclipse while running my Maven project

TestNG Error in Maven ProjectHi Davis and others as well: - Below is my project screenshot and code and i am getting this TestNg Error using Maven Project, please help me on this and I have also attached few screenshots for reference:-
GenerateReports.java
package ExtentReports.GenerateHTMLReports;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.reporter.ExtentSparkReporter;
public class GenerateReports {
ExtentReports E2;
#BeforeTest
public void config()
{
String s1 = System.getProperty("user.dir")+"\\reports\\index1.html";
ExtentSparkReporter E1 = new ExtentSparkReporter(s1);
E1.config().setReportName("MyAutomationReport");
E1.config().setDocumentTitle("GoogleHomePage");
E2 = new ExtentReports();
E2.attachReporter(E1);
E2.setSystemInfo("Tester", "G");
}
#Test
public void initializeDriver()
{
E2.createTest("MyHTMlReportTest");
System.setProperty("webdriver.chrome.driver",
"F:\\Selenium_Training\\Ganesh_Project\\Browser_Drivers\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com");
driver.getTitle();
driver.close();
E2.flush();
}
}
Pom.xml
I am getting a TestNG error while running a Maven project in XML and here is the error message:-
org.testng.TestNGException:
TestNG by default disables loading DTD from unsecured Urls. If you need to explicitly load the DTD from a http url, please do so by using the JVM argument [-Dtestng.dtd.http=true]
at org.testng.xml.TestNGContentHandler.resolveEntity(TestNGContentHandler.java:115)
at com.sun.org.apache.xerces.internal.util.EntityResolverWrapper.resolveEn
Also, i tried the below possibilities and again getting error message which is different from this:-
I tried to add Jvm argument -Dtestng.dtd.http=true and placed it under run -->run configurations-->arguments-->VM arguments in eclipse
After placing the above jvm argument i am getting this new error message in console, do not know how to solve it below is the error after adding jvm argument and running the maven project:-
org.testng.TestNGException: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at org.testng.TestNG.parseSuite(TestNG.java:354)
at org.testng.TestNG.initializeSuitesAndJarFile(TestNG.java:374)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:103)

Run unit tests with spring boot, "no runnable methods" error

I'm running unit tests with Spring boot, but I get a weird no runnable error. The tests pass by the way, but after all test have ended successfully, I get this weird error out of nowhere:
java.lang.Exception: No runnable methods
at org.junit.runners.BlockJUnit4ClassRunner.validateInstanceMethods(BlockJUnit4ClassRunner.java:191)
at org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(BlockJUnit4ClassRunner.java:128)
at org.junit.runners.ParentRunner.validate(ParentRunner.java:416)
at org.junit.runners.ParentRunner.<init>(ParentRunner.java:84)
at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:65)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.<init>(SpringJUnit4ClassRunner.java:137)
at org.springframework.test.context.junit4.SpringRunner.<init>(SpringRunner.java:49)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
How do I fix this? Why is Spring boot looking for a runnable in my tests?
Here is an example of my code
package ca.bell.uc.hello.world
import org.junit.Assert
import org.junit.jupiter.api.Test
import org.junit.runner.RunWith
import org.springframework.test.context.junit4.SpringRunner
#RunWith(SpringRunner::class)
internal class example {
#Test
fun f() {
Assert.assertTrue(true)
}
}
And here is a screenshot of the error:
Thanks
P.S. This is Kotlin
You are trying to use JUnit 5 #Test annotation because of import:
import org.junit.jupiter.api.Test
In the console log we can see that JUnit 4 is used.
If you want to work with JUnit 4 you should use the import:
import org.junit.Test
Indeed it's the mix of JUnit4 and JUnit5. I had the same thing with tests with #Ignore or #Disabled that would not want to get ignored based on which #Test it was.
You can avoid this issue with spring by updating your spring dependencies and remove the Junit4 dependencies from your build.gradle.kts so that you won't mix them (tested with Gradle v6, check the groovy syntax if you use build.gradle).
testImplementation("org.springframework.boot:spring-boot-starter-test") {
exclude(module = "junit")
exclude(module = "junit-vintage-engine")
exclude(module = "mockito-core")
}
testImplementation("org.junit.jupiter:junit-jupiter:5.4.2")
testImplementation("org.junit.jupiter:junit-jupiter-api")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
Once you have that you don't need to use #RunWith(SpringRunner::class) you can see the difference in your import, they should all contains "jupiter" and your class could look like this:
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
internal class ExampleTest {
#Test
fun test() {
Assertions.assertTrue(true)
}
}

org.apache.http.conn.HttpHostConnectException: Connect to localhost:8080 [localhost/127.0.0.1] failed: Connection refused: connect

I'm new to spring-boot and I'm testing spring-boot application using http API, but I got connection error.
GET http://localhost:8080/api/helloworld
org.apache.http.conn.HttpHostConnectException: Connect to localhost:8080 [localhost/127.0.0.1] failed: Connection refused: connect
//HelloWorldController.java
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class HelloWorldController {
#GetMapping(value="/api/helloworld")
public String helloWorld(){
return "helloworld";
}
}
//helloworld.http
GET http://localhost:8080/api/helloworld
And It works well with running main java file.
Please let me know where I'm doing wrong. Thank you.
I solved it by running npm install. In the server.js

Gradle Error in contextLoads under test

i am trying to build a web using Spring and Angular Js tegether w/ Gradle. my problem is When i run my project or build gradle this error comes out.
"contextLoads FAILED"
"What went Wrong:"
.> There were failing tests.: /build/reports/tests/test/index.html
Caused by:
org.springframework.beans.factory.UnsatisfiedDependencyException
Caused by:
org.springframework.beans.factory.NoSuchBeanDefinitionException
ContextLoad Leads me to this Class:
package com.linkedin.learning.linkedinlearningfullstackappangularspringboot;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
#RunWith(SpringRunner.class)
#SpringBootTest
public class LinkedInLearningFullStackAppAngularSpringBootApplicationTests {
#Test
public void contextLoads() {
}
}
heres the screenshot to make it clearer:
enter image description here

Kotlin - running an integration test, error `fun main is already defined`

I have written an integration test for a Web application but I don't know how to run this test. Maven build fails with the following error when I execute mvn test
[INFO] Compiling Kotlin sources from [src/main/kotlin]
[INFO] Classpath: <...>
[INFO] Classes directory is <...>\target\classes
[INFO] Module name is demo
[ERROR] <...>\src\main\kotlin\demo\Application.kt: (9, 1) 'public fun main(args: kotlin.Array<kotlin.String>): kotlin.Unit' is already defined in demo
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
Here is my main class (Application.kt)
package demo
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
#SpringBootApplication
open class Application
fun main(args: Array<String>) {
SpringApplication.run(Application::class.java, *args)
}
Here is a part of my test class
package demo
import org.junit.runner.RunWith
import org.springframework.boot.test.SpringApplicationConfiguration
import org.springframework.boot.test.WebIntegrationTest
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner
import demo.Application
import demo.model.City
import demo.repository.CityRepository
#RunWith(SpringJUnit4ClassRunner.class)
#SpringApplicationConfiguration(classes = Application.class)
#WebIntegrationTest({"server.port=8080"})
class CityControllerTest {
#Autowired
private var cityRepository: CityRepository?
private val restTemplate: RestTemplate = TestRestTemplate()
// ...
}
There is no such error when I execute mvn clean test. So I suppose Kotlin is frightened of previously compiled classes. Though I am very unsure...
What is the right way to run an integration test?
It is a bug (https://youtrack.jetbrains.com/issue/KT-10051). Already fixed and the fix will be delivered very soon

Resources