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 8589361
In Process

The Archive Base Latest Questions

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

My code is the consumer of an API (www.abc.com/public/news/apple.json). I get a json array

  • 0

My code is the consumer of an API (www.abc.com/public/news/apple.json). I get a json array in return which I then parse and populate in my own data structure. the code responsible for doing this is:

    public Map<String,List<NewsItem>> populateNewsArray() throws Exception
        {
            url = domain + newsApiStr;
            InputStream stream = getNews(url, true);

           //jackson library object mapper
           ObjectMapper mapper = new ObjectMapper();

          //NewsApiObject class implements the structure of the json array returned.
           List<NewsApiObject> mappedData = mapper.readValue(stream, NewsApiObject.class));

           //populate the properties in a HashMap.
          //return HashMap
     }

     public InputStream getNews(String request, boolean bulk) throws Exception
     {
        URL url = new URL(request); 
        connection = (HttpURLConnection) url.openConnection();           
        connection.setDoOutput(true);
        connection.setInstanceFollowRedirects(false);
        connection.setRequestMethod("GET"); 
        connection.setRequestProperty("Content-Type", "text/plain"); 
        connection.setRequestProperty("charset", "utf-8");
        connection.connect();

        return connection.getInputStream();
     }

As you can see I am not the controller of the api, only the consumer. It is said that in unit tests, one is not suppose to make http requests. In this scenario, how can I unit test the populateNewsArray() function to see if the object mapping was correct (without any exceptions) and a valid hashmap was returned?

  • 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-11T23:02:24+00:00Added an answer on June 11, 2026 at 11:02 pm

    You should extract getNews() into a separate interface, e.g. NewsReader (although the word Reader has a specific meaning in the JDK, I like the name…)

    public interface NewsReader {
        InputStream getNews(String request, boolean bulk) throws Exception
    }
    

    Then implement that interface with using HttpURLConnection as per your code and update your code to allow injection of that particular interface. Then, if you need to test how your code handles an InputStream, you can create a mock of NewsReader which returns a InputStream with well-known content.

    Remember to aim for high cohesion: your class shouldn’t be an HTTP client and a stream parser.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A consumer is trying to call my REST API from client-side code (Javascript /
I want to write a function for uploading photos to flickr as http://www.flickr.com/services/api/upload.api.html .
I have a WCF API which supports SOAP/JSON/XML but we don't provide JSONP. This
The code below is streaming the twitter public timeline for a variable which output
From REST API of twitter I user get/followers function. I pasted a code snippet
The following source code are used to implement product/consumer pattern, but they don't work
Is the following C++ code well-formed? void consumer(char const* p) { std::printf("%s", p); }
I managed to get the access token for google's contacts API but when i
I am trying to get Oauth working with the Google API using Python. I
I have RoR 3.0 web application which is acting as an OAuth API provider.

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.