Posts

Showing posts with the label Junit

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