ThrowingException Causing UnitTest Break?












2















I have a situation that at some case the actual function throwing an exception and i written unit test for it but unfortunately the unit test is failed.



Sample Code:



// 'CheckNumber()' function is Present in 'Number' class.

public int CheckNumber(int Number)
{
if (Number < 0 || Number > MaxNumber) // MaxNumber = 300
throw new ArgumentOutOfRangeException();

//..
}


Unit Test:



I am using NUnit Framework.



// When The Number is Less than Zero Or Greater than Maximun Number

[Test]
public void CheckNumberTest()
{
Number number = new Number();
int returnedValue = number.CheckNumber(-1);

// Assertion.
Assert.That(returnedValue , Throws.ArgumentOutOfRangeException);
}


This Test is failing when i run test. This test is actually throwing the exception and TestMethod is going to break? So How to fix it?










share|improve this question




















  • 1





    Try use Assert.Throws<ArgumentOutOfRangeException>(() => your function)

    – Johnny
    Jan 3 at 8:11











  • I tried this: but Test is still failing! Assert.Throws<ArgumentOutOfRangeException>(() => CheckNumberTest) @johnny

    – Rehan Shah
    Jan 3 at 8:16


















2















I have a situation that at some case the actual function throwing an exception and i written unit test for it but unfortunately the unit test is failed.



Sample Code:



// 'CheckNumber()' function is Present in 'Number' class.

public int CheckNumber(int Number)
{
if (Number < 0 || Number > MaxNumber) // MaxNumber = 300
throw new ArgumentOutOfRangeException();

//..
}


Unit Test:



I am using NUnit Framework.



// When The Number is Less than Zero Or Greater than Maximun Number

[Test]
public void CheckNumberTest()
{
Number number = new Number();
int returnedValue = number.CheckNumber(-1);

// Assertion.
Assert.That(returnedValue , Throws.ArgumentOutOfRangeException);
}


This Test is failing when i run test. This test is actually throwing the exception and TestMethod is going to break? So How to fix it?










share|improve this question




















  • 1





    Try use Assert.Throws<ArgumentOutOfRangeException>(() => your function)

    – Johnny
    Jan 3 at 8:11











  • I tried this: but Test is still failing! Assert.Throws<ArgumentOutOfRangeException>(() => CheckNumberTest) @johnny

    – Rehan Shah
    Jan 3 at 8:16
















2












2








2


1






I have a situation that at some case the actual function throwing an exception and i written unit test for it but unfortunately the unit test is failed.



Sample Code:



// 'CheckNumber()' function is Present in 'Number' class.

public int CheckNumber(int Number)
{
if (Number < 0 || Number > MaxNumber) // MaxNumber = 300
throw new ArgumentOutOfRangeException();

//..
}


Unit Test:



I am using NUnit Framework.



// When The Number is Less than Zero Or Greater than Maximun Number

[Test]
public void CheckNumberTest()
{
Number number = new Number();
int returnedValue = number.CheckNumber(-1);

// Assertion.
Assert.That(returnedValue , Throws.ArgumentOutOfRangeException);
}


This Test is failing when i run test. This test is actually throwing the exception and TestMethod is going to break? So How to fix it?










share|improve this question
















I have a situation that at some case the actual function throwing an exception and i written unit test for it but unfortunately the unit test is failed.



Sample Code:



// 'CheckNumber()' function is Present in 'Number' class.

public int CheckNumber(int Number)
{
if (Number < 0 || Number > MaxNumber) // MaxNumber = 300
throw new ArgumentOutOfRangeException();

//..
}


Unit Test:



I am using NUnit Framework.



// When The Number is Less than Zero Or Greater than Maximun Number

[Test]
public void CheckNumberTest()
{
Number number = new Number();
int returnedValue = number.CheckNumber(-1);

// Assertion.
Assert.That(returnedValue , Throws.ArgumentOutOfRangeException);
}


This Test is failing when i run test. This test is actually throwing the exception and TestMethod is going to break? So How to fix it?







c# unit-testing nunit






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 8:26









Johnny

3,2101021




3,2101021










asked Jan 3 at 8:04









Rehan ShahRehan Shah

707116




707116








  • 1





    Try use Assert.Throws<ArgumentOutOfRangeException>(() => your function)

    – Johnny
    Jan 3 at 8:11











  • I tried this: but Test is still failing! Assert.Throws<ArgumentOutOfRangeException>(() => CheckNumberTest) @johnny

    – Rehan Shah
    Jan 3 at 8:16
















  • 1





    Try use Assert.Throws<ArgumentOutOfRangeException>(() => your function)

    – Johnny
    Jan 3 at 8:11











  • I tried this: but Test is still failing! Assert.Throws<ArgumentOutOfRangeException>(() => CheckNumberTest) @johnny

    – Rehan Shah
    Jan 3 at 8:16










1




1





Try use Assert.Throws<ArgumentOutOfRangeException>(() => your function)

– Johnny
Jan 3 at 8:11





Try use Assert.Throws<ArgumentOutOfRangeException>(() => your function)

– Johnny
Jan 3 at 8:11













I tried this: but Test is still failing! Assert.Throws<ArgumentOutOfRangeException>(() => CheckNumberTest) @johnny

– Rehan Shah
Jan 3 at 8:16







I tried this: but Test is still failing! Assert.Throws<ArgumentOutOfRangeException>(() => CheckNumberTest) @johnny

– Rehan Shah
Jan 3 at 8:16














2 Answers
2






active

oldest

votes


















1














The problem here is that your method doesn't return any value but instead throws exception.



int returnedValue = number.CheckNumber(-1); //throws ArgumentOutOfRangeException


The test code executes as every other peace of code and it will bubble exception up before someone catch it. In your case it was caught by test executor because you do not have any try/catch block here.



The proper way to write your test is to use Assert.Throws<TException>.



[Test]
public void CheckNumberTest()
{
//Arrange
Number number = new Number();

//Act
var throws = new TestDelegate(() => number.CheckNumber(-1));

//Assert.
Assert.Throws<ArgumentOutOfRangeException>(throws);
}





share|improve this answer































    1














    Please check documentation here!



    and you will of course need to understand variances between exceptions in the same link
    this should help you thorough



    // Require an ApplicationException - derived types fail!
    Assert.Throws( typeof(ApplicationException), code );
    Assert.Throws<ApplicationException>()( code );

    // Allow both ApplicationException and any derived type
    Assert.Throws( Is.InstanceOf( typeof(ApplicationException), code );
    Assert.Throws( Is.InstanceOf<ApplicationException>(), code );

    // Allow both ApplicationException and any derived type
    Assert.Catch<ApplicationException>( code );

    // Allow any kind of exception
    Assert.Catch( code )





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54018422%2fthrowingexception-causing-unittest-break%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      The problem here is that your method doesn't return any value but instead throws exception.



      int returnedValue = number.CheckNumber(-1); //throws ArgumentOutOfRangeException


      The test code executes as every other peace of code and it will bubble exception up before someone catch it. In your case it was caught by test executor because you do not have any try/catch block here.



      The proper way to write your test is to use Assert.Throws<TException>.



      [Test]
      public void CheckNumberTest()
      {
      //Arrange
      Number number = new Number();

      //Act
      var throws = new TestDelegate(() => number.CheckNumber(-1));

      //Assert.
      Assert.Throws<ArgumentOutOfRangeException>(throws);
      }





      share|improve this answer




























        1














        The problem here is that your method doesn't return any value but instead throws exception.



        int returnedValue = number.CheckNumber(-1); //throws ArgumentOutOfRangeException


        The test code executes as every other peace of code and it will bubble exception up before someone catch it. In your case it was caught by test executor because you do not have any try/catch block here.



        The proper way to write your test is to use Assert.Throws<TException>.



        [Test]
        public void CheckNumberTest()
        {
        //Arrange
        Number number = new Number();

        //Act
        var throws = new TestDelegate(() => number.CheckNumber(-1));

        //Assert.
        Assert.Throws<ArgumentOutOfRangeException>(throws);
        }





        share|improve this answer


























          1












          1








          1







          The problem here is that your method doesn't return any value but instead throws exception.



          int returnedValue = number.CheckNumber(-1); //throws ArgumentOutOfRangeException


          The test code executes as every other peace of code and it will bubble exception up before someone catch it. In your case it was caught by test executor because you do not have any try/catch block here.



          The proper way to write your test is to use Assert.Throws<TException>.



          [Test]
          public void CheckNumberTest()
          {
          //Arrange
          Number number = new Number();

          //Act
          var throws = new TestDelegate(() => number.CheckNumber(-1));

          //Assert.
          Assert.Throws<ArgumentOutOfRangeException>(throws);
          }





          share|improve this answer













          The problem here is that your method doesn't return any value but instead throws exception.



          int returnedValue = number.CheckNumber(-1); //throws ArgumentOutOfRangeException


          The test code executes as every other peace of code and it will bubble exception up before someone catch it. In your case it was caught by test executor because you do not have any try/catch block here.



          The proper way to write your test is to use Assert.Throws<TException>.



          [Test]
          public void CheckNumberTest()
          {
          //Arrange
          Number number = new Number();

          //Act
          var throws = new TestDelegate(() => number.CheckNumber(-1));

          //Assert.
          Assert.Throws<ArgumentOutOfRangeException>(throws);
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 3 at 8:20









          JohnnyJohnny

          3,2101021




          3,2101021

























              1














              Please check documentation here!



              and you will of course need to understand variances between exceptions in the same link
              this should help you thorough



              // Require an ApplicationException - derived types fail!
              Assert.Throws( typeof(ApplicationException), code );
              Assert.Throws<ApplicationException>()( code );

              // Allow both ApplicationException and any derived type
              Assert.Throws( Is.InstanceOf( typeof(ApplicationException), code );
              Assert.Throws( Is.InstanceOf<ApplicationException>(), code );

              // Allow both ApplicationException and any derived type
              Assert.Catch<ApplicationException>( code );

              // Allow any kind of exception
              Assert.Catch( code )





              share|improve this answer




























                1














                Please check documentation here!



                and you will of course need to understand variances between exceptions in the same link
                this should help you thorough



                // Require an ApplicationException - derived types fail!
                Assert.Throws( typeof(ApplicationException), code );
                Assert.Throws<ApplicationException>()( code );

                // Allow both ApplicationException and any derived type
                Assert.Throws( Is.InstanceOf( typeof(ApplicationException), code );
                Assert.Throws( Is.InstanceOf<ApplicationException>(), code );

                // Allow both ApplicationException and any derived type
                Assert.Catch<ApplicationException>( code );

                // Allow any kind of exception
                Assert.Catch( code )





                share|improve this answer


























                  1












                  1








                  1







                  Please check documentation here!



                  and you will of course need to understand variances between exceptions in the same link
                  this should help you thorough



                  // Require an ApplicationException - derived types fail!
                  Assert.Throws( typeof(ApplicationException), code );
                  Assert.Throws<ApplicationException>()( code );

                  // Allow both ApplicationException and any derived type
                  Assert.Throws( Is.InstanceOf( typeof(ApplicationException), code );
                  Assert.Throws( Is.InstanceOf<ApplicationException>(), code );

                  // Allow both ApplicationException and any derived type
                  Assert.Catch<ApplicationException>( code );

                  // Allow any kind of exception
                  Assert.Catch( code )





                  share|improve this answer













                  Please check documentation here!



                  and you will of course need to understand variances between exceptions in the same link
                  this should help you thorough



                  // Require an ApplicationException - derived types fail!
                  Assert.Throws( typeof(ApplicationException), code );
                  Assert.Throws<ApplicationException>()( code );

                  // Allow both ApplicationException and any derived type
                  Assert.Throws( Is.InstanceOf( typeof(ApplicationException), code );
                  Assert.Throws( Is.InstanceOf<ApplicationException>(), code );

                  // Allow both ApplicationException and any derived type
                  Assert.Catch<ApplicationException>( code );

                  // Allow any kind of exception
                  Assert.Catch( code )






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 3 at 8:18









                  M.ElfekyM.Elfeky

                  6612




                  6612






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54018422%2fthrowingexception-causing-unittest-break%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Monofisismo

                      Angular Downloading a file using contenturl with Basic Authentication

                      Olmecas