Throw exception in test method using MSTest

Multi tool use
Multi tool use


Throw exception in test method using MSTest



I'm using MSTest to write test cases for my application.
I have a method where files are moved from one directory to another directory. Now when I run code coverage, it shows that the catch block is not covered in code coverage.
This is my code as below.


class Class1
{
public virtual bool MoveFiles( string fileName)
{
bool retVal = false;
try
{
string sourcePath = "PathSource";
string destinationPath = "DestPath";
if (Directory.Exists(sourcePath) && Directory.Exists(destinationPath))
{
string finalPath = sourcePath + "\" + fileName ;
if (Directory.Exists(finalPath))
{
File.Move(finalPath, destinationPath);
retVal = true;
}
}
}
catch (Exception ex)
{
LogMessage("Exception Details: " + ex.Message);
retVal = false;
}
return retVal;
}
}



The test method for the above code is this.


[TestMethod()]
public void MoveFilesTest()
{
string filename = "test";
Class1 serviceObj = new Class1();
var result = serviceObj.MoveFiles(filename);
Assert.IsTrue(result);
}



When I run my code coverage, it shows only the try block is covered and not the catch block. So in order to do that, I need to write another test method and generate an exception and test method will look something like this.


[TestMethod()]
public void MoveFilesTest_Exception()
{
string filename = "test";
Class1 serviceObj = new Class1();
ExceptionAssert.Throws<Exception>(() => serviceObj.MoveFiles(filename));
}



Can anyone help to create an exception for this code as I couldn't do that or at least guide me how to do it?
Many thanks!





It looks like you are writing unit tests. In unit tests you normally try to avoid relying on concrete parts like the filesystem. Anyway, you could mock the directory class and set it up to throw an exception on calling in your second test method.
– royalTS
Jul 2 at 6:20





You would have to create a situation where File.Move throws. I would suggest creating another collaborator for this and stub it to throw. If that's not possible I'd try to open the file in test and then try to move it. Note that your assert will still not work because you catch and not re-throw any exceptions, but your catch would be covered if you assert on false. Note also that this is kind of integration tests. In unit tests you would avoid talking to file system / DB/ external services
– DevNewb
Jul 2 at 6:22





1 Answer
1



You can use the Expected Exception Attribute in your tests to indicate that an exception is expected during execution.



The following code will test for invalid characters in the filename and should raise an ArgumentException since > is an invalid character in filenames:


>


[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void InvalidCharacterInFileNameTest()
{
string filename = "test>";
Class1 serviceObj = new Class1();
serviceObj.MoveFiles(filename);
}



Update:



Since the Directory.Exists() 'supresses' any exception that might occur, you also need to change the code in your function to throw an exception if the source file does not exist or is invalid.


Directory.Exists()



This is just an example to show how it can be implemented but your code could look similar to this:


public virtual bool MoveFiles(string fileName)
{
bool retVal = false;
try
{
string sourcePath = "PathSource";
string destinationPath = "DestPath";
if (Directory.Exists(sourcePath) && Directory.Exists(destinationPath))
{
string finalPath = sourcePath + "\" + fileName;
if (Directory.Exists(finalPath))
{
File.Move(finalPath, destinationPath);
retVal = true;
}
else
{
throw new ArgumentException("Source file does not exists");
}
}
}
catch (Exception ex)
{
LogMessage("Exception Details: " + ex.Message);
retVal = false;
}
return retVal;
}





Thanks for the reply. My apologies for declaring the variable name wrongly. This is basically folder name itself. string filename ="test". I have changed this and checked. But it is skipping the if condition i.e "if (Directory.Exists(finalPath))" and proceeds further. It doesn't throw any exception.
– CrazyCoder
Jul 2 at 9:13






See the updated answer
– S.Dav
Jul 2 at 9:43





@S.Dav but you are still swallowing all exceptions with that catch
– DevNewb
Jul 2 at 18:33






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

w7ZYxRbWQNpUZ,BnYxAOjs 3,kk,aFbKT,wxqI tuz7wjK2
MYy 1So9Ycj,2RyOA7 HnKm3 JhPa q qzE24MQk7MzFG59,ganNyysV3wD0a,IThaQo

Popular posts from this blog

Rothschild family

Cinema of Italy