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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:19:36+00:00 2026-06-05T16:19:36+00:00

OK so I have the following classes in C#: class Program { static void

  • 0

OK so I have the following classes in C#:

class Program
{
    static void Main(string[] args)
    {
        MyClass myClass = new MyClass("Hello World");
        myClass.WriteToConsole();
    }
}

class MyClass
{
    private string MyProperty { get; set; }

    public MyClass(string textToEncapsulate)
    {
        MyProperty = textToEncapsulate;
    }

    public void WriteToConsole()
    {
        Console.WriteLine(MyProperty);
    }
}

Three questions:

  1. What is unit testing?
  2. Would unit testing be beneficial in the above example?
  3. How would I go about ‘Unit Testing’ the above example?

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-06-05T16:19:38+00:00Added an answer on June 5, 2026 at 4:19 pm

    1. What is unit testing?

    Manual testing is time consuming. It can be hard to run the exact same set of tests each time by hand to make sure that all parts of your code are functioning as expected. When testing a complete product by hand, it’s also really hard to test all code paths.

    How would you test the reaction of your code when a database is unavailable? Or when some erroneous data is stored? That would take quite some time to get right.

    Unit Testing means that we start testing the smallest possible parts of our code. And to make sure we can do this easily we automate the process. This means that we write test code that tests our production code.

    For example:

    int a = 3;
    int b = 5;
    
    Calculator c = new Calculator();
    int sum = c.Sum(a, b);
    
    Assert.AreEqual(8, sum);
    

    This tests assures that your Sum function on your Calculator class is working correctly.

    Now, let’s say that you want to optimize the inner workings of your Calculator class. You start changing and optimizing code. After each change you run your unit test and when they all succeed you know you haven’t broken any code.

    Let’s say that in production a user submits a bug report for your Calculator. Your first step will be to write a unit test that shows this bug. After the new test is failing (because the bug is still there!) you fix the bug, the unit tests succeeds and you can be certain that this bug will never come back.

    This safety harness is one of the biggest benefits of unit tests.

    2 Would unit testing be beneficial in the above example? 3 How would I go about ‘Unit Testing’ the above example?

    Unit Testing is a good practice. It helps you prove that your code is working. In your example however, it would be hard to test the code.

    An output to the console is not something that can be easily tested. If however, you would abstract the idea of Console.WriteLine then your code becomes better testable.

    Writing Unit Tests is actually quite simple. The problem is writing code that can actually be tested.

    A better testable version of your code would be:

    class Program
    {
        static void Main(string[] args)
        {
            MyClass myClass = new MyClass(new ConsoleOutputService(), "Hello World");
            myClass.WriteToConsole();
        }
    }
    
    public interface IOutputService
    {
    
        void WriteLine(string MyProperty);
    }
    
    public class ConsoleOutputService : IOutputService
    {
        public void WriteLine(string MyProperty)
        {
            Console.WriteLine(MyProperty);
        }
    }
    
    class MyClass
    {
        private IOutputService _outputService;
        private string MyProperty { get; set; }
    
        public MyClass(IOutputService outputService, string textToEncapsulate)
        {
            _outputService = outputService;
            MyProperty = textToEncapsulate;
        }
    
        public void WriteToConsole()
        {
            _outputService.WriteLine(MyProperty);
    
        }
    }
    

    You have replaced your direct dependency on the Console with an interface. When unit testing this code, you could supply a fake for your IOutputService and check the outcome.

    A really good book is xUnit Test Patterns. It shows the common pitfalls in writing unit test and patterns to avoid/fix them.

    I also wrote a blog myself about testable code a couple of months ago. It’s somewhat more advanced but maybe you can get something out of it. If you have any questions, feel free to ask.

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

Sidebar

Related Questions

Assume I have the following classes class Genre { static hasMany=[author:Author] } class Author{
I have the following two classes: public class Address { public string AddressLine1 {
Let's say I have this Hello.scala. object HelloWorld { def main(args: Array[String]) { println(Hello,
I have following classes: class Department { private string departmentId; private string departmentName; private
I have a static array of classes similar to the following: public class Entry
I have the following classes: class Vigil < ActiveRecord::Base after_update :do_something_cool private def do_something_cool
Suppose, I have following classes: public class DisposableObj : IDisposable { public ChildObj CreateObj();
with C#, java or c++ ... we have the following classes class A {
I have the following existing classes: class Gaussian { public: virtual Vector get_mean() =
I have the following c# classes: class A : Object { foo() {} }

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.