Running JUnit tests in Maven with Surefire Plugin and skipTests in default packaging

March 3rd, 2015

Further to the JUnit test we have created, it’s time to have it built into a maven build script so that the test can be run.
Once you have JUnit in your pom.xml file, maven will run tests with JUnit and all valid JUnit test classes under src/test/java directory.
However if you are using more than one test framework such as both TestNG and JUnit, you will need to explicitly specify JUnit as the test provider.

<plugins>
[...]
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.18.1</version>
    <dependencies>
      <dependency>
        <groupId>org.apache.maven.surefire</groupId>
        <artifactId>surefire-junit47</artifactId>
        <version>2.18.1</version>
      </dependency>
    </dependencies>
  </plugin>
[...]
</plugins>

For the default maven packaging, you might not want to run test and the tests can be run as a separate job/task.
To address this, all you need to do is create a skipTests property in the pom.xml and have it set to false only when you need to run the tests.


    <properties> 
    	<skipTests>true</skipTests> 
    </properties>

Use skipTests in the configuration element.


<plugins>
[...]
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.18.1</version>
    <dependencies>
      <dependency>
        <groupId>org.apache.maven.surefire</groupId>
        <artifactId>surefire-junit47</artifactId>
        <version>2.18.1</version>
      </dependency>
    </dependencies>
    <configuration>
      <skipTests>${skipTests}</skipTests>
    </configuration>
  </plugin>
[...]
</plugins>

To run the tests:
mvn test -DskipTests=false

Java, Software , ,





Quick Guide to JUnit multiple parameter tests with Parameterized annotation

February 15th, 2015

It is extremely easy to create JUnit test classes to run and test your application or Java code.
JUnit is a simple framework to write repeatable tests. It is an instance of the xUnit architecture for unit testing frameworks.
All it needs is to include the JUnit library and use the @Test annotation for the method you need to run as test.
However, sometimes we need the convenience of the same test method to run with different parameters.
Following is an example how it can be done easily with some changes to a single test method by adding @RunWith(Parameterized.class) before the class name and then initialize a collection of parameters with @Parameters.
You can also read the parameters from a data provider once using a initializing method with @BeforeClass.

package net.marvinlee.test;
 
import java.util.ArrayList;
import java.util.Collection;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
 

@RunWith(Parameterized.class)
public class JunitTest
{

	private String code;
	private String description;
	

	public JunitTest(String code, String description)
	{
		super();
		this.code = code;
		this.description = description;
	}

	 @Parameters
	 public static Collection getParameters() 
	 { 
		 ArrayList<Object[]> paramList = new ArrayList<Object[]>();
		 addTestcases(paramList);
		 return paramList;
	 }

	protected static void addTestcases(ArrayList<Object[]> paramList)
	{
		for(int i=1;i<=10;i++)
		{
			paramList.add(new Object[]{"Code" + i, "Description " + i});
		}
	}

	@BeforeClass
	public static void init() 
	{ 
		System.out.println("Current dir:" +  JunitTest.class.getClassLoader().getResource("").getPath()); 
	}
	 

	@Test
	public void validateCases() 
	{
		boolean matched = ((code.equals("Code5") && description.equals("Description 5"))
			|| (code.equals("Code6") && description.equals("Description 6")));
		Assert.assertTrue("Failed test=" + code, matched);
		 
	}
	 
}

JUnit test classes can be run with Eclipse IDE and the above test class will show run results similar to the following screenshot.
Only the 4th and 5th testcases that match with the hardcoded values will pass while we get remaining 8 failures.
junit

Have a run with it!

Java, Software ,



Getting Started with OpenShift – easy reference by Steven Pousty & Katie J. Miller

January 11th, 2015

With the advancement of cloud technologies, the options of PaaS (Platform as a Service) for web applications are aplenty. It also helps that there are many available selections to support Java.
Although Red Hat was placed in Gartner ‘Visionaries’ Magic Quadrant, Red Hat’s OpenShift platform is my preferred choice of PaaS at the moment.

OpenShift is a cloud computing platform as a service product from Red Hat. A version for private cloud is named OpenShift Enterprise.

The speed of developing and getting up and running for a product is absolutely crucial for a quick go-to-market advantage, hence any PaaS with the easiest use will have an upper hand over the rest.

For a newer platform like OpenShift, having a book like ‘Getting Started with OpenShift’ is one of the best way to get impatient beginners for a quick run hands-on experience with it. There are many other ways offered such as following the Getting Started – online version or simply the forums, but those are beside the point of this book review.

About The Authors

Steve Poutsy is a Developer Advocate for OpenShift. He has spoken at over 50 conferences and done over 30 workshops including Monktoberfest, MongoNY, JavaOne, FOSS4G, CTIA, AjaxWorld, GeoWeb, Where2.0, and OSCON. Before OpenShift, Steve was a developer evangelist for LinkedIn, deCarta, and ESRI.

Katie Miller is also a OpenShift Developer Advocate. Katie is a polyglot programmer with a penchant for Haskell. The functional programming enthusiast co-founded the Lambda Ladies online community and co-organizes the Brisbane Functional Programming Group. She is passionate about coding, open source, software quality, languages of all kinds, and encouraging more girls and women to pursue careers in technology.

About The Book

The book is meant as ‘A Guide For Impatient Beginners’ but I am glad to say it is not as brief a ‘Getting Started’ guide as any average technical books.

For anyone who doesn’t need any introduction about PaaS concepts, they can dive right in from Chapter 3 as the first couple of  chapters provide conceptual details about PaaS and the basics about cloud computing.

There are 2 important terms in OpenShift, gears and cartridges.

Gears are secure containers for your code. Each gear is allocated CPU, memory, disk, and network bandwidth. A single gear can be used to create an entire web application complete with a private database instance. Multiple gears can be used to create multiple applications or configure your applications to automatically scale in response to web traffic. There are currently three gear types on OpenShift Online: small, medium, and large. Each size provides 1 GB of disk space by default. The large gear has 2 GB of RAM, the medium gear has 1 GB of RAM, and the small and small.highcpu gears have 512 MB of RAM.

Cartridges are plug-ins added to a gear. Cartridges are the plug-ins that house the framework or components that can be used to create and run an application. One or more cartridges run on each gear, and the same cartridge can run on many gears for clustering or scaling. There is quite a list of supported cartridges and OpenShift also supports D-I-Y cartridges, which means most of the popular technologies that you need are already supported by default. Example cartridges are the application servers (Jboss, Tomcat, PHP, Ruby, Node.js, Perl), databases (MongoDB, MySQL, PostgreSQL), and integration tools (Jenkins, cron).

Due to the nature of cloud computing, secure access is important for the application to be accessed or managed. A sub-chapter is dedicated for this and there are sufficient resource in the online guide to get the setup done.

Advanced concepts are also covered in the later chapters of the book, such as storage (it’s shared hosting), websockets, backup options and team collaboration.

 

 

Table of Contents

1. Introduction
    What Is the Difference Between IaaS, PaaS, and SaaS?
    The Three Versions of OpenShift
    Choosing the Right Solution for You
    Things to Understand
        Words You Need to Understand
        Technology You Need to Understand
2. Creating Applications
    Preliminary Steps
    Setting Up the Command-Line Tools
    Creating Your First Application
    Autoscaling and Why You Should Use It by Default
    Reasons to Move to the Paid Tier
3. Making Code Modifications
    Cloning Code to Your Local Machine
    Modifying Application Code
    Building and Deploying Your Code
    Action Hook Scripts
    Hot-Deploying Code
4. Adding Application Components
    Database-Related Cartridges
    Nondatabase Cartridges
        Cron
        Continuous Integration
        Metrics and Monitoring
    Finding Cartridges and QuickStarts
        Adding Third-Party Cartridges
5. Environment and Application Management
    SSH Access
        Using SSH to Interact with a Database
        Importing SQL in an SSH Session
    Environment Variables
        Preconfigured Environment Variables
        Custom Environment Variables
        Overriding Preconfigured Environment Variables
    Log Access
    Changing Application Server or Database Settings
        Application Server Configuration Changes
        Database Configuration Changes
    Using Marker Files
6. Library Dependencies
    Where to Declare Dependencies
    Incorporating Your Own Binary Dependencies
    Modifying Your Application to Use the Database
        Code to Connect to the Database
        Code to Close the Database Connection
        Code to Query the Terms for the Insult
        What We Have Gained by Adding a Database
7. Networking
    WebSockets
    SSH Port Forwarding
    Custom URLs
    SSL Certificates
    Talking to Other Services
    Addressable Ports
8. Disk Usage
    Where You Can Write “to Disk”
    Determining How Much Disk Space Is Used
    Copying Files to or from Your Local Machine
    Other Storage Options
9. Backup
    Managing Deployments and Rollbacks
        Manual Deployments
        Keeping and Utilizing Deployment History
    Application Snapshots with RHC
    Backing Up Your Database
        Writing a Cron Script
        Moving Data off the Gear
10. Team Collaboration
    Managing Multiple SSH Keys
    Domain Access for Teams
    Possible Workflows
11. Summary
    What We Covered
    Other Areas to Explore
    Final Words
Appendix A. Basic Linux for Non-Linux Users

Conclusion

I’m going to make known that I do prefer OpenShift over many PaaS providers.

My preference of OpenShift over other PaaS competitors:
– Low barrier of entry, free tier with no credit card required
– A free tier provides 3 gears – that is 3 web applications hosting for free!
– Minimal technical restrictions, non-proprietary platform API or database
– Easy upgrade from free tier to paid tier, affordability
– Backed by a reputable technology corporation

My preferred key benefits from those listed on OpenShift website:
– Application portability
– Extensible cartridge system for adding services
– Automatic application stack provisioning and application scaling
– Choice of cloud infrastructure – preventing lock-in
– Minimized vendor lock-in – built on open source technologies

You can now download this book in ebook format for FREE or optionally you can purchase it from Amazon.com

Books, Java, Software, Technology , , , , ,