Cannot run test suite on 2 different browsers using data provider - maven

I would like to run my selenium test suite which contains 2 tests(test1 and test2) on 2 different browsers.
I want to run the 2 tests in order so i used dependsOnMethods to specify the order
I created a data provider containing the browsers as parameters (i used 2 browser names as strings) but i could not pass it to #BeforeSuite
when i passed the data provider to test1 ,test1 ran 2 times then test2 ran 1 time.
This is not what i need , i need test1 and test2 to run in order on each browser.
Any suggestion how i can achieve this?
Thanks In advance
#Test( groups = { "login-positive"},dataProvider="browserList")
public void loginTest(String browser)throws MalformedURLException{
PageUtils.initializeBrowser(browser);
loginPage= PageUtils.getHomePage().goToLoginPage(PageUtils.getWebDriver());
String username="raghda";
String password="I123123";
loginPage.fillUserName(username);
loginPage.fillPassword(password);
loggedInPage=loginPage.clickSubmit();
Assert.assertEquals("raghda", loggedInPage.getDisplayedUserName());
}
#Test( groups = { "login- positive"},dataProvider="browserList",dependsOnMethods= {"loginTest"})
public void ValidateGeneralDonation(String browser)throws MalformedURLException{
generalDonPage=loggedInPage.clickGeneralDonation();
generalDonPage.selectCharity(1);
generalDonPage.checkDonationItem(0);
assertTrue(generalDonPage.getDonateButton());
}
POM.xml
<profile>
<id>RegressionTest</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<!-- Suite testng xml file to consider for test execution -->
<suiteXmlFiles>
<suiteXmlFile>testngregression.xml</suiteXmlFile>
</suiteXmlFiles>
<properties>
<property>
<name>parallel</name>
<value>methods</value>
</property>
<property>
<name>threadCount</name>
<value>1</value>
</property>
</properties>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
testngregression.xml
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite1" verbose="1">
<test name="regression">
<parameter name="loggedIn" value="false"></parameter>
<groups>
<define name="login-all">
<include name="login-positive" />
<include name="login-negative" />
</define>
<define name="login-positive-only">
<include name="login-positive" />
</define>
<run>
<include name="login-positive-only" />
</run>
</groups>
<classes>
<class name="basicportal.automation.TestDriver" />
</classes>
</test>
</suite>

Related

Running Specific TestNG Groups from Maven

I have 2 groups of test cases as mentioend below.
#Test(groups="one", dataProvider = "TestData")
public void firstTest(String data){
//Code
}
#Test(groups="one", dataProvider = "TestData")
public void secondTest(String data){
//Code
}
#Test(groups="two", dataProvider = "TestData")
public void thirdTest(String data){
//Code
}
Below is the XML file.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Test-Automation" parallel="methods" thread-count="2" verbose="1">
<test name="Suite Test" parallel="methods" thread-count="2" verbose="1">
<listeners>
<listener class-name="GroupByInstanceEnabler"></listener>
</listeners>
<classes>
<class name="SampleTest">
<methods>
<include name="firstTest"/>
<include name="secondTest"/>
<include name="thirdTest"/>
</methods>
</class>
</classes>
</test>
</suite>
Below is the pom.xml build details.
<build>
<finalName>Automation</finalName>
<filters>
<filter>profiles/${build.profile.id}/config.properties</filter>
</filters>
<resources>
<resource>
<filtering>true</filtering>
<directory>${basedir}/src/test/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>${project.basedir}/testng.xml</suiteXmlFile>
</suiteXmlFiles>
<parallel>method</parallel>
<threadCount>2</threadCount>
</configuration>
</plugin>
</plugins>
</build>
My Question:
Using Maven, how do I run the group "one" and group "two" separately.
I tried "mvn test -Dgroups=two" but it only runs as normal(all tests).
Note: I use 2 different profiles to run the group "one" twice with different values. This is the reason you see profile configuration in the pom.xml file.
You can make use of a beanshell expression for getting this done.
You first add a beanshell expression as below to your suite xml file.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="false">
<test name="Test">
<method-selectors>
<method-selector>
<script language="beanshell">
<![CDATA[whatGroup = System.getProperty("groupToRun");
groups.containsKey(whatGroup);
]]>
</script>
</method-selector>
</method-selectors>
<classes>
<class name="organized.chaos.GroupsPlayGround" />
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
This way you can make use of the suite xml from your IDE and still choose which groups to be executed. You can enrich this beanshell to run everything by default if no value is provided via the JVM argument -DgroupToRun=one
For more information on beanshell execution refer:
Official TestNG documentation here
My blog post link

Duplicate bean conflicts during integration tests (multiple, merged contexts?, re: surefire vs failsafe?)

I am experiencing some dependency injection errors during the integration-test phase of my application. I think it's happening because spring is firing up a context during unit testing, then caching all my mock bean instances used for unit tests, which then conflict with the beans fired up during the integration-test phase, causing DI in my classes to fail because Spring is finding multiple instances of the same beans (mock vs real instances).
Note: This issue is only present during the integration-test phase of the app. The app otherwise starts, runs, and unit tests normally (it's a webapp and I run it with maven 3 using the tomcat:run-war goal).
This is the basic composition of my integration tests. I wrote a bogus one yesterday just to try and isolate the issue (no DI involved in this test):
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations="file:src/main/webapp/WEB-INF/spring/root-context.xml")
#DirtiesContext
public class ITNonsenseTest {
#Test
public void doNothing() {
System.out.println("hello, world");
Assert.assertTrue(true);
}
}
My integration tests all begin with the prefix "IT" and my unit tests all begin with the prefix "UT"
I am using apache surefire for unit tests, and apache failsafe for integration tests:
from my pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven.nfire.plugin.version}</version>
<configuration>
<excludes>
<exclude>**/IT*.java</exclude>
</excludes>
<includes>
<include>**/UT*.java</include>
</includes>
<skipTests>
${skipUnitTests}
</skipTests>
</configuration>
<executions>
<execution>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>${maven.nfire.plugin.version}</version>
</plugin>
<!-- For Integration Tests -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${maven.nfire.plugin.version}</version>
<configuration>
<includes>
<include>**/IT*.java</include>
</includes>
<excludes>
<exclude>**/UT*.java</exclude>
</excludes>
<skipTests>
${skipIntegrationTests}
</skipTests>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
Even when running my simple ITNonsenseTest above, the Spring context fails to initialize properly. Since many of the beans I'm trying to create already exist in the context (as mock bean instances from my Unit test phase), Spring is complaining about duplicates being found.
Is there a way to tear down the context after the unit test phase, and build a new context for the integration-test phase? Or am I just doing something completely wrong?
This is my root-context.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:util="http://www.springframework.org/schema/util"
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.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- Root Context: defines shared resources visible to all other web components -->
<context:property-placeholder location="file://${conf.dir}/*.properties" />
<context:component-scan base-package="com.myapp.models, com.myapp.dao, com.myapp.service" />
<beans:import resource="simplesm-context.xml" />
<beans:import resource="datasources.xml" />
<beans:import resource="rabbit-context.xml" />
<util:properties id="swaggerProps" location="classpath:swagger.properties" />
<util:properties id="encryptionProps" location="classpath:sharedEncryption.properties" />
<aop:aspectj-autoproxy />
<beans:bean name="cacheManager" class="com.google.code.ssm.spring.SSMCacheManager">
<beans:property name="caches">
<beans:set>
<beans:bean class="com.google.code.ssm.spring.SSMCache">
<beans:constructor-arg name="cache" index="0" ref="defaultCache" />
<beans:constructor-arg name="expiration" index="1" value="86400" />
<beans:constructor-arg name="allowClear" index="2" value="false" />
</beans:bean>
</beans:set>
</beans:property>
</beans:bean>
<beans:bean name="defaultCache" class="com.google.code.ssm.CacheFactory">
<!-- <beans:property name="cacheName" value="defaultCache" /> -->
<beans:property name="cacheClientFactory">
<beans:bean name="cacheClientFactory" class="com.google.code.ssm.providers.spymemcached.MemcacheClientFactoryImpl" />
</beans:property>
<beans:property name="addressProvider">
<beans:bean class="com.google.code.ssm.config.DefaultAddressProvider">
<beans:property name="address" value="${ureg.memcached.url}:${ureg.memcached.port}" />
</beans:bean>
</beans:property>
<beans:property name="configuration">
<beans:bean class="com.google.code.ssm.providers.CacheConfiguration">
<beans:property name="consistentHashing" value="true" />
</beans:bean>
</beans:property>
</beans:bean>
</beans:beans>
This is my project's dependencies' versioning info:
<properties>
<java-version>1.7</java-version>
<org.springframework-version>4.0.6.RELEASE</org.springframework-version>
<org.springframework.amqp.version>1.3.5.RELEASE</org.springframework.amqp.version>
<org.springframework.data.version>1.6.0.RELEASE</org.springframework.data.version>
<google.simple.spring.memcached.version>3.2.1</google.simple.spring.memcached.version>
<maven.nfire.plugin.version>2.17</maven.nfire.plugin.version>
</properties>
Lastly, I tried adding a #DirtiesContext to all of my Unit tests that use SpringJunit4ClassRunner, but I'm still seeing this error. All of my unit tests are structured in the following manner:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration
#DirtiesContext
public class UTAdditionalDataPointsServiceTest {
#Configuration
static class AdditionalDataPointsServiceUnitTestContextConfiguration {
#Bean
#Qualifier("props")
public Properties getProperties() {
return Mockito.mock(Properties.class);
}
#Bean
#Qualifier("npJdbcTemplate")
public NamedParameterJdbcTemplate getNamedParameterJdbcTemplate() {
return Mockito.mock(NamedParameterJdbcTemplate.class);
}
#Bean
#Qualifier("uJdbcTemplate")
public JdbcTemplate getJdbcTemplate() {
return Mockito.mock(JdbcTemplate.class);
}
#Bean
public AdditionalDataPointsDao getAdditionalDataPointsDao() {
return Mockito.mock(AdditionalDataPointsDaoImpl.class);
}
#Bean
public AdditionalDataPointService getAdditionalDataPointService() {
return Mockito.mock(AdditionalDataPointServiceImpl.class);
}
}
#Inject
AdditionalDataPointService adpService;
#Test
public void testGetAdditionalDataPoints() throws Exception {
List<AdditionalDataPoint> adpList = adpService.getAdditionalDataPoints(1);
org.junit.Assert.assertNotNull(adpList);
}
I'm fairly confident the issue is the mock instances cached in the context during the unit testing phase, because if i comment out the definition for a bean when theyre instantiated in my root-context.xml file, the errors for those duplicates go away.
The stack traces are always the same, along the lines of:
nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [java.util.Properties] is defined: expected single matching bean but found 2: realInstanceBeanName,mockInstanceBeanNameGetter
Configuring classesDirectory do the job for me. See Maven-failsafe-plugin fails to execute integration tests
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<classesDirectory>${project.build.outputDirectory}</classesDirectory>
</configuration>
</plugin>

hibernatetools ant task - hbm2java - creates pojo without package statement

I'm exporting my mapping (hbm.xml) to pojo classes using ant task.
It generates the POJO files in the mapped directory. But the Classes miss the Package statement. It simply creates all files in default package
// default package
// Generated Aug 23, 2012 12:34:40 PM by Hibernate Tools 3.2.2.GA
Here is my ant build file for the task.
<project name="Hibernate Tools for Ant - hbm2java" default="gensrc">
<path id="tools">
<path location="lib/hibernate-tools-3.2.3.GA.jar"/>
<path location="lib/hibernate3.6.10.jar"/>
<path location="lib/freemarker-2.3.8.jar"/>
<path location="lib/hsqldb-2.2.4.jar"/>
<path location="lib/commons-logging.jar"/>
<path location="lib/dom4j-1.6.1.jar"/>
<path location="lib/slf4j-api-1.6.1.jar"/>
<path location="lib/hibernate-jpa-2.0-api-1.0.1.Final.jar"/>
</path>
<taskdef name="gen-src" classname="org.hibernate.tool.ant.HibernateToolTask"
classpathref="tools"/>
<target name="gensrc">
<gen-src destdir="src/main/java">
<configuration
configurationfile="src/main/resources/hibernate.cfg.xml">
<fileset dir="src/main/java/com/kee/example/domain/maps">
<include name="Event.hbm.xml"/>
</fileset>
</configuration>
<hbm2java destdir="src/main/java/com/kee/example/domain"/>
</gen-src>
</target>
</project>
the default Pojo.ftl (inside hibernate-tools.jar) has the declaration as below
${pojo.getPackageDeclaration()}
// Generated ${date} by Hibernate Tools ${version}
what should i be changing in order to have correct package declaration in the generated POJO.
Update:
Here is my Mapping file
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.kee.example.domain">
<meta attribute="generated-class">EventBase</meta>
<meta attribute="implement-equals">true</meta>
<meta attribute="scope-field">protected</meta>
<class name="com.kee.example.domain.Event" table="event">
<id name="id" type="java.lang.Long">
<generator class="native"/>
</id>
<property name="eventDate" type="timestamp"/>
<property name="eventString" type="java.lang.String"/>
</class>
</hibernate-mapping>
I was getting the same problem in maven and not sure about the reason but it was resolved once I started using annotationconfiguration instead of jdbcconfiguration.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>hibernate3-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<components>
<component>
<name>hbm2ddl</name>
<implementation>annotationconfiguration</implementation>
<outputDirectory>${db.src.dir}</outputDirectory>
</component>
<component>
<name>hbm2java</name>
<implementation>annotationconfiguration</implementation>
<outputDirectory>src/main/java</outputDirectory>
</component>
</components>
<componentProperties>
<drop>true</drop>
<create>true</create>
<export>false</export>
<format>true</format>
<jdk5>true</jdk5>
<ejb3>true</ejb3>
<outputfilename>${ddl.file}</outputfilename>
<templatepath>src/main/resources/hibernate-template</templatepath>
<delimiter>;</delimiter>
<configurationfile>src/main/resources/hibernate.cfg.xml</configurationfile>
</componentProperties>
</configuration>
<dependencies>
<dependency>
<groupId>${jdbc.groupId}</groupId>
<artifactId>${jdbc.artifactId}</artifactId>
<version>${jdbc.version}</version>
</dependency>
</dependencies>
</plugin>
I did not want the annotated pojo classes so I had commented codes in *.ftl files.

Missing #XmlRootElement when creating a client from a wsdl

I have a question in regards to consuming a web service based on a third party wsdl file.
I've taken the given wsdl and generated the 120+ java files required. This process was done by using xjc. Within the Sping environment, I was able to successfully create a couple of JUnit tests by calling a couple of the exposed services.
But, in order to successfully test those services I had to add the #XmlRootElement annotation to the generated java files. Otherwise, I would encounter an error stating
"com.sun.istack.SAXException2: unable to marshal type
"com.beam.services.client.UserGetRequestData" as an element because it
is missing an #XmlRootElement annotation"
.
I've exhausted my search… I have no control as to how the wsdl file is created/structured. How can I go about generating the java files to ensure that the #XmlRootElement annotation is included or go about writing the client side code in way to avoid the error above?
Thank you.
If you really need the #XmlRootElement you could use simple binding mode if your type is used for only one element. The reason why JAXB does not include the annotation by default and how to use simple binding is explained here: https://community.oracle.com/blogs/kohsuke/2006/03/03/why-does-jaxb-put-xmlrootelement-sometimes-not-always:
your schema might be used by other schemas that XJC isn't compiling
right now
and
Such notion isn't defined in the spec, but as an experiment we have
such aggressive optimization mode in XJC, tentatively called
"simple-minded binding mode".
The sample seems to got lost when the moved the blog, but it looked like this:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="1.0" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" jaxb:extensionbindingprefixes="xjc">
<xs:annotation>
<xs:appinfo>
<jaxb:globalbindings>
<xjc:simple/>
</jaxb:globalbindings>
</xs:appinfo>
</xs:annotation>
<xs:element name="foo" type="bar"/>
<xs:complextype name="bar"/>
</xs:schema>
The other possibilty is to wrap it in a JAXBElement. The ObjectFactory should include a method for creating these wrapped objects.
If you are using mavem then check this link it worked for me.
Create Maven project. Below you can see the POM:
<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.zmeu</groupId>
<artifactId>zmeu-blog-maven-jaxb</artifactId>
<version>1.0-SNAPSHOT</version>
<name>ZMEU Blog Maven JAXB</name>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.8.0</version>
<configuration>
<schemaDirectory>src/main/resources/schema</schemaDirectory>
<bindingDirectory>src/main/resources/schema</bindingDirectory>
<generatePackage>org.zmeu.blog.jaxb</generatePackage>
<strict>false</strict>
<extension>true</extension>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics</artifactId>
<version>0.6.2</version>
</plugin>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics-annotate</artifactId>
<version>0.6.2</version>
</plugin>
</plugins>
<args>
<arg>-Xannotate</arg>
<arg>-XtoString</arg>
</args>
</configuration>
<executions>
<execution>
<id>generate</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics-runtime</artifactId>
<version>0.6.2</version>
</dependency>
</dependencies>
</project>
Write XML Schema (schema.xsd):
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="user" type="user" />
<xs:element name="userList" type="userList" />
<xs:complexType name="user">
<xs:all>
<xs:element name="id" type="xs:long" minOccurs="0" />
<xs:element name="name" type="xs:string" />
<xs:element name="registrationDate" type="xs:dateTime" />
</xs:all>
</xs:complexType>
<xs:complexType name="userList">
<xs:sequence>
<xs:element name="user" type="user" minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:schema>
Customize JAXB Bindings (binding.xjb):
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:annox="http://annox.dev.java.net"
xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"
version="2.1">
<jaxb:globalBindings>
<!-- Use java.util.Calendar instead of javax.xml.datatype.XMLGregorianCalendar for xs:dateTime -->
<jaxb:javaType name="java.util.Calendar" xmlType="xs:dateTime"
parseMethod="javax.xml.bind.DatatypeConverter.parseDateTime"
printMethod="javax.xml.bind.DatatypeConverter.printDateTime" />
<!-- Force all classes implements Serializable -->
<xjc:serializable uid="1" />
</jaxb:globalBindings>
<!-- Annotate the following classes with XmlRootElement -->
<jaxb:bindings schemaLocation="schema.xsd" node="/xs:schema">
<jaxb:bindings node="xs:complexType[#name='user']">
<annox:annotate>
<annox:annotate annox:class="javax.xml.bind.annotation.XmlRootElement" name="user" />
</annox:annotate>
</jaxb:bindings>
<jaxb:bindings node="xs:complexType[#name='userList']">
<annox:annotate>
<annox:annotate annox:class="javax.xml.bind.annotation.XmlRootElement" name="userList" />
</annox:annotate>
</jaxb:bindings>
</jaxb:bindings>
</jaxb:bindings>
Run the build using mvn clean install command. Build must be successful. Generated classes will be located in target/generated-sources/xjc directory. Below is a snippet from generated User class:
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "user", propOrder = {})
#XmlRootElement(name = "user")
public class User implements Serializable, ToString {
private final static long serialVersionUID = 1L;
protected Long id;
#XmlElement(required = true)
protected String name;
#XmlElement(required = true, type = String.class)
#XmlJavaTypeAdapter(Adapter1 .class)
#XmlSchemaType(name = "dateTime")
protected Calendar registrationDate;
}
You are done!

Excluding TestNG Groups From Maven

I have some slow tests that rely on the database that I don't want run every time I build my project with Maven. I've added the excludedGroups element to my pom file as explained http://maven.apache.org/plugins/maven-surefire-plugin/test-mojo.html#excludedGroups but I can't seem to get it working.
I've created a minimal project. Here is the pom.xml:
<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>test</groupId>
<artifactId>exclude</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.4.2</version>
<configuration>
<excludedGroups>db</excludedGroups>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>5.14</version>
</dependency>
</dependencies>
</project>
And these are the two test classes:
public class NormalTest {
#Test
public void fastTest() {
Assert.assertTrue(true);
}
}
and
public class DatabaseTest {
#Test(groups={"db"})
public void slowTest() {
Assert.assertTrue(false);
}
}
However both tests still run. I can't figure out what I'm doing wrong.
I ended up creating external test suits:
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="tests">
<test name="standard">
<groups>
<run>
<exclude name="slow" />
<exclude name="external" />
<exclude name="db" />
</run>
</groups>
<packages>
<package name="com.test.*" />
</packages>
</test>
</suite>
and
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="tests">
<test name="full">
<packages>
<package name="com.test.*" />
</packages>
</test>
</suite>
and specifyied which to run in a profile:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.11</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>src/test/resources/suites/standard.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
...
<profile>
<id>fulltest</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>src/test/resources/suites/full.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
</profile>
From my experience, the excluded groups feature works only when you have a set of included groups. So in order to do what you want, you need to add all the tests to at least one group (you can do this "easily" by annotating the class rather than methods).
For example (just changing the NormalTest)
#Test( groups = "fast")
public class NormalTest {
#Test
public void slowTest() {
Assert.assertTrue(true);
}
}
and in your configuration
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.4.2</version>
<configuration>
<groups>fast</groups>
<excludedGroups>db</excludedGroups>
</configuration>
</plugin>
I know that this is not obvious, but it's the way that testng works :S. As a side note, I've always used an external configuration file for testng rather that the embedded configuration in the pom, so the parameter groups might not be correct.

Resources