TestNG - first steps example of the way of writing the article "JPA - first steps," I decided that I would do well to learn the first steps to assist framework'u perform unit tests -
TestNG. My first impression is very positive. Compared to the JUnit is simpler and more accessible interface, but is more powerful and is also much better documented. Hierarchy
tests in TestNG we doczycznienia with the following hierarchy of tests: suite (set), Test, Class (class containing test methods) and Method (Testing Method). While the Test Suite and are purely logical entities (defined in the configuration level), the Class and Method are units that contain the physical implementation of the tests. In addition, TestNG allows marking Class and Method marker group. At the same time it is possible to assign them to multiple groups. For all of these entities hierarchy and groups can set out the methods that will be called before or after the tests.
dependencies between tests
TestNG allows you to define dependencies between tests. You can specify that the execution of a method of testing organization or a class of tests will depend on successful implementation of the other test methods or test groups. Test Configuration
Tests in TestNG are configured in a xml file conforming to the DTD: http://testng.org/testng-1.0.dtd and through endorsements. In one configuration file can define a Suite (\u0026lt;suite> marker), within which is defined by any number of Test'ów (\u0026lt;Test> tags), or you can import the configuration files for other sets of tests. On the test, in turn, may consist of any number of groups (tag \u0026lt;groups>), class (\u0026lt;classes> marker), and you can specify the Java packages (\u0026lt;pakages> marker), within which the class will be searched for testing. For class, you can also specify which methods are to be executed and which not. Both individuals and Test Suite, you can define parameters that can be transferred In Test methods (marker \u0026lt;parameter>). With the use of annotations can include determine such things as: - whether the class includes the tests (Test annotation), - if the method is a method of testing (including the endorsement test) - the term the group owning class or the method (parameter groups for the endorsement test), and - Testing method or class containing the tests is dependent on other tests (parameter dependsOnGroups dependsOnMethods and annotations test) - identify ways to call before or after the tests (annotate Before and After * *), - define the parameters for the transmission method testing (annotation Parameters)
Example The following example is a unit tests developed for the article "JPA - first steps." It consists of a configuration file, the abstract base class and two classes containing a specific test method.
configuration file configuration file contains the definition of course test (implemented in two classes) and methods used by the majority of testing parameters.
\u0026lt;suite name="Suite1"> \u0026lt;parameter name="user1" value="heniek" /> \u0026lt;parameter name = "user2" value = "Frank" /> \u0026lt;test Name="Test"> \u0026lt;classes> \u0026lt;class name="pl.dwalczak.jpapg1.UserTest" /> \u0026lt;class name="pl.dwalczak.jpapg1.MessageTest" /> ; \u0026lt;/ classes> \u0026lt;/ test> \u0026lt;/ suite>
abstract base class
This class defines the basic services needed for testing the model mapping JPA. First of all, provide the EntityManager, which initiates and begins a transaction before executing each test method. However, after performing the test method and to approve the transaction closes EntityManager'a. After completing the entire test suite (Suite) is closed EntityManagerFactory.
pl.dwalczak.jpapg1 package; javax.persistence.EntityManager import, import org.apache.log4j.Logger; org.testng.annotations.AfterMethod import, import org.testng.annotations.AfterSuite imports, org.testng.annotations.BeforeMethod imports, pl.dwalczak.jpapg1.util.JpaUtil; abstract public class {private static JpaTestCase final Logger LOG = Logger.getLogger (JpaTestCase.class) protected EntityManager em; @ BeforeMethod beginWorkUnitAndTransaction public void () {try {em = JpaUtil. getEntityManager (); em.getTransaction (). begin ();} catch (RuntimeException e) {LOG.error ("Can not beginWorkUnitAndTransaction", e);
}
}
@AfterMethod
public void closeWotkUnitAndCommitTransaction() {
try {
if (false == em.getTransaction().getRollbackOnly()) {
em.getTransaction().commit();
} else {
em.getTransaction().rollback();
}
} catch (RuntimeException e) {
LOG.error("Can't closeWotkUnitAndCommitTransaction", e);
} finally {
if (null != em) {
try {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
em.close();
} catch (RuntimeException e) {
LOG.error("Can't close entity manager!", e);
}
} I = null;}} @ AfterSuite public void closeEntityManagerFactory () {JpaUtil.closeEntityManagerFactory ();}}
UserTest
Class This class contains all tests for entity "you" and is provided with a suitable marker groups ("user") . It includes examples of passing parameters to methods of testing and determining the relationship between methods of Test.
pl.dwalczak.jpapg1 package, import java.util.List; import org.apache.log4j.Logger; org.testng.Assert import, import org.testng.annotations.Parameters; org.testng.annotations.Test import, import pl.dwalczak.jpapg1.model.User; @ Test (groups = "user") public class UserTest extends JpaTestCase{
private static final Logger LOG = Logger.getLogger(UserTest.class);
@Test
@Parameters({ "user1" })
public void createUser1(String user) {
LOG.debug("create user: " + user);
em.persist(new User(user));
}
@Test
@Parameters({ "user2" })
public void createUser2(String user) {
LOG.debug("create user: " + user);
em.persist(new User(user));
}
@Test(dependsOnMethods={"createUser1", "createUser2"})
@SuppressWarnings(value="unchecked")
public void list() {
LOG.debug("get user list");
em.getTransaction().setRollbackOnly();
List list = em.createNamedQuery (user.findAll "). getResultList (); LOG.debug (" user list: "+ list); Assert.assertEquals (list.size (), 2);} @ Test (dependsOnMethods = { "createUser2"}) @ Parameters ({"user2"}) public void findUser (String user) {LOG.debug ("find user" + user); User u = (User) em.createNamedQuery (user.findByNickName " ). setParameter ("nickname", user). getSingleResult (); LOG.debug ("Found user:" + u); Assert.assertNotNull (u);}}
MessageTest
Class This class contains all tests for entity " Message. " Execution of all testing methods This class is subject to a positive test result from the group "user".
pl.dwalczak.jpapg1 package, import java.util.List; import org.apache.log4j.Logger; org.testng.Assert import, import org.testng.annotations.Parameters; org.testng.annotations.Test import, import pl.dwalczak.jpapg1.model.Message imports, pl.dwalczak.jpapg1.model.User; @ Test (dependsOnGroups = "user") public class extends MessageTest JpaTestCase {private static final Logger LOG = Logger.getLogger (MessageTest.class) ; @ Test @ Parameters ({"user1" "user2"}) public void user1SendToUser2 (String user1, String user2) {LOG.debug ("user1SendToUser2") SendMessage ("title1", "content1" user1, user2);
}
@Test
@Parameters({ "user2", "user1" })
public void user2SendToUser1(String user1, String user2) {
LOG.debug("user2SendToUser1");
sendMessage("title2", "content2", user1, user2);
}
private void sendMessage(String title, String content, String userFrom, String userTo) {
User uFrom = (User) em.createNamedQuery("user.findByNickName").setParameter("nickName", userFrom).getSingleResult();
User uTo = (User) em.createNamedQuery("user.findByNickName").setParameter("nickName", userTo).getSingleResult();
Message message = new Message(title, content, uFrom, uTo);
LOG.debug("send a message: " + message);
em.persist(message);
}
@Test(dependsOnMethods={"user1SendToUser2", "user2SendToUser1"})
@Parameters({"user1"})
@SuppressWarnings(value="unchecked")
public void viewUser1Messages(String user) {
LOG.debug("view messages of: " + user);
User u = (User) em.createNamedQuery("user.findByNickName").setParameter("nickName", user).getSingleResult();
Assert.assertNotNull(u);
List received = em.createNamedQuery("message.findReceived").setParameter("user", u).getResultList();
LOG.debug("received messages: " + received);
Assert.assertEquals(received.size(), 1);
Letter em.createNamedQuery send = ("message.findSend"). SetParameter ("user", u). GetResultList (); LOG.debug ("send messages:" + send); Assert.assertEquals (send.size (), 1);}}
Resources
source files TestNG documentation