Home » Java » java.net.ConnectException when running many integration tests with JUnit and Spring boot-Exceptionshub

java.net.ConnectException when running many integration tests with JUnit and Spring boot-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have a problem when I am running integration tests. My code sends an email each time a user is created, so in my JUnit test cases, I send around 30 mails in one minute. In my local machine it works fine, but when I run tests in my server, it fails and throws the exception:

Couldn't connect to host, port: mail.mydomain.com, 587; timeout -1;
  nested exception is:
        java.net.ConnectException: Connection refused. Failed messages: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: mail.mydomain.com, 587; timeout -1;
  nested exception is:
        java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
        at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
        at java.net.Socket.connect(Socket.java:589)
        at java.net.Socket.connect(Socket.java:538)
        at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:353)
        at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:239)
        at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2175)
        at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:740)
        at javax.mail.Service.connect(Service.java:366)
        at org.springframework.mail.javamail.JavaMailSenderImpl.connectTransport(JavaMailSenderImpl.java:515)
        at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:435)
        at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:359)
        at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:354)
        at com.service.impl.mail.sendMail(ServicieCorreo.java:37)
...

There are around 17 tests that fail, but when I run the tests individually, it works well.

I am thinking could be a problem with my mail server or with my server ports, does someone has an idea of what could be the error?

How to&Answers:

I found the error. The application configuration was the same for the tls in the dev environment and the other ones. I just change this config and now it works.

Thanks.