Posts

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...