Running unit test cases using Maven

You can run unit test cases using Maven, too. In fact, the install target of Maven also runs unit tests. However, it is possible to run only unit tests. To do this, right-click on the project in Package Explorer and select Run As | Maven test.

You might see the following error in the console:

java.lang.NoClassDefFoundError: org/junit/Assert 
  at packt.book.jee.eclipse.ch5.bean.CourseTest.testIsValidCourse 
(CourseTest.java:10) 
Caused by: java.lang.ClassNotFoundException: org.junit.Assert 
  at java.net.URLClassLoader$1.run(URLClassLoader.java:366) 
  at java.net.URLClassLoader$1.run(URLClassLoader.java:355) 
  at java.security.AccessController.doPrivileged(Native Method) 

The reason for this error is that we haven't added a dependency on JUnit for our Maven project. Add the following dependency in pom.xml:

    <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <version>4.12</version> 
    </dependency> 

Refer to the Using Maven for project management section in Chapter 2, Creating a Simple JEE Web Application, to learn how to add dependencies to a Maven project.

Run the Maven test again; this time, the test should pass.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.187.165