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

  • Home
  • SEARCH
  • 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 8504451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:02:12+00:00 2026-06-11T02:02:12+00:00

I have the following class which contains a hard coded URL that never changes:

  • 0

I have the following class which contains a hard coded URL that never changes:

    public class HttpClient {
        private final String DOWNLOAD_URL = "http://original.url.json";

        public String readJsonDataFromUrl() throws IOException {
            URLConnection urlConnection = getUrlConnection();

            BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
            StringBuffer content = new StringBuffer();

            String readLine = "";
            while ((readLine = reader.readLine()) != null)  {
                content.append(readLine);
            }

            return content.toString();
        }

        private URLConnection getUrlConnection() throws IOException {
            URL jsonLocator = new URL(DOWNLOAD_URL);

            return jsonLocator.openConnection();
        }
    }

Now imagine that I’d like to expect the IOException in my test. In my opinion, the only way to do that is to rewrite the complete class in a mock object because of the final variable:

public class HttpClientMock extends HttpClient  {
    private final String DOWNLOAD_URL = "http://wrong.test.url.json";

    @Override
    public String readJsonDataFromUrl() throws IOException {
        URLConnection urlConnection = getUrlConnection();

        BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
        StringBuffer content = new StringBuffer();

        String readLine = "";
        while ((readLine = reader.readLine()) != null)  {
            content.append(readLine);
        }

        return content.toString();
    }

    private URLConnection getUrlConnection() throws IOException {
        URL jsonLocator = new URL(DOWNLOAD_URL);
        URLConnection urlConnection = jsonLocator.openConnection();

        return urlConnection;
    }
}

But this is somehow far-fetched. If the original methods would be changed, the test results could still be positive because with this attempt, I don’t actually test the original class anymore.

How can this be done properly? (I don’t want to use a framework just for this one test, so are there any design attempts to solve this in a common way?)

  • 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-06-11T02:02:13+00:00Added an answer on June 11, 2026 at 2:02 am

    Thanks to everybody, but I think that Gilbert Le Blanc’s solution is the most preferable for that case which looks like this:

    The original class:

    public class HttpClient {
        private final String DOWNLOAD_URL = "http://my.original.json.url";
    
        public String readJsonDataFromUrl() throws IOException {
            URLConnection urlConnection = getUrlConnection();
    
            BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
            StringBuffer content = new StringBuffer();
    
            String readLine = "";
            while ((readLine = reader.readLine()) != null)  {
                content.append(readLine);
            }
    
            return content.toString();
        }
    
        private URLConnection getUrlConnection() throws IOException {
            URL jsonLocator = new URL(getConnectionString());
    
            return jsonLocator.openConnection();
        }
    
        protected String getConnectionString()  {
            return DOWNLOAD_URL;
        }
    }
    

    The mock object:

    public class HttpClientMock extends HttpClient  {
        private String downloadUrl = "http://my.original.json.url";
    
        public HttpClientMock()  {
            super();
        }
    
        public HttpClientMock(String downloadUrl)  { 
            this.downloadUrl = downloadUrl;
        }
    
        @Override
        protected String getConnectionString()  {
            return downloadUrl;
        }
    }
    

    And the working tests:

    public class HttpClientTest {
    
        private JSONParser jsonParser = new JSONParser();
    
        @Test
        public void readJsonDataFromUrlSucceeds() throws IOException, ParseException {
            HttpClient httpClient = new HttpClientMock();
    
            String jsonString = httpClient.readJsonDataFromUrl();
            JSONObject jsonObject = (JSONObject)jsonParser.parse(jsonString);
    
            assertTrue(jsonObject.size() > 0);
        }
    
        @Test(expected = IOException.class)
        public void readJsonDataFromMalformedUrlFails() throws IOException, ParseException {
            HttpClient httpClient = new HttpClientMock("http://malformed");
    
            httpClient.readJsonDataFromUrl();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following class public class classB : classA which contains function dostuff
I have a base class which contains following properties: public virtual Vector3b SIZE {
I have a windows service that references an assembly which contains the following class.
I have the following singleton class which contains the following property: public class Manager
I have a class which must implement the following property public ICollection<IType> Items {
So I have a class that looks something like the following: public class MyClass
I have an abstract base class which contains a private nested implementation. visual c++
I have a BlogStore class which contains two observablecollections like so public class BlogStore
I have the following class which as you can see contains a EditText, Button
I have the following class which handles my user logged in / logged 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.