0

I'm trying to replicate a scenario where the code should rerun a certain number of times if an exception is caught.

using System;
using System.Threading;

public class Program
{
    public static void Main()
    {
        MyClass myClass = new MyClass();
        Console.WriteLine(myClass.MyMethod(5));
    }
}

public class MyClass
{
    public Test(){}

    public string MyMethod(int tryCount) {          
        while(tryCount > 0)
        {
            // some logic that returns the switchType
            // in this case i'm just manually setting it to 1       
            int switchType = 1;

            try {
                switch(switchType)
                {
                    case 1:
                        return "it worked!!!";
                    case 2:
                        break;
                }
            } catch (Exception e){
                if (--tryCount == 0) throw new Exception("Failed");
            }
        }

        return null;
    }
}

How do I force it to go the catch block, so that I can test my re-try logic is working?

blankface
  • 5,757
  • 19
  • 67
  • 114
  • Not really, since in my case, for the sake of testing, I want to force an exception. – blankface May 08 '20 at 06:14
  • It's probably in the code that produces `switchType` than you want to catch, no? – vc 74 May 08 '20 at 06:19
  • There is no way, simple `switch` with `int`s will throw exception... therefore there is no way, or reason to test it. If you had something that could throw exception (eg. `someClass.DoSomething()`).. then you could mock that part (eg. `someClassMock.Setup(mock => mock.DoSomething()).throws(new Exception())`) – Šimon Kocúrek May 08 '20 at 06:20
  • @vc74 That makes sense, I'll flesh it out to trigger an exception. Besides that is my retry logic sound? – blankface May 08 '20 at 06:22

2 Answers2

1

The shortest answer to your question is "just throw a new exception".

But you probably want to be able to test Happy Path too.

Here's what I'd do:

using System;
using System.Threading;

public class Program
{
    public static void Main()
    {
        // what happens when no problem
        MyClass.happyPath = true;
        MyClass myClass = new MyClass();
        Console.WriteLine(myClass.MyMethod(5));

        // does the retry work?
        MyClass.happyPath = false;
        MyClass myClass = new MyClass();
        Console.WriteLine(myClass.MyMethod(5));
    }
}

public class MyClass
{

    public static boolean happyPath = true; 

    public Test(){}

    public string MyMethod(int tryCount) {          
        while(tryCount > 0)
        {
            // some logic that returns the switchType
            // in this case i'm just manually setting it to 1       
            int switchType = 1;

            try {
                switch(switchType)
                {
                    case 1:

                        if (!happyPath) {
                           throw new Exception ("Oops, it didn't work!");
                        }
                        return "it worked!!!";
            case 2:
                        break;
                }
            } catch (Exception e){
                if (--tryCount == 0) throw new Exception("Failed");
            }
        }

        return null;
    }

}

The throw that you add inside the break statement should be caught by your catch which allows you to check out your retry logic. Suggest you add some logging.

GregHNZ
  • 7,946
  • 1
  • 28
  • 30
  • This is exactly what I was looking for. Thanks. On another note, does my re-try logic look sound to you? – blankface May 08 '20 at 06:31
0

You can throw exception from your code manually while testing.

throw new Exception("Your custom message");