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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:02:26+00:00 2026-05-27T09:02:26+00:00

I am unit testing a method say ABC() like myclass target = new myclass();

  • 0

I am unit testing a method say ABC() like

myclass target = new myclass();
target.ABC();

This ABC() method in turn calls another method XYZ() from a different class like anotherclass.XYZ() and this XYZ() method’s input parameter depends on values from textbox (s).

Since, at test run there is no values from textbox passed I am getting a null referrence exception while running the test (I mean test fails with exception).

Similar situation (another): method fetches id value from querystring like

HttpContext context
id = context.request.querystring["id"]

Since, at test run this id values is null I get a null referrence exception(I mean test fails with exception).

To my understanding, logically this is correct since this is test run and not actual run but still want to confirm once …

Am I doing anything wrong?

OR

My test is functioning correct?

Please suggest. Appreciate your help.

Thanks.

  • 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-27T09:02:27+00:00Added an answer on May 27, 2026 at 9:02 am

    I have no idea what your code looks like but it probably needs to be refactored a little bit to allow unit testing.

    For instance, if you have code like this:

    MyClass{
    
      private AnotherClass _another = new AnotherClass();
    
      public string ABC(){
        return _another.XYZ();
      }
    }
    
    public AnotherClass{
      //Context comes from somewhere...
      public string XYZ(){
        return context.request.querystring["id"]
      }
    }
    

    You would want to change it to something like this:

    interface IAnotherClass{
      string XYZ();
    }
    
    MyClass{  
      private IAnotherClass _another = new AnotherClass();
    
      public MyClass(IAnotherClass anotherClass){
        _another = anotherClass;
      }
    
      public string ABC(){
        return _another.XYZ();
      }
    }
    
    public AnotherClass: IAnotherClass{
      //Context comes from somewhere...
      public string XYZ(){
        return context.request.querystring["id"]
      }
    }
    

    You can then use a mocking framework to mock up the IAnotherClass as IceN has show. As for the HttpContext – that becomes a little more complicated, but if it’s only used by the AnotherClass you wont need to mock it since the mock object will just return whatever you like. Obviously when you come to test AnotehrClass it becomes an issue. If you find you do need to mock it then there are a number of examples of what to do – for instance this one.

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

Sidebar

Related Questions

This is my doubt on what we regard as a unit while unit-testing. say
Say I've got a mock setup like this: JUnit4Mockery context = new JUnit4Mockery(); MyInterface
I'm unit testing a method which uses automapper to map a class from my
This is pretty basic question around unit testing. I have a method e.g. GetOrderDetails
I'm having trouble unit testing a method that changes some properties of a reference
In unit testing, the setup method is used to create the objects needed for
I am starting out with unit testing, I have a method that uses the
Should one create unit tests involving IO? Ie, testing a class method for serializing/deserializing
I have a question about unit testing. Say I have a controller with one
I am new to Python and while unit testing some methods on my object

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.