Main Tutorials

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 "[email protected]";
	}

}

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, "[email protected]");
		

	}

}

Output


[email protected]
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 Author

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

Comments

Subscribe
Notify of
13 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Sergio Carabetta
8 years ago

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.

billy
2 months ago

terrible,dont waste your time on this

Martin Lorentsen
2 years ago

I have looked for a setup example with TestNG + Spring + Cucumber + SureFire – there are lots of examples with JUnit, but not with TestNG. I want to be able to run the test from Maven in order to integrate the test into Jinkins, when I run with JUnit it runs the tests, but it is unable to find any tests when running through TestNG. Does anyone has a working example?

NAZIA TARANNUM
4 years ago

no profiles configuration..

Shantaram
6 years ago

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
7 years ago

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 candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: 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 candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

Julien
7 years ago

Needs to add to
– pom.xml:

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

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

Digvijay Bele
9 years ago

Hi ,

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

Sanyam
9 years ago

Your blogs are really good…. Seriously…

Tony
10 years ago

Good…

Udhayakumar GK
10 years ago

Thats great.

Thanks,

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

Mehdi Afifi
10 years ago

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

Avner
9 years ago

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