Book Image

Mockito for Spring

By : Sujoy Acharya
Book Image

Mockito for Spring

By: Sujoy Acharya

Overview of this book

Table of Contents (12 chapters)
Mockito for Spring
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Mocking an environment


The Environment interface and the PropertySource class were added to Spring 3.1 in order to simplify working with properties. In Spring 3.2, MockEnvironment and MockPropertySource were added to the mock properties in tests. We'll create a program to configure a bean from a properties file value and then mock out the properties file value with MockEnvironment and MockPropertySource.

The following are the steps:

  1. Create a myProp.properties properties file under the test source folder and add the following property:

    message = I'm the king
  2. You can define a Spring configuration context by annotating a class with the @Configuration annotation. The @PropertySource annotation takes the properties' filenames and sets the properties to the Environment resource. Create a MyConfig configuration class under the com.packt.environment package. The following is the configuration class:

    @Configuration
    @PropertySource({"classpath:myProp.properties"})
    public class MyConfig {
    
      @Resource...