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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T04:22:37+00:00 2026-05-11T04:22:37+00:00

I need to test a method belonging to a service class. This service class

  • 0

I need to test a method belonging to a service class. This service class has several dependencies in the constructor, some used by this method, some not. If we should not be using a DI container for our unit tests what is the best way to instantiate the service class?

var service = new ServiceClass(new Repository1(), new Repository2(), new ServiceClass2(), etc.); 

That’s hard to read and seems like a lot of code just to test one method. Things get real messy when some of those dependencies have dependencies of their own.

  • 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. 2026-05-11T04:22:37+00:00Added an answer on May 11, 2026 at 4:22 am

    You should really look at using a mocking framework to isolate your test from the actual dependent objects. I’m assuming you use C# (from the var keyword), so I’ll give an example from RhinoMock.

    var respository1 = MockRepository.GenerateMock<Repository1>(); repository1.Expect( r => r.SomeMethod() ).Return( someValue );  var repository2 = MockRepository.GenerateMock<Repository2>(); repository2.Expect( r => r.Method2() ).Return( someValue );   ...  var service = new Service( repository1, repository2, ... );  repository1.VerifyAllExpectations(); repository2.VerifyAllExpectations(); 

    Using this mechanism you can control how the mock object responds to your class under test and you isolate your test code from related dependencies. You can also test that your class under test is interacting properly with the classes that it depends on by verifying that the expectations that you set up have been met (called).

    As for the number of parameters in the constructor, consider providing a default constructor that takes no parameters and having public settors for the dependencies. Then you can use the convenience mechanisms in C# 3.0 for defining them.

    var service = new Service {     Repository1 = repository1,     Repository2 = repository2,     ... }; 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why does this method (test) need a return value (it is always true)? public
I need to test some security related classes that depend on Spring Security. This
I need to write a unit test for a method that will print a
I need to simulate a test scenario in which I call the getBytes() method
I need to test a url that it does not end with .asp So
I'm new to Moq and learning. I need to test that a method returns
Iam writing unit tests for my repository and I need to test repository method
I recently need to build a Expression tree so I wrote a Test method
I have used a happy test to create a method and now i am
Sometimes I need a test method to be executed simultaneously by N threads (like

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.