Tuesday 18 March 2014

How to put your code under test with Maven, Spring and Junit

So i worked a lot with maven, spring and junit, and on every project i faced the need to have my code under test and so i do each time. an i thought why not have a simple how to do so and so i do this post :D.

so let see what you need to have your own maven project running

  1. JDK 1.6+.
  2. Maven.
  3. IDE or any text editor.
so my pom.xml looks like

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.shereifhawary.spring</groupId>
<artifactId>testing</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<spring.version>3.0.5.RELEASE</spring.version>
</properties>
<dependencies>
<!-- Spring 3 dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- testing dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>

</dependencies>
</project>

and i have a simple model and simple service that use this model

as

package com.shereifhawary.spring;
public class SimpleModel {
private String value = "some value";
public String getValue(){
return this.value;
}
public void setValue(String value) {
this.value = value;
}
}

package com.shereifhawary.spring;
public class SimpleService {
private SimpleModel model;
public SimpleModel getModel() {
return model;
}
public void setModel(SimpleModel model) {
this.model = model;
}
public String getValue(){
return model.getValue();
}
}

inside the src/test/resources/beans.xml which carry the bean definition

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

         <bean id="simpleModel" class="com.shereifhawary.spring.SimpleModel">
             <property name="value" value="some value from Spring config"/>
         </bean>
 
<bean id="simpleService" class="com.shereifhawary.spring.SimpleService">
   <property name="model" ref="simpleModel"/>
</bean>
</beans>


and the test case should be like

package com.shereifhawary.spring.tests;
import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.shereifhawary.spring.SimpleService;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/beans.xml" })
public class TestSpringContext {
         
         // the autowire will set the simpleSerice object with the one in the bean.xml
         // so you can use it inside the testcase without even using setup function
@Autowired
SimpleService simpleService;
@Test
public void testSimpleService(){
                // as you can see we set the value with "some value" in the class itself,
                // but inside the bean.xml we set it with "some value from Spring config
"
assertNotEquals("some value",simpleService.getValue());
assertEquals("some value from Spring config
",simpleService.getValue());
}
}

i hope you like it, if you have any questions please leave it below ;)