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

13 comments on “TestNG + Spring Integration Example

  1. 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?

    Reply
  2. 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.

    Reply
  3. 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)}

    Reply
  4. Needs to add to
    – pom.xml:

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

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

    Reply
  5. 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.

    Reply
  6. Hi ,

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

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

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

    Reply

Leave a Comment

Your email address will not be published. Required fields are marked *