# Spring JUnit Example

In 
Published 2022-12-03

This tutorial explains to you how to use JUnit for testing in Spring. We are using Eclipse as IDE.

JUnit is a unit testing framework for the Java programming language. JUnit is important also in test-driven development (when you start the projects with the tests and after that you code the project).

In order to use and to do some tests with JUnit in Spring, you have to create a simple Spring application and configure it to use Maven (Maven is mandatory only to follow my example).

Here is the pom.xml file dependencies:

Now we have to create the following classes:

package com.example.config;
 
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
 
@Configuration
@ComponentScan("com.example.*")
public class SpringContextConfig {
 
}
package com.example.services;
 
import org.springframework.stereotype.Service;
 
@Service
public class MyServiceA {
 
    public int getNumber1(int val1) {
         
        return val1*10;
    }
}
package com.example.main;
 
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.example.config.SpringContextConfig;
import com.example.services.MyServiceA;
 
// Class Main for Spring JUnit tests example
public class Main {
 
    public static void main (String [] args) {
         
        try (AnnotationConfigApplicationContext context 
                  = new AnnotationConfigApplicationContext(SpringContextConfig.class)) {
                 
             System.out.println("Spring context created ...");
              
             //GET an instance from context BY CLASS
             // SINGLETON by default in Spring
             MyServiceA myServiceA =  context.getBean(MyServiceA.class);
              
             int result1 = myServiceA.getNumber1(4);
              
             System.out.println("Result1="+result1);
        }
    }
}
package com.example.tests.MyServiceA;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Repeat;
import org.springframework.test.annotation.Timed;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.util.Assert;

import com.example.config.SpringContextConfig;
import com.example.services.MyServiceA;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=SpringContextConfig.class)
public class TestMyServiceA {

  @Autowired
  private MyServiceA myServiceA;

  @Test
  public void checkMyServiceAInjection() {
    Assert.notNull(myServiceA, "myServiceA is not injected");
  }

  @Test
  public void checkIfGetNumber1Is40() {
    int result1 = myServiceA.getNumber1(4);
    Assert.isTrue(result1 == 40, "The Result1 is not 40");
  }

  @Test
  @Repeat(10)
  @Timed(millis=1000)
  public void checkIfGetNumber1IsGreater10() {
    int result1 = myServiceA.getNumber1(4);
    Assert.isTrue(result1 > 10, "The Result1 is not > 40");
  }

}

When I run the application as a Java Application I receive:

but when I run the application with JUnit I receive:

You can see that all the tests are passed.