How to avoid hardcodings while testing
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Avinash_Salimath
Posted On:   Monday, September 8, 2003 12:03 AM

How to avoid hardcodings in test classes. For example, if I need to test a method which takes a filename as input and returns a generated filename along with the folder name -- public class Class1 { public String method1(String strFileName) throws Exception { //........ } } and in the test class public void testMethod1() throws Exception { String actual = new Class1().method1("filename"); String expected = "folder1\filename.ext"; assertEquals("Both are equal",actual,expected); } In the test case I want to avoid the filename.    More>>

How to avoid hardcodings in test classes. For example, if I need to test a method which takes a filename as input and returns a generated filename along with the folder name --


public class Class1 {

public String method1(String strFileName) throws
Exception {

//........

}

}


and in the test class

public void testMethod1() throws Exception {

String actual = new Class1().method1("filename");

String expected = "folder1\filename.ext";

assertEquals("Both are equal",actual,expected);

}


In the test case I want to avoid the filename.

   <<Less

Re: How to avoid hardcodings while testing

Posted By:   Karthik_Jagadeesan  
Posted On:   Tuesday, October 28, 2003 11:01 PM

You could try the JTestCase framework.You could test your method with any number of data using the framework.You could visit the site - jtestcase.sourceforge.net/htmls/main.html.

Re: How to avoid hardcodings while testing

Posted By:   Naveen_Punjabi  
Posted On:   Tuesday, October 21, 2003 05:52 PM


Well there has to be hard coding at some point or the
other. Junit is a Unit testing framework and you need to
provide some relevant data to get the output (which
needs to be verified). Actually, in the Test case file
itself you are doing hard coding at 2 points

1.The value that is the Input to the method to be tested


2.The expected value against which you check your output
from the method to be tested. e.g. "String expected
= "folder1\filename.ext";" in your code.


What you can do is take is hardcoding to a layer above,
by writing a "PROCEDURE" in your "Class1" that takes
hard codec values from your console (or whatever) and
sets the corresponding parameters in the Test case
file. This would remove explicit hard coding in the
test case file.

But here care should be taken that your parameters are
correctly initiallized before the invocation of each
and every test method. You might want to put
you "PROCEDURE" in the Init method to reduce some risk.


Thanks,
Naveen Punjabi
www-scf.usc.edu/~npunjabi
About | Sitemap | Contact