Archive

Posts Tagged ‘testing’

Working With Scala Test

May 16, 2011 1 comment

Scala Test is an open source Test framework for the java platform. With Scala test we can either test java or Scala code. It integrates with popular existing tools like JUnit, TestNG and it is designed to do different styles of testing like Behavior Driven Design for example.

Let’s have a look at a simple JUnit4 Test in Scala Test

package com.inphina.ibat.junit

import org.scalatest.junit.AssertionsForJUnit
import org.junit.Assert._
import org.junit.Test
import  org.junit.Before

class SimpleJunit4Demo extends AssertionsForJUnit {

  var sb: StringBuilder = _

  @Before def initialize() {
    sb = new StringBuilder("Welcome To ")
  }

  @Test
  def verifyEasy() {
    sb.append("ScalaTest!")
    assertEquals("Welcome To ScalaTest!", sb.toString)
  }

}

This Test implementation is not very different from a normal JUnit Test we are accustomed to. In order to understand ScalaTest there are three concepts we need to be aware of.

  • Suite: A collection of tests. A Test is anything that has a name and can succeed or fail
  • Runner: ScalaTest provides a Runner application that can run Suites of Tests
  • Reporter: As Tests are run events are fired to a reporter, it takes care of presenting results back to the user

Read more…

Categories: Scala Tags: ,

Servlet Unit Testing : Experiences with Mockrunner

February 13, 2011 1 comment

This is a follow-up of my previous post where I provided different views on approaching Servlet testing along with an overview of various frameworks for doing the same. In this post, I will be delving a bit into detail about my experiences while unit testing Servlet code with Mockrunner, one of the frameworks mentioned in my last post.

Integration with Maven I am using maven (version 2.2.1) as build management tool for the project, so a publicly available artifact allowing an easy integration in my project was my preference. Unfortunately Mockrunner team doesn’t publish their artifacts to a Maven repository as is clear from this discussion thread on their user-forum. But I did find a publicly available Mockrunner artifact to include in my pom.xml and decided to give it a try. As feared, I ran into issues because Mockrunner’s pom file was referring to couple of dependencies which are not available on Maven public repository. I am talking here of 0.4 version of Mockrunner with com.mockrunner as groupid and mockrunner-jdk1.6-j2ee1.3 as artifactid. Problematic artifactIds for me were cglib-nodep and jboss-jee. In case you want to integrate Mockrunner through Maven, here are a couple of steps to make it work:

  • Exclude problematic dependencies inside your project’s pom.xml. The snippet of my pom.xml for doing it :
    		<dependency>
    			<groupId>com.mockrunner</groupId>
    			<artifactId>mockrunner-jdk1.6-j2ee1.3</artifactId>
    			<version>0.4</version>
    			<exclusions>
    				<exclusion>
    					<groupId>cglib-nodep</groupId>
    					<artifactId>cglib-nodep</artifactId>
    				</exclusion>
    				<exclusion>
    					<groupId>jboss</groupId>
    					<artifactId>jboss-jee</artifactId>
    				</exclusion>
    			</exclusions>
    		</dependency>
    
  • Download Mockrunner from the web-site. Copy both the above mentioned jars (cglib-nodep-2.2.jar and jboss-javaee-modified.jar) from downloaded & exploded archive to a subdirectory inside your project. Explicitly mention both these jars as System dependencies in your pom.xml. Code snippet of my pom.xml for your reference:
    		<dependency>
    			<groupId>cglib</groupId>
    			<artifactId>nodep</artifactId>
    			<version>2.2</version>
    			<scope>system</scope>
    			<systemPath>${basedir}/lib/cglib-nodep-2.2.jar</systemPath>
    		</dependency>
    		<dependency>
    			<groupId>jboss</groupId>
    			<artifactId>javaee-modified</artifactId>
    			<version>4.2.0</version>
    			<scope>system</scope>
    			<systemPath>${basedir}/lib/jboss-javaee-modified.jar</systemPath>
    		</dependency>
    

    It’s based on the assumption that you copied the jars inside lib folder of the project.

  • Read more…

Servlet Testing : Requirements and Options

January 14, 2011 1 comment

It may sound a little bizarre in today’s world of multiple frameworks that we would need to write Servlets ourselves. Well that happens to be the case for me at the moment. If you were in a similar situation, you would like a way to test these servlets as well. Here it interesting as there are different opinions in terms of what and how we should be handling Servlet testing. One can get more fine-grained but I would like to broadly classify them in two categories:

  • Integration Testing
  • Functional Unit Testing

Integration Testing Many people believe that Servlets are like glue between your presentation layer and business layer. Most of the business logic is written in separate Java classes which gets just called via servlets. This business layer gets tested using appropriate testing frameworks like JUnit, DBUnit, Mock Objects etc. Testing of Servlets should then implicitly happen through an Integration Testing Framework. In this case, we will typically be interacting through a Servlet Container. If that’s your thought or requirement, following are some of the most popular test-frameworks for the purpose:
Read more…

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: