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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:46:50+00:00 2026-06-01T09:46:50+00:00

I have two Controller unit tests and each one sets an HttpClient metaclass execute

  • 0

I have two Controller unit tests and each one sets an HttpClient metaclass execute in setUp() like the following:

HttpClient.metaClass.execute = { HttpUriRequest request ->
    <return expected data for my tests>
}

Then I attempt to tear down the metaClass in tearDown() with the following code:

protected void tearDown() {
    super.tearDown()
    GroovySystem.metaClassRegistry.removeMetaClass(HttpClient.class)
}

However only one of my unit tests passes because the return from the HttpClient is incorrect/unexpected. If I add the logic needed for both tests in the metaClass.execute of both tests I get no testing failures. However this is cumbersome and impractical, especially in an agile development environment.

What am I doing wrong with trying to tear down this HttpClient metaclass registration? How can I troubleshoot this further?

I’m currently using grails 1.3.7 on a CentOS 5 install.

Edit: I should clarify that my problem is that the metaClass override is causing issues between test classes, not test cases. We’ve been setting up the metaClass override so that it will return correct data for all of the test cases in a given class. So Test class A has the metaClass data for it’s test cases and Test class B has the metaClass data for it’s test cases. The issue is that since Test class A gets tested first, test class B ends up using the metaClass definition from test class A and fails because of this.

  • 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-01T09:46:51+00:00Added an answer on June 1, 2026 at 9:46 am

    The solution I ended up using is a bit of a hack but it’s easier to maintain. I just made a class in src/groovy that creates the HttpClient.metaClass and then just made each test class that needs it run the static method on the new class. That way all of the necessary code for the meta classing is in one place.

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

Sidebar

Related Questions

Can somebody explain me one thing. I have two methods in my controller :
If I have a Spring Controller with two SEPARATE methods, one annotated by: @ExceptionHandler(Exception.class)
I have a question about unit testing. Say I have a controller with one
In my Asp.net MVC app, I have two methods on a controller, one for
In my controller I have two actions called Friends. The one that executes depends
I have two table views in a single controller.One is grouped whose frame is
Let me have two very basic objects like: public class View { public View(Controller
In my view controller I have two UIScrollViews . One is to display latest
I have two view controller classes in my application delegate. I can change from
I have two actions in my controller : def find @item = Item.find(params[:id]) render

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.