Tuesday, July 5, 2011

NetBeans SpringMVC (REST) Project Junit testing: Errors & Resolutions

This blog is a consolidated view of the problems that are being faced while executing JUNIT based test cases for a  Spring MVC based web project in Netbeans.
The project environment is:
1. NetBeans 6.9
2. JDK 6u13
3. Tomcat 6.0.32
4. Windows XP



Problems faced while executing Junit test cases:
1. Memory problems while executing test cases
2. A number of properties are being exposed with the help of catalina.properties like the proxy username and password need not to be hardcoded in the applicationcontext files & a number of different properties files (like cache.properties, dbdetails.properties)



The test package "Test Packages" contains all the Junit test cases. 




An Example of a single test case is
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(
{
        "classpath*:/web/WEB-INF/applicationContext.xml",
        "classpath*:/web/WEB-INF/dispatcher-servlet.xml"
}) 
public class DaoTest {  
// JUNIT Specific Code
}
Since our project requires a number of properties file that are being configured through catalina.properties like logback configuration file path, dbdetails properties file path etc. (since we are loading all the properties defined in all of these properties file through Spring PropertyPlaceHolderConfigurer) which is provided automatically at runtime while running the application under TOMCAT, here the challenge is to provide all those properties through an external medium to the spring junit runner, so that the Spring application context is being initialized successfully.


For This Go to NetBeans menu: 
a. Tools -> Options  -> ANT Tab
b. Look for the properties at the bottom of the window.



For each of the properties that you have externalize by making use of catalina.properties we need to enter a line here in the PROPERTIES box.
E.g. Suppose we have a propery named “https.proxyPort” defined in the catalina.properties then we need to define a property here also, however we need to keep in mind that each property needs to be prepended by “test-sys-prop.”



      So the final property is test-sys-prop.https.proxyPort=1971 as shown in above diagram.
Similarly if we have externalize the Log4j property file it will be
test-sys-prop.logback.configurationFile=file\:///c\:/logback.xml
NOTE:
The “\” entered above is automatically added by netbeans we just need to add the following.
test-sys-prop.logback.configurationFile=file:///c:/logback.xml



Now Click on “OK” and netbeans will start building the indices for internal purposes.
Start Running your Junit Test Cases and Spring will initialize properly as in the TOMCAT.  And we can get access to all our beans by using @Autowired annotation in the test Cases.



Memory Problem:
Now if we are doing some memory intensive work, we need to provide netbeans with the –Xms and –Xmx runtime arguments. Otherwise we will keep on getting OutofMemoryError : heap space. To remove this:
Right Click on the project -> Properties -> Run


Place the –Xms & -Xmx parameter as per your requirements.

Hope it helps you to do the UNIT Testing in NetBeans. For this blog, I have taken help of a number of blogs,the main one is :http://forums.netbeans.org/post-294.html

No comments:

Post a Comment