jazzIIIlove
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..
Test case is as follows:
Regards.
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;
}
}
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);
}
Regards.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Good points. Thanks.
ASKER
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.
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.