TestNG + Spring Integration Example

In this tutorial, we will show you how to test Spring’s components with TestNG.

Tools used :

  1. TestNG 6.8.7
  2. Spring 3.2.2.RELEASE
  3. Maven 3
  4. Eclipse IDE

1. Project Dependencies

To integrate Spring with TestNG, you need spring-test.jar, add the following :

pom.xml

	<properties>
		<spring.version>3.2.2.RELEASE</spring.version>	
		<testng.version>6.8.7</testng.version>
	</properties>
	
	<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${spring.version}</version>
		</dependency>
			
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>${spring.version}</version>
		</dependency>

		<dependency>
			<groupId>org.testng</groupId>
			<artifactId>testng</artifactId>
			<version>${testng.version}</version>
			<scope>test</scope>
		</dependency>
	</dependencies>

2. Spring Component

Create a simple Spring component, later we will test this component with TestNG.

EmailGenerator.java

package com.mkyong.testng.project.service.email;

public interface EmailGenerator {

	String generate();
	
}
RandomEmailGenerator.java

package com.mkyong.testng.project.service.email;

import org.springframework.stereotype.Service;

@Service
public class RandomEmailGenerator implements EmailGenerator {

	@Override
	public String generate() {
		return "feedback@yoursite.com";
	}

}

3. TestNG + Spring

Create a Spring configuration file in test folder, for Spring components scanning.

${project}/src/test/resources/spring-test-config.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	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.2.xsd
    http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-3.2.xsd
	">

	<context:component-scan base-package="com.mkyong.testng" />

</beans>

To access the Spring components in TestNG, extends AbstractTestNGSpringContextTests, see the following example :

${project}/src/test/java/com/mkyong/testng/examples/spring/TestSpring.java

package com.mkyong.testng.examples.spring;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.Assert;
import org.testng.annotations.Test;
import com.mkyong.testng.project.service.email.EmailGenerator;

@Test
@ContextConfiguration(locations = { "classpath:spring-test-config.xml" })
public class TestSpring extends AbstractTestNGSpringContextTests {

	@Autowired
	EmailGenerator emailGenerator;

	@Test()
	void testEmailGenerator() {

		String email = emailGenerator.generate();
		System.out.println(email);

		Assert.assertNotNull(email);
		Assert.assertEquals(email, "feedback@yoursite.com");
		

	}

}

Output


feedback@yoursite.com
PASSED: testEmailGenerator

===============================================
    Default test
    Tests run: 1, Failures: 0, Skips: 0
===============================================

Download Source Code

Download it – TestNG-Spring-Example.zip (35 KB)

References

  1. Spring – TestNG support classes
  2. Spring AbstractTestNGSpringContextTests JavaDoc

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
11 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
10 Comment authors
ShantaramYerlanJulienSergio CarabettaDigvijay Bele Recent comment authors
newest oldest most voted
trackback
JUnit + Spring integration example

[…] TestNG + Spring Integration Example […]

Sergio Carabetta
Guest
Sergio Carabetta

Spring-context is missing from the dependencies, and you also forgot to mention the needed TestNG Eclipse plug-in. Sorry, but an incomplete example is worse than no example at all.

Shantaram
Guest
Shantaram

Hello sir,
First of all thank you for providing such a beautiful tutorials. Can you please provide a TestNG + Spring MVC integration tutorial.
Thank you.

Yerlan
Guest
Yerlan

My project is bases on Maven+Spring +MyBatis Why do I get the following errors: FAILED CONFIGURATION: @BeforeClass springTestContextPrepareTestInstance java.lang.IllegalStateException: Failed to load ApplicationContext … Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘homeController’: Unsatisfied dependency expressed through field ‘userService’: Error creating bean with name ‘userService’: Unsatisfied dependency expressed through field ‘userMapper’: No qualifying bean of type [kz.ismailov.ESchoolProject.mappers.UserMapper] found for dependency [kz.ismailov.ESchoolProject.mappers.UserMapper]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [kz.ismailov.ESchoolProject.mappers.UserMapper] found for dependency [kz.ismailov.ESchoolProject.mappers.UserMapper]: expected at least 1 bean which qualifies as autowire… Read more »

Julien
Guest
Julien

Needs to add to
– pom.xml:

org.springframework.integration
spring-integration-core
${spring.version}

– @ContextConfiguration(locations = { “classpath*:spring-test-config.xml” })

Digvijay Bele
Guest
Digvijay Bele

Hi ,

I am facing “FAILED CONFIGURATION: @BeforeClass springTestContextPrepareTestInstance” issue while running a test case create using TestNG. Please help me out.

Sanyam
Guest
Sanyam

Your blogs are really good…. Seriously…

Tony
Guest
Tony

Good…

Udhayakumar GK
Guest
Udhayakumar GK

Thats great.

Thanks,

Udhayakumar G K
http://saibabaprayerrequest.blogspot.in

Mehdi Afifi
Guest
Mehdi Afifi

Hi mkyong
please provide a tutorial for GWT framework with a example project for GWT + spring.
Thank you very much.

Avner
Guest
Avner

Note that assertEquals parameters are reversed: first put the expected value and then the actual, and not the other way around