Using TestNG on Spring Framework and Hibernate Test Case
Dear readers, at the time I wrote this article, I just have 2 months of being an employee at the IT Consultant Company in Jakarta, Indonesia. But that’s not what I want to share in this article. In this company, I was expected to follow the java development stack which is applied in this company. The Spring Framework and Hibernate are include.
I’ve been 6 years in Java programming language, but I admit, I never involve Hibernate framework as an ORM(Object Relational Mapping) method on my projects development stack. While Spring Framework, I’ve been using it since 2 years ago. This condition force me to do a little adaptation of this Hibernate Framework.
I often met some difficulties which is stress me so much, when the codes of the dao layer using a hibernate criteria. For a few developers who always using hibernate framework, it should be easier to use hibernate criteria, but for me, it really stress me so much.
To find a method on a dao layer work or not, sometimes it wouldn’t so effective to test it by deploying whole application just only to test one method. And I decide to use unit-test when testing dao layer methods, so I don’t need to deploy whole application.
Okay, below is the step to create a unit-test in order to test the methods on dao layer which created using Spring Framework and hibernate:
- Create a hibernate configuration file, Spring Context file, and all those supported files, and put them onto test/resources folder just like the picture below:

- Create a file called TestDao, and put it onto test/java folder. The codes of the files is below:
package com.mojo.maven.test;
import com.mojo.maven.dao.NewsCategoryDao;
import com.mojo.maven.dao.NewsDao;
import com.mojo.maven.model.News;
import com.mojo.maven.model.NewsCategory;
import org.apache.log4j.Logger;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.hibernate.context.ManagedSessionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
@ContextConfiguration(locations={"classpath*:maven1ApplicationContext-test.xml"})
public class TestDao extends AbstractTestNGSpringContextTests{
private Logger logger = Logger.getLogger(TestDao.class);
@Autowired
public SessionFactory sessionFactory;
private Session session;
@Autowired
private NewsDao newsDao;
@Autowired
private NewsCategoryDao newsCategoryDao;
/**
* Inisialisasi hibernate context sebelum test
*/
@BeforeMethod
void init() {
logger.debug("init session factory");
session = sessionFactory.openSession();
ManagedSessionContext.bind(session);
}
/**
* cleanup hibernate context setelah test
*/
@AfterMethod
void destroy() {
logger.debug("destroy session factory");
session.close();
ManagedSessionContext.unbind(sessionFactory);
}
@Test
public void testGetNews(){
News news = newsDao.getById(1);
logger.info("News ID : " + news.getId());
logger.info("News Title : " + news.getNewsTitle());
logger.info("News URL : " + news.getNewsUrl());
logger.info("News Category : " + news.getNewsCategory().getCategoryName());
logger.info("News Category Description : " + news.getNewsCategory().getCategoryDescription());
}
}
What we have to pay attention on the class are, the class have an annotation @ContextConfiguration and extends AbstractTestNGSpringContextTests class which is provide as a base class to use it as a test class for TestNG Framework. Also on the annotation @ContextConfiguration defined the location of the Spring Context, so the compiler can find a Spring Context file when the execution is triggered.
The next thing which have to note are 2 methods which is defined on the class, those are void init() method and void destroy() method. void init() method, given an annotation @BeforeMethod which have a function as an hibernate context initializations before all other the test method executed. While void destroy() method given an annotation @AfterMethod which have a function to close a hibernate session after all test method executed.
Well, that’s all that I can share for this time. If you feel the codes need more improvement, so go head, to make it perfect.
I’ll hope it usefull.
Josescalia
———————————————-
The quick brown fox jump over the lazy dog

One day, I read, that this sentence “the quick brown fox jump over the lazy dog” contains all the character of the alphabet, just because I’m so curious, I thought to create a simple java application that checks whether the sentence is actually using all the letters in the alphabet or not.
The pseudo code of the application will look like this:
- Declare the string.
- Remove all spaces between the words from the sentence.
- Remove duplicate character.
- Convert the string into Character Arrays.
- Sort the Character Arrays.
- Loop the Character Array and print out to console.
First, I need to create a function that can remove duplicate character from a given parameter and the return of the function have to be a string. Ok, here’s the code:
public static String removeDuplicates(String s) {
StringBuilder noDupes = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
String si = s.substring(i, i + 1);
if (noDupes.indexOf(si) == -1) {
noDupes.append(si);
}
}
return noDupes.toString();
}
The function above using StringBuilder object as a container to process removing duplicate character, and the function will return the string formed by StringBuilder object. And then, I will use this function in the application. Ok, here’s the code:
import java.util.Arrays;
/**
* Created by IntelliJ IDEA.
* User: Josescalia
* Date: 7/8/11
* Time: 8:24 PM
* To change this template use File | Settings | File Templates.
*/
public class ArrayTest {
public static void main(String[] args) {
//the string declaration
String sStr = "the quick brown fox jumps over the lazy dog";
//remove the spaces
sStr = sStr.replace(" ", "");
//remove the duplicate character using the function
sStr = removeDuplicates(sStr);
//Convert string into Character Array
char[] a = sStr.toCharArray();
//Sort the Array
Arrays.sort(a);
//let's loop and print out...is is correct, using all the character of the alphabet ????
for (int i = 0; i < a.length; i++) {
char c = a[i];
System.out.print(c);
}
//it's true :P
}
public static String removeDuplicates(String s) {
StringBuilder noDupes = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
String si = s.substring(i, i + 1);
if (noDupes.indexOf(si) == -1) {
noDupes.append(si);
}
}
return noDupes.toString();
}
}
Well, that’s all. Feel free to use and explore for your creations and needs.
I Hope that’s usefull
Thanks
Josescalia
Squid Proxy, and Adzapper in Windows
Browsing website on the internet with high speed data access is a separate pleasure for some people including me. To open a site with no waiting time to load the page of a website is always a dream. But sometimes we’re getting bad-mood if we face a situation where the website that we would like to open are taking too long to load, because it have to wait each elements on the website to download to our computer. Images, swift files and any other elements.
A web page made up-over elements such as images, text, swift files, and others. When we want to open a web page, elements are downloaded to our computer and placed in the temporary folder of the browser we use. We can enjoy a web page perfectly after all the elements that make up the page is finished to download.
We may have a habit to visit the same website every day, especially right now, the social community era, facebook, twitter, myspace, we visit these websites just to make status, commenting a status and other activities. The same images, swift files, will be downloaded every time we visit those websites. This is actually a routine that we can make a strategy about.
Proxy is a tool that we can use to create a strategy for this routine. The proxy has the ability to store images taken from a website that we have accessed before, so the browser no longer need to download the same images from its origin. Proxies are smart enough to recognize the latest content from a website that we access, so proxy will download the latest content and storing it into a place commonly called cache.
Advertising, sometimes we are very disturbed by the ads that float on a websites that we might not want to see it, and the ads usually created using the large size of images. We also can use a proxy to block ads from a website. In addition, the proxy can also be used to block sites.
On a computer network in a company, proxies are necessary nowadays, at least use to save bandwidth and to limit the access to few websites that considered can make the employees forget about work.
The computer network administrators usually install a proxy in linux machine, and for those of you who want to install a proxy on the linux machine, please look for it on Google, there’s a lot of articles around proxy and linux machine. This time we will try to install a proxy into Windows. OK, let’s live it.
Installing Squid Proxy
1. Download squid proxy from this link http://squid.acmeconsulting.it/download/squid-2.6.STABLE23-bin.zip.
2. Then extract it on to C:\
3. And then rename the squid.conf.default file on the etc folder to squid.conf
4. Add this line http_access allow localhost exactly under the line http_access allow localnet
5. Then open the windows console and got to C:\Squid\sbin\, and the type: squid.exe -z command just like picture below:
6. And then run squid as a windows service by typing this command on the console: squid.exe -i. And then check if the squid are installed as a window service like picture below:
7. And then start the Squid Service.
The next step is configuring the browser in order to use the squid as web proxy, the steps are:
1. On firefox, simply click tools and then click option, there have to be a option window appear, and in the Advanced menu, click network tab and then click setting button just like the picture below:

2. And the on the Connection Setting window that appear after the setting button clicked, modify the connection setting just like this picture:

3. And then browse just like we usually do, for the first time the browsing activity will look like usual, because the squid proxy will store the contents of the web onto the cache, and later then the browser will fetch the static content from the squid cache.
Installing Adzap
To block the advertisement on the web, we need a squid plug in, which is adzapper. The problem is adzapper is a perl script that only can be execute by using perl environment. So definitely we need to install perl on our windows system. I choose Strawberry Perl as a perl platform to be installed on my windows machine. Strawberry perl can be fetch for download from this url http://strawberry-perl.googlecode.com/files/strawberry-perl-5.12.1.0.msi
After finished download the strawberry perl, then please install the strawberry pearl on your own windows machine. I have installed Strawberry Perl on C:\Web\ on my windows machine. Please pay an attention that all setting of adzapper must match to the location of your Perl installation.
And the next step is download the adzapper script from this URL: http://adzapper.sourceforge.net/scripts/squid_redirect, open the link on your browser, and after all the text/script is appeared, save the script to the C:\squid\etc\ folder and name it as squid-redirect.pl.
And the next step is configuring this adzapper plug in into the squid the we have installed before. Open the squid.conf file, this file is located on C:\Squid\etc\ folder and then add this line on the end of row of the file
redirect_program C:/Web/strawberry/perl/bin/perl.exe c:/squid/etc/squid_redirect.pl
Please make an attention that my perl installation located on C:\Web\ folder, please adjust the setting to the path of your own perl installation.
And after that, restart the proxy squid by using a window service just like the picture before, or maybe by using the command line by typing squid.exe –k reconfigure. And we have finished the adzapper script installation on squid. Now it’s time to check that the adzapper is working properly or not. Open the browser and then open the web site that you knew it have full of ads. The adzapper will block the ads juts like this picture below, and if it does, then the adzapper is working properly.

Actually there are a lot of things that we can do with squid and adzap to fill up our needs about the web-caching problems, please visit the official site of squid on http://squid-cache.org and adzapper http://adzapper.sourceforge.net/ to have a guide to maximize the both function.
Thanks
I Hope this will helpfull
Josescalia
Installing Squid Proxy
