Posts

Showing posts from October, 2013

How to substitute dynamic placeholder in properties file

This blog will explain you how can you use replace placeholder with original value in properties file. To achieve this task we will you Java API MessageFormat . Suppose you have properties file named "welcome.properties" having message : welcome=Hi {0} , Welcome to {1} Sample :  import java.io.File; import java.io.FileInputStream; import java.text.MessageFormat; import java.util.Properties; /**  * @author abdul  *  */ public class DynamicPlaceholder { /** * @param args * @throws Exception  */ public static void main(String[] args) throws Exception { File propFile = new File("D:\\juno\\Practise\\src\\com\\waheed\\dynamic\\placeholder\\substitution\\welcome.properties"); Properties props = new Properties(); FileInputStream stream=new FileInputStream(propFile); props.load(stream); String message = props.getProperty("welcome");                // Here the {0} and {1} will be substi...

Junit Test with Mockito

Mockito is a testing framework for Java which allows the creation of Test Double objects called "Mock Objects" for use in automated unit testing in conjunction with framework like Junit.  For more details, Check here . This blog will show you step by step working of Mockito. Step 1 : You need mockito-all and Junit jars into you project classpath which you can download it from here . If you are using maven, add following dependency into your pom.xml file. <dependency>             <groupid>org.mockito</groupid>             <artifactid>mockito-all</artifactid>             <version>1.9.5</version> </dependency> <dependency>             <groupid>junit</groupid>             <artifactid...

Testing REST Client using MockRestServiceServer

Problem : I have a Web Application which is deployed on tomcat server and I needed to write unit test cases which can test all the controllers. Secondly It internally also hits another Web server using RestTemplate that means for the success of all unit test cases both the applications should be up and running.    Solution : After spending some time on Google, I found that Spring provide a new feature “MockRestServiceServer” by which testing a REST Client is simple. This new feature is in Spring 3.2.x but also available in Spring 3.1.x via the extra jar named “spring-test-mvc”. So, You can test your Spring MVC controllers very easy without starting any Web Application. MockRestServiceServer takes the approach of mocking the server and allowing you to specify expected behavior and responses in your junit test class. It also handles server exception classes. MockRestRequestMatchers offers many hamcrest matchers to check your request URL, headers, HTTP method, an...

How to create Spring MVC project using Maven and Eclipse

Image
This blog will show you how quickly you can  create a Spring MVC project and get it up and running, using the Maven archetype called spring-mvc-archetype . Note: First You should verify that the Maven Integration for FTP is already installed in your eclipse, If not first installed and then create a new project. Steps : In Eclipse IDE, Goto  File > New > Project Select  Maven > Maven Project and click  Next . Make sure you don’t check the option Create a simple project (skip archetype selection) , and click Next . In the next screen, Select Catalog as All Catalogs , Archetype as spring-mvc into the Filter   and select  maven-archetype-webapp in the artifact list as shown below :     In case, If you don't see the above artifact in your Archtype then Click on "Add Archetype" and Add : Archetype Group Id: co.ntier Archetype Artifact Id: spring-mvc-archetype Archetype Version: 1.0.2 Repository URL: http:...