Unit testing - Java-Springs

Dependency Injection should make your code less dependent on the container than it would be with traditional Java EE development.The POJOs that make up your application should be testable in JUnit or TestNG tests, with objects simply instantiated using the new operator, without Spring or any othercontainer.You can use mock objects (in conjunction with other valuable testing techniques) to test your code in isolation.If you follow the architecture recommendations for Spring, the resulting clean layering and componentization of your codebase will facilitate easier unit testing.For example, you can test service layer objects by stubbing or mocking DAO or Repository interfaces, without needing to access persistent data while running unit tests.

True unit tests typically run extremely quickly, as there is no run time infra structure to set up. Emphasizing true unit tests as part of your development methodology will boost your productivity.You may not need this to help you write effective unit tests for your IoC-based applications. For certain unit testing scenarios, however, the Spring Frame work provides the following mock objects and testing support classes.

Mock objects

JNDI

The org. spring frame work. mock. jndi package contains an implementation of the JNDI SPI, which you can use to set up a simple JNDI environment for test suites or stand-alone applications. If, for example, JDBC Data Sources get bound to the same JNDI names in test code as within a Java EE container, you can reuse both application code and configuration in testing scenarios without modification.

Servlet API

The org. spring frame work. mock. web package contains a comprehensive set of Servlet API mock objects, targeted at usage with Spring's Web MVC framework, which are useful for testing web contexts and controllers. These mock objects are generally more convenient to use than dynamic mock objects such as EasyMock or existing Servlet API mock objects such as MockObjects.

Portlet API

The org. spring frame work. mock. web. portlet package contains a set of Portlet API mock objects, targeted at usage with Spring's Portlet MVC framework.

Unit testing support classes

General utilities

The org. spring frame work. test.util package contains Reflection Test Utils, which is a collection of reflection-based utility methods. Developers use these methods in unit and integration testing scenarios in which they need to set a non-public field or invoke a non-public setter method when testing application code involving, for example:

  • ORM frameworks such as JPA and Hibernate that condone private or protected field access as opposed to public setter methods for properties in a domain entity.
  • Spring's support for annotations such as @Autowired and @Resource, which provides dependency injection for private or protected fields, setter methods, and configuration methods

Spring MVC

The org. spring frame work. test. web package contains Model And View Assert, which you can use in combination with JUnit 4+, TestNG, and so on for unit tests dealing with Spring MVC Model And View objects.

Unit testing Spring MVC Controllers

To test your Spring MVC Controllers, use Model And View Assert combined with Mock HttpS ervlet Request, Mock Http Session, and so on from the org.spring frame work .mock.web package.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Java-Springs Topics