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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T05:13:47+00:00 2026-05-21T05:13:47+00:00

Here is my problem: I have an n-tiers application for which I have to

  • 0

Here is my problem:

I have an n-tiers application for which I have to write unit tests. Unit tests are for the business layer.

I have a method to test called Insert() and this one use two protected methods from inheritance and call directly a method from Data access layer.

So I have made a mock object for the DAL. But here is the point, in a (edit 🙂 protected method from inheritance, It will use another object from DAL! It seems it is not possible to mock this one!

Here is the method for test code:

public int Insert(MYOBJECT aMyObject)
    {
            //first inherited method use the FIRSTDALOBJECT so the mock object --> No problem
            aMyObject.SomeField= FirstInherited();

            //Second inherited method (see after) --> my problem
            aMyObject.SomeOtherField = SecondInherited();

            // Direct access to DALMethod, use FIRSTDALOBJECT so the mock -->No Problem
            return this.FIRSTDALOBJECT.Insert(aMyObject);             
     }

Here is the SecondInherited method:

 protected string SecondInherited ()
    { 
        // Here is my problem, the mock here seems not be possible for seconddalobject                                          
        return ( new SECONDDALOBJECT Sdo().Stuff());
    }

And here is the unit test method code :

    [TestMethod()]
    public void InsertTest()
    {
        BLLCLASS_Accessor target = new BLLCLASS_Accessor();
        MYOBJECT aMyObject = new MYOBJECT { SomeField = null, SomeOtherField = 1 };
        int expected = 1;
        int actual;

        //mock
        var Mock = new Mock<DAL.INTERFACES.IFIRSTDALOBJECT>();
        //Rec for calls
        List<SOMECLASS> retour = new List<SOMECLASS>();
        retour.Add(new SOMECLASS());

        //Here is the second call (last from method to test)
        Mock
            .Setup(p => p.Insert(aMyObject))
            .Returns(1);

        // Here is the first call (from the FirstInherited())
        Mock
            .Setup(p => p.GetLast())
            .Returns(50);
        // Replace the real by the mock
        target.demande_provider = Mock.Object;

        actual = target.Insert(aMyObject);
        Assert.AreEqual(/*Some assertion stuff*/);
    }

Thank you for reading all the question 🙂 Hope it is clear enough.

  • 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-21T05:13:48+00:00Added an answer on May 21, 2026 at 5:13 am

    Your text seems to say that SecondInherited is private, while in the code example it is protected. Anyway, if it is not protected, I would suggest changing its access qualifier as the first step.

    You can create a subclass solely for testing purposes, and override SecondInherited there to avoid creating SECONDDALOBJECT and just return some value suitable for your tests.

    This way you can write your first unit test(s) with minimal changes to the class being tested, thus minimizing the chances of breaking something. Once you have the unit tests in place, these allow you to do more refactoring safely, eventually achieving a better (more testable / mockable) design, such as using Dependency Injection, or a Factory. (I would probably prefer an Abstract Factory over Factory Method here, as the latter would practically force you to keep subclassing the tested class).

    The fundamental, highly recommended book for learning this technique (and many more) is Working Effectively With Legacy Code.

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

Sidebar

Related Questions

Here's my problem: I have a virtual method defined in a .h file that
Here's my problem: I have do create a menu/list of actions (which would be
hello everyone i have a problem with my 3-tiers application i don't know how
Say you have an application divided into 3-tiers: GUI, business logic, and data access.
I have problem adding arraylist to list view, will explain about my problem here..
First, sorry for my bad english, I'm French. Here the problem : I have
Here is the problem I have: I have a lot (tens of thousands) of
Here's my problem - I have some code like this: <mx:Canvas width=300 height=300> <mx:Button
Here is the problem: we have lots of Javascripts and lots of CSS files,
Here's my problem: I have to call a web service with a secure header

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.