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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:04:10+00:00 2026-06-14T20:04:10+00:00

I have a class (lets call it A) that: In the constructor takes a

  • 0

I have a class (lets call it A) that:

  • In the constructor takes a config and based on it, creates a stub of
    a web service and stores a reference to it in a private field.
  • Has a few methods that call web methods and some stuff inbetween.

I started to create a unit test that:

  • Creates an instance of a class A with a dummy configuration.
  • Through reflection it injects the mocked web service stub.

Although that web service has plenty of methods.

  • Should I mock them all (in every test, with different data)?
  • Or maybe I should create another layer that encapsulates only the web methods that are being used?
  • Or there is another approach?
  • 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-14T20:04:11+00:00Added an answer on June 14, 2026 at 8:04 pm

    You should create a wrapper interface around your webservice, and make your class under test take a dependency on that interface, rather than directly on the webservice; you can then mock the interface. Only make that interface expose the methods of the webservice that you find interesting. This is known as a facade pattern, and is detailed here.

    Without having a clue about what you’re testing, aim for something like this:

    public interface IWebserviceWrapper
    {
        Whatever DoStuff(int something);
    }
    
    public class WebserviceWrapper : IWebserviceWrapper
    {
        private WebService _theActualWebservice;
    
        public WebserviceWrapper(Webservice theService)
        {
            _theActualWebService = theService;
        }
    
        public Whatever DoStuff(int something)
        {
             return _theActualWebservice.DoSomething(something);
        }
    
    }
    

    Then your test would look like this (in this case, using MOQ)

    public void Test_doing_something()
    {
        Mock<IWebserviceWrapper> _serviceWrapperMock = new Mock<IWebserviceWrapper>();
    
        _serviceWrapperMock.SetUp(m => m.DoStuff(12345)).Returns(new Whatever());
    
        var classUnderTest = new ClassUnderTest(_serviceWrapperMock.Object);
    
        var result = classUnderTest.Dothings(12345);
    
        Assert.Whatever....
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class, lets call it 'Words', that reads a file and creates
What's the best way to call a class constructor that can have lots of
I have a class file called MarkerCustom. MarkerCustom has a constructor that takes three
Lets say you have a class MyClass that has a constructor public Myclass(SomeObject o)
I have a non-Activity class (let's call it NonActivity) that needs to post a
First suppose that I have an abstract class, let's call it AbstractClass. Suppose public
I have a useful object written in PHP lets call it my_class.php <?php class
Let's say I have a MySingleton class with a static constructor. I want that
If I have a service (service-layer object not web service) called WidgetProcessor and that
Lets say, for example, that I have a class that requires the use of

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.