Stephen Nimmo

9Feb/090

TestNG, Spring, Maven – Quick and Easy


Warning: file_get_contents(http://feeds.delicious.com/v2/json/urlinfo/data?url=http%3A%2F%2Fstephennimmo.com%2Fblog%2F2009%2F02%2F09%2Ftestng-spring-maven-quick-and-easy%2F) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 999 Unable to process request at this time -- error 999 in /home1/stephenn/public_html/blog/wp-content/plugins/digg-digg/dd.class.php on line 866

First, you need the dependencies. TestNG requires you to have a classifier for the jdk.

<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/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.stephennimmo</groupId>
	<artifactId>demo</artifactId>
	<packaging>jar</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>domain</name>
	<dependencies>
		<dependency>
			<groupId>org.testng</groupId>
			<artifactId>testng</artifactId>
			<version>5.8</version>
			<classifier>jdk15</classifier>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>2.5.6</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring</artifactId>
			<version>2.5.6</version>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.14</version>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<version>2.4</version>
				<configuration>
					<suiteXmlFiles>
						<suiteXmlFile>${basedir}/src/test/resources/testng.xml</suiteXmlFile>
					</suiteXmlFiles>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

Now the testng.xml that is located in your test resources dir.

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="domain" verbose="5">

	<test name="DomainObjectFactoryTests">
		<packages>
			<package name="com.stephennimmo.domain.test" />
		</packages>
	</test>

</suite>

Here's the test class. Pay attention that it extends the AbstractTestNGSpringContextTests. If you want transactions involved, extend AbstractTransactionalTestNGSpringContextTests and set it up in the @BeforeClass.

@ContextConfiguration(locations={"classpath:ServiceFactoryContext.xml"})
public class TestPersonService extends AbstractTestNGSpringContextTests {

	@Autowired
	private PersonService personService;

	@Test(groups = { "service" })
	public void testPersonService(){
		long id = 1000;
		Person p = personService.find(id);
		assertNotNull(p);
		assertEquals(p.getId(), id);
	}

}

Thanks to Josh Long.
http://www.joshlong.com/jl/entry/testng_spring_testcontext_and_maven

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


No trackbacks yet.