Link to home
Start Free TrialLog in
Avatar of jazzIIIlove
jazzIIIloveFlag for Sweden

asked on

Unit Testing a C# or Java function in NUnit or JUnit which doesn't take an input argument. How to

Hi there;

I have been trying to test my functions which I wrote in C# and Java, but when the function takes no input argument and calls other functions in it. I have no clue how I can arrange the test functions. This is same in C# and Java. I am using NUnit and JUnit respectively.

I have 3 basic problems actually:
1) What if the function to be tested receives no input arguments
2) What if the function to be tested calls other functions (should I test the other functions individually or within the target function?)
3) No return values or global variables such as ArrayLists or Dictionaries..

Following code covers, the first 2. I want to test some dummy files apart from the List.txt file, but I don't know how I can set the test case since the filepath is not passed and is not even global to the class..

 public bool Read()
        {
            string line;
            alist = new SortedDictionary<string, string>();

            using (StreamReader sr = new StreamReader("List.txt"))
            {
                while ((line = sr.ReadLine()) != null)
                {
                    try
                    {
                        // Splitting line into atoms where the first atom represents prefix, and third atom represents a operator numerical code                                                        
                        if (!line.StartsWith("O"))
                        {
                            string[] atoms = line.Split(new char[] { ' ', '\t' });
                            Prefix = atoms[0];
                            Price = atoms[2];
                            Search();
                        }
                        else
                        {
                            Display();
                            alist.Clear();
                            Letter = line[line.Length - 2];
                        }
                    }
                    catch (FileNotFoundException e)
                    {
                        return false;
                    }
                }
                sr.Close();
                return true;
            }
        }

Open in new window


Test case is as follows:

 [Test]
        public void CanRead_Absolute_Path()
        {
            // Arrange
----------->            string path = "ListEmpty.txt";  I cannot pass the file name...What should I do? Should I read it?
            var target = new Routing();
            var expected = true;

            // Actual
            var actual = target.Read();
----------->when you check above read function, there are display function and a arraylist clearing...How can I populate this?
            // Assert
            Assert.AreEqual(expected, actual);
        }

Open in new window


Regards.
SOLUTION
Avatar of topdog770
topdog770
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jazzIIIlove

ASKER

Hi;

Thanks for the comment. Ok, let's see. Following is another code segment, this time in Java and Junit:

It's straight forward, reads a file and inside this function, another function call is made.

public boolean read(String filePath)
	{
		try{
			// Open the file that is the first command line parameter
			FileInputStream fstream = new FileInputStream(filePath);

			// Get the object of DataInputStream
			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));

			String strLine;
			//Read File Line By Line
			while ((strLine = br.readLine()) != null)   {
				split(strLine);				
			}			
			//Close the input stream
			in.close();
			return true;

		}catch (Exception e){//Catch exception if any
			System.out.println("Error: " + e.getMessage());
			return false;
		}
	}

Open in new window


The problem is that split() function causes null pointer exceptions. I just want to skip that line as nothing happens in JUnit. Possible? I googled a lot but couldn't find out any example. Can you help?

Is it the "mock"ing thing in testing world?

Regards.
Hi as an update;
I installed mockito jar and try to invoke doNothing function as follows:
      public void readTestRegularTextFile()
      {
            // Arrange
            Routing r = new Routing();            //read and split functions reside in this class
            Mockito.doNothing().when(r).split("");            
            boolean actual = r.read("List.txt");
            boolean expected = true;

The problem is that it's not working, and moreover, the split(..) function content changes for every line read. So, what to do? I am just trying to proof the files are read.

Regards.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Good points. Thanks.