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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:01:43+00:00 2026-06-12T03:01:43+00:00

Suppose the following template method implementation: public abstract class Abs { public void DoYourThing()

  • 0

Suppose the following template method implementation:

public abstract class Abs
{
    public void DoYourThing()
    {
        log.Info("Starting");
        try
        {
            DoYourThingInternal();
            log.Info("All done");
        }
        catch (MyException ex)
        {
            log.Error("Something went wrong here!");
            throw;
        }
    }

    protected abstract void DoYourThingInternal();    
}

Now, there are plenty of info around on how to test the Abs class, and make sure that DoYourThingInternal is called.
However, suppose I want to test my Conc class:

 public class Conc : Abs
 {
     protected override void DoYourThingInternal()
     {
         // Lots of awesome stuff here!
     }
 }

I wouldn’t want to do conc.DoYourThing(), since this will invoke the parent method, which was already tested separately.

I would like to test only the overriding method.

Any ideas?

  • 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-12T03:01:44+00:00Added an answer on June 12, 2026 at 3:01 am

    I wouldn’t consider DoYourThingInternal() to be separate from DoYourThing() (as in, two separate modules of code that can be tested in isolation) since you won’t be able to instantiate your abstract class alone anyways and the 2 methods will always be run together. Besides, DoYourThingInternal() has access to all protected members of your class and could modify them, with potential side effects on DoYourThing(). So I think it would be dangerous to test DoYourThing() in complete isolation from a concrete implementation of DoYourThingInternal().

    However, that doesn’t mean you can’t have separate tests for DoYourThing()‘s expected behavior, which has to remain the same across all implementations of Abs, and DoYourThingInternal()‘s expected behavior.

    You could use an (abstract) base test class where you define a test for the general behavior expected from DoYourThing(). Then create as many test subclasses as there are implementations of Abs, with unit tests for the specifics of each implementation.

    The test from the base test class will be inherited, and when you run any subclass’s tests, the inherited test for DoYourThing() will also run :

    public abstract class AbsBaseTest
    {
      public abstract Abs GetAbs();
    
      [Test]
      public void TestSharedBehavior() 
      {
        getAbs().DoYourThing();
    
        // Test shared behavior here...
      }
    }
    
    [TestFixture]
    public class AbsImplTest : AbsBaseTest
    {
      public override Abs GetAbs()
      {
        return new AbsImpl();
      }
    
      [Test]
      public void TestParticularBehavior()
      {
        getAbs().DoYourThing();
    
        // Test specific behavior here
      }
    }
    

    See http://hotgazpacho.org/2010/09/testing-pattern-factory-method-on-base-test-class/

    Don’t know if abstract test class inheritance is supported by all unit test frameworks though (I think NUnit does).

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

Sidebar

Related Questions

Suppose the following class public class Message { // some code } And a
Suppose I have following code package memoryleak; public class MemoryLeak { public static int
Suppose I have following template class: template<unsigned char I, unsigned char F> class FOO
Suppose I have following template template<unsigned char I, unsigned char F> class FOO {
Suppose that I have following class template<unsigned char B, unsigned char F> class Foo
Suppose the following template definition (the code is meaningless): template<class X, class Y> bool
gcc 4.5.1, SuSE Linux i686 Suppose we have following code: template<typename realT> class B
Suppose I have a class template <typename T> class A { public: template <typename
Suppose I have got the following template template <typename T> class{ T t; };
Suppose I've got the following program: namespace ReflectionTest { public class Example { private

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.