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
About the Author
About the Reviewers

Printing Hello World

In this section, we'll create a hello world example and set up the Eclipse environment for Spring. You can download the latest Eclipse version from

Note that Spring provides a specific Eclipse distribution for Spring, known as Spring Tool Suite (STS). STS is customized for developing Spring applications. You can download STS from

Download the Spring 4.1.0 JAR from the Maven repository at or

  1. Launch Eclipse and create a Java project and name it SpringOverview.

  2. Add the following dependencies:

  3. Create a com.packt.lifecycle package under src.

  4. Add a HelloWorld class with following details:

    public class HelloWorld {
      private String message;
      public String getMessage() {
        return message;
      public void setMessage(String message) {
        this.message = message;
  5. Add an XML file, applicationContext.xml, directly under the src folder and add the bean definition as follows:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns=""
     <bean id="helloWorld" class="com.packt.lifecycle.HelloWorld">
       <property name="message" value="Welcome to the Spring world">
  6. Create a Java class, HelloWorldExample, and add the following lines to check the bean configuration:

    public class HelloWorldExample {
      public static void main(String[] args) {
        ApplicationContext context = new 
        HelloWorld world = (HelloWorld) 

    We load the Spring bean configuration from an XML file, which is kept in the classpath and named applicationContext.xml, and then ask the context to find a bean with a name or ID as helloWorld. Finally, we call the getMessage() method on the bean to check the value we set in the application context.

  7. When we run the HelloWorldExample program, the following output is displayed: