Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4259182
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T05:44:57+00:00 2026-05-21T05:44:57+00:00

Hey, have been trying to work this out for last day or so but

  • 0

Hey, have been trying to work this out for last day or so but hitting brick wall. Trying to unit test this bit of code. But not sure if need to use EasyMock or not?? Seem few examples online but seem to be using older techniques.

public boolean verifyConnection(final String url) {
    boolean result;

    final int timeout = getConnectionTimeout();
    if (timeout < 0) {
        log.info("No need to verify connection to client. Supplied timeout = {}", timeout);
        result = true;
    } else {
        try {
            log.debug("URL: {} Timeout: {} ", url, timeout);

            final URL targetUrl = new URL(url);
            final HttpURLConnection connection = (HttpURLConnection) targetUrl.openConnection();

            connection.setConnectTimeout(timeout);
            connection.connect();
            result = true;
        } catch (ConnectException e) {
            log.warn("Could not connect to client supplied url: " + url, e);
            result = false;
        } catch (MalformedURLException e) {
            log.error("Malformed client supplied url: " + url, e);
            result = false;
        } catch (IOException e) {
            log.warn("Could not connect to client supplied url: " + url, e);
            result = false;
        }
    }
    return result;
}

It just take’s in a url checks its valid and returns T or F.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T05:44:58+00:00Added an answer on May 21, 2026 at 5:44 am

    I have always observed that Mocking Can be avoided as much as possible because it can lead to difficult to maintain JUnit tests and defeat the whole purpose.

    My suggestion would be to create a temporary server on your local machine from a JUnit itself.
    At the beginning of JUnit you can create a server(not more than 10-15 lines of coding required) using Java sockets and then in your code pass the URL for the local server. This way you are reducing mocking and ensuring maximum code coverage.

    Something like this –

    public class SimpleServer extends Thread {
    
    public void run() {
        try {
            serverSocket = new ServerSocket(port);
    
              while (true) {
                Socket s = serverSocket.accept(); 
              }
        } 
        catch (IOException e) {
                e.printStackTrace();
        }
        finally {
            serverSocket = null;
        }
    }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey I have been trying to work on this for some time but I
Hey i have been trying this for a while but with no luck. I
Hey all i have been trying to figure out why i am getting this
Hey all, i have been trying for a few minutes on this problem and
hey people I'm new at jquery and i have been trying to figure this
Hey all, I have been doing nothing but web development over the last few
Hey this might be a simple question, but i have been stumped on it
Hey guys so I have been trying to figure out why my update function
Hey guys, I've been trying to play with mongoose & node, but I have
hey all, I'm very confused here, i've been trying to figure this one out.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.