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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:04:32+00:00 2026-05-29T05:04:32+00:00

I am trying to figure out how to write my Test cases for a

  • 0

I am trying to figure out how to write my Test cases for a service I am going to write.

The service will use HTTPBuilder to request a response from some URL. The HTTPBuilder request only needs to check the response for a success or failure. The service implementation will be be something as simple as:

boolean isOk() {
    httpBuilder.request(GET) {
        response.success = { return true }
        response.failure = { return false }
    }
}

So, I want to be able to mock the HTTPBuilder so that I can set the response to be either success/failure in my test so I can assert that my service’s isOk method returns True when the response is a success and False, when the response is a failure.

Can any one help with how I can mock the HTTPBuilder request and set the response in a GroovyTestCase?

  • 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-29T05:04:33+00:00Added an answer on May 29, 2026 at 5:04 am

    Here’s a minimal example of a mock HttpBuilder that will handle your test case:

    class MockHttpBuilder {
        def result
        def requestDelegate = [response: [:]]
    
        def request(Method method, Closure body) {
            body.delegate = requestDelegate
            body.call()
            if (result)
                requestDelegate.response.success()
            else
                requestDelegate.response.failure()
        }
    }
    

    If the result field is true, it’ll invoke the success closure, otherwise failure.

    EDIT: Here’s an example using MockFor instead of a mock class:

    import groovy.mock.interceptor.MockFor
    
    def requestDelegate = [response: [:]]
    def mock = new MockFor(HttpBuilder)
    mock.demand.request { Method method, Closure body ->
        body.delegate = requestDelegate
        body.call()
        requestDelegate.response.success() // or failure depending on what's being tested
    }
    mock.use {
        assert isOk() == true
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to figure out how to write a jquery formula that will sum all
I have been trying to figure out the best practices to write test-friendly code,
I'm trying to figure out how to write a MySQL query that will return
I've been trying to figure out how to write this regular expression. It is
Trying to figure out which to use.
Trying to figure out how to adequately test my accounts controller. I am having
I am trying to figure out how to correctly and efficiently unit test my
I'm trying to figure out the best option to use anonymous event listeners that
I'm writing a simple REST service in Node.js (just experimenting), trying to figure out
Trying to figure out how to write a custom matcher for a primitive value.

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.