Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception in MS Unit Test?

I created a unit test for a method of my project. That method raises an exception when a file is not found. I wrote a unit test for that, but I'm still not able to pass the test when the exception is raised.

Method is

public string[] GetBuildMachineNames(string path)
{
    string[] machineNames = null;

    XDocument doc = XDocument.Load(path);

    foreach (XElement child in doc.Root.Elements("buildMachines"))
    {
        int i = 0;
        XAttribute attribute = child.Attribute("machine");
        machineNames[i] = attribute.Value;
    }
    return machineNames;
}

Unit Test

[TestMethod]
[DeploymentItem("TestData\\BuildMachineNoNames.xml")]
[ExpectedException(typeof(FileNotFoundException),"Raise exception when file not found")]
public void VerifyBuildMachineNamesIfFileNotPresent()
{
    var configReaderNoFile = new ConfigReader();
    var names = configReaderNoFile.GetBuildMachineNames("BuildMachineNoNames.xml");
}

Should I handle the Exception in the method or am I missing something else??

EDIT:

The path I am passing is not the one to find the file, so this test should pass... i.e. what if file not exists in that path.

like image 976
PawanS Avatar asked Feb 04 '11 10:02

PawanS


People also ask

What are exceptions in JUnit?

Advertisements. JUnit provides an option of tracing the exception handling of code. You can test whether the code throws a desired exception or not. The expected parameter is used along with @Test annotation.

Should exceptions be unit tested?

Unit test cases for exceptions should improve the stability and robustness of your application. Unit Test cases can ensure of proper exception handling is implemented.

Can we throw exception in JUnit test?

When using JUnit 4, we can simply use the expected attribute of the @Test annotation to declare that we expect an exception to be thrown anywhere in the annotated test method. In this example, we've declared that we're expecting our test code to result in a NullPointerException.


2 Answers

In your unit test it seems that you are deploying an xml file: TestData\BuildMachineNoNames.xml which you are passing to the GetBuildMachineNames. So the file exists and you cannot expect a FileNotFoundException to be thrown. So maybe like this:

[TestMethod]
[ExpectedException(typeof(FileNotFoundException), "Raise exception when file not found")]
public void VerifyBuildMachineNamesIfFileNotPresent()
{
    var configReaderNoFile = new ConfigReader();
    var names = configReaderNoFile.GetBuildMachineNames("unexistent.xml");
}
like image 183
Darin Dimitrov Avatar answered Oct 16 '22 20:10

Darin Dimitrov


By putting [ExpectedException(typeof(FileNotFoundException),"Raise exception when file not found")] attribute you are expecting that the method will throw an FileNotFoundException, if the FileNotFoundException not thrown Test will fail. Otherwise Test will be success.

like image 23
Anuraj Avatar answered Oct 16 '22 19:10

Anuraj