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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:03:03+00:00 2026-05-11T22:03:03+00:00

The test class below verifies that a simple HttpService gets content from a given

  • 0

The test class below verifies that a simple HttpService gets content from a given URL. Both the implementations shown make the test pass, though one is clearly wrong because it constructs the URL with an incorrect argument.

To avoid this and correctly specify the behaviour I want, I’d like to verify that in the use block of the test case, I construct one (and only one) instance of the URL class, and that the url argument to the constructor is correct. A Groovy enhancement seems like it would let me add the statement

mockURLContext.demand.URL { assertEquals "http://www.foo.com", url }

but what can I do without that Groovy enhancement?

Update: Replaced “mock” with “stub” in the title, as I’m only interested in checking the state not necessarily the detail of the interactions. Groovy has a StubFor mechanism that I haven’t used, so I’ll leave my code as is, but I think you could just replace MockFor with StubFor throughout.

import grails.test.*
import groovy.mock.interceptor.MockFor

class HttpServiceTests extends GrailsUnitTestCase {
    void testGetsContentForURL() {
        def content = [text : "<html><body>Hello, world</body></html>"]

        def mockURLContext = new MockFor(URL.class)
        mockURLContext.demand.getContent { content }
        mockURLContext.use {
            def httpService = new HttpService()
            assertEquals content.text, httpService.getContentFor("http://www.foo.com")
        }
    }
}    

// This is the intended implementation.
class HttpService {
    def getContentFor(url) {
        new URL(url).content.text
    }
}

// This intentionally wrong implementation also passes the test!
class HttpService {
    def getContentFor(url) {
        new URL("http://www.wrongurl.com").content.text
    }
}
  • 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-11T22:03:04+00:00Added an answer on May 11, 2026 at 10:03 pm

    What does mocking the URL get you? It makes the test difficult to write. You won’t be able to react to feedback the mock objects give you about the design of the API of the URL class, because it’s not under your control. And if you don’t precisely fake the behaviour of the URL and what it exposes about the HTTP protocol, the test will not be reliable.

    You want to test that your “HttpService” object actually loads the data correctly from a given URL, copes with different content type encodings correctly, handles different classes of HTTP status code appropriately, and so forth. When I need to test this kind object — one that merely wraps some underlying technical infrastructure — I write a real integration test that verifies that the object really does use the underlying technology correctly.

    For HTTP I write a test that creates an HTTP server, plugs a servlet into the server that will return some canned data, passed the URL of the servlet to the object to make it load the data, check that the loaded result is the same as the canned data used to initialise the servlet, and stop the server in the fixture tear-down. I use Jetty or the simple HTTP server that is bundled with JDK 6.

    I’d only use mock objects to test the behaviour of objects that talk to the interface(s) of that object I’ve integration tested.

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

Sidebar

Ask A Question

Stats

  • Questions 166k
  • Answers 166k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer We use SketchFlow and have had a great response from… May 12, 2026 at 1:18 pm
  • Editorial Team
    Editorial Team added an answer When you apply an animation like this, the view's frame… May 12, 2026 at 1:18 pm
  • Editorial Team
    Editorial Team added an answer Put a border around your canvas and attach the clip… May 12, 2026 at 1:18 pm

Related Questions

If one has a project that has tests that are executed as part of
Thanks to everyone in advance, I am trying to access any context parameters in
I am trying to test a dao method which uses the hibernate criteria API,
I Have a really simple test class as below. For some reason my test

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.