Programmers Quotes

Programmers' quotations about programming languages and IT

Upcoming Java Power Tools Bootcamps in Canberra and Sydney, Wellington, London and Paris – Don’t miss out!

Friday Nov 27, 2009


Tags Java

new
All true craftsmen need the best tools

There are still a few places available on the Canberra, Brisbane, Sydney and Wellington sessions of the Java Power Tools bootcamps. Come see what the buzz is about!

And, after a very popular session in London in July, we are working with SkillsMatter to plan next years European schedule. Sessions are already scheduled for Paris and London in February next year.

There are lots of tools, techniques and practices that you can use to speed up your development process, at all levels. But sometimes it’s hard to know what tools exist, or how best to use them. And it can be frustrating and slow going if you have to discover all the tips yourself.

These bootcamps are a great way to improve your development skills across the board no matter what Java technologies you use. Made up of a seris of in-depth tutorials on development tools and practices right across the development life cycle, we go from build scripting and build automation, unit, integration and functional testing, right through to automated deployment. The course is above all pragmatic: at each stage, we look at how you can speed up your development using the latest in Java tools and best practices.

We look at how to use Maven to streamline and standardize your development process, and waste less time with low-level build scripting, and let you take your builds to the next level of productivity.

We also study code quality metrics and code coverage tools such as Checkstyle, PMD, Findbugs and Sonar, and look at the best ways to use these tools to improve your code and train your team. We look at a range of testing tools and techniques, including the latest JUnit 4 features, and other testing tools such as SoapUI, Selenium, easyb, infinitest and testing with Groovy.

We also look at the broader picture: learn how to automate deployment with Cargo, and how to use Continuous Integration, using Hudson and Nexus to bind the whole development and deployment process together, acting as a communications hub for your development team and automating everything from snapshot builds to staging and production releases.

I’ve been getting very positive feedback about the course, both from newer developers and from more experienced ones. I got several comments along the lines of “One of the best and most useful courses I have attended”. Many developers appreciate the global view they get of current Java Best Practices: “This was a great all round introduction to best practices for development process optimization. I found all of the content very helpful and easy to understand.. Others liked the global picture, and the way the course covers not only what tools exist, but when they are appropriate: “Gives a very good overall view of the Java development environment. Not just how to write Java code but the ‘business end’ – how to build, test, deploy, manage and monitor.”

We also offer generous group and public service discounts. So what are you waiting for? Sign up today!

VN:F [1.0.9_379]
Rating: 5.0/5 (1 vote cast)

Web testing BDD-style with JWebUnit and Easyb

Monday Nov 2, 2009


Tags Java

Behaviour-driven development is a great way to design and build the web layers of your application. In this article, I look at how to use JWebUnit, a fast and light-weight web testing framework, with Easyb, a powerful Groovy-based BDD framework.

JWebUnit is a web testing framework built on HTMLUnit. It runs in memory, so it’s fast. However, it also has an intuitive, high-level API, making it easy to use even if you have only an approximate idea of the exact HTML details of your application screens. A simple JWebUnit test case is shown here:


import net.sourceforge.jwebunit.junit.WebTestCase;
import net.sourceforge.jwebunit.junit.WebTester;

public class TestJWebUnitDepositStoryUI extends WebTestCase {

	public void setUp() {
		setBaseUrl("http://localhost:9090/ebank-web");
	}

	public void testDepositingCashShouldAddToBalance() {
		beginAt("/");
		assertTextPresent("Current Balance: $0");
		setTextField("depositAmount","100");
		clickButtonWithText("deposit");
		assertTextPresent("Current Balance: $100");
	}
}

JWebUnit works fine like this, but we can still do better by adding a little BDD-flavoring to the mix. As it turns out, behaviour-driven development (or “behaviour-driven design”) is a great way to model user interaction with your application. In BDD, you structure your requirements in a “given [some condition or state], when [some event occurs] then [we expect something else to happen]“. For example, in an online banking application, one such requirement could be “Given that I am logged onto the bank web site and my bank account balance is $0, when I deposit $100, then the new balance should be $100.”

Easyb is a behaviour-driven development (BDD) framework for Java and Groovy applications. It is based on Groovy, but uses a special DSL to express requirements and tests in BDD-style. Like many BDD frameworks, easyb lets you express your requirements (more precisely, your user’s behaviour) in almost plain English, before filling in the details with test code. For example, here is a working Easyb test scenario describing how we might interact with our banking application:


scenario "Deposit cash via the web interface", {
    given "the application home page is displayed"
    and "the current balance is 0"
    when "I type \$100 in the 'deposit cash' field"
    and "I click on the 'deposit' button"
    then "the new balance should be \$100"
}

As far as Easyb is concerned, this is executable test code – however it won’t do very much. When you run Easyb against a scenario like this one, it will be flagged as “pending implementation”. So now let’s see how we can implement this test using JWebUnit.

JWebUnit test cases need to extend the WebTestCase base class. This makes the framework hard to integrate with JUnit 4 or with pure Java-based BDD frameworks such as JBehave. However, Easyb is built on Groovy. With a little Groovy magic, we can embed a JWebUnit client inside our Easyb test scenarios. The only trick is that we still need to have a class that extends WebTestCase, as it is an Abstract class. We also need to initialize the internal ‘tester’ member variable with an instance of the WebTester class. A simple Groovy class like this will do the trick.


class DepositCashWebClient extends WebTestCase {

    public DepositCashWebClient() {
        tester = new WebTester();
    }
}

We also need to create a new instance of this class for each test case. We can do this using the Easyb “before_each” keyword, as shown here:


before_each "initialize a web client", {
  given "web client is up and running", {
      webClient = new DepositCashWebClient()
      webClient.setBaseUrl("http://localhost:9090/ebank")
  }
}

Our full Easyb test story looks like this:


import net.sourceforge.jwebunit.junit.WebTestCase;
import net.sourceforge.jwebunit.junit.WebTester;

class DepositCashWebClient extends WebTestCase {

    public DepositCashWebClient() {
        tester = new WebTester();
    }
};

before_each "initialize a web client", {
  given "web client is up and running", {
      webClient = new DepositCashWebClient()
      webClient.setBaseUrl("http://localhost:9090/ebank")
  }
}

scenario "Deposit cash via the web interface", {
    given "the application home page is displayed", {
      webClient.beginAt("/")
    }
    and "the current balance is 0", {
      webClient.assertTextPresent("Current Balance: \$0")
    }
    when "I type \$100 in the 'deposit cash' field", {
      webClient.setTextField("depositAmount","100")
    }
    and "I click on the 'deposit' button", {
      webClient.clickButtonWithText("deposit")
    }
    then "the balance should be \$100", {
      webClient.assertTextPresent("Current Balance: \$100")
    }
}

And running these easyb scenarios will generate you a report that looks like this:

Picture 5.png

Although this seems longer than the Java version, it is arguably more readable, and reflects the intention of the code more accurately than the Java equivalent. The test report also makes it quite clear what requirements are being tested, in very readable terms. Writing the tests using a BDD approach also helps you keep a good perspective on how closely your application actually does what your users have asked for. Reports like the one above can also give users and testers a better understanding of what is being coded.

I will be talking about EasyB and BDD along with Lasse Koskela at Agile 2009 next week in Chicago, in a talk called Executable requirements: BDD with easyb and JDave. So, if you are interested to learn more about BDD, come along!

And if you want to learn more about Behaviour-Driven Development, web testing, Maven, and a whole lot of other useful tools and tricks, why not come along to a Java Power Tools bootcamp session – there are workshops coming up soon in Canberra, Sydney, Brisbane and Wellington.

“Probably the best training course I’ve been on.”…”Not just how to write Java code but the ‘business end’ – how to build, test, deploy, manage and monitor”…”One of the best and most useful courses I have attended. And they didn’t even try to sell me anything!” - There are still some places for the courses in Wellington, Canberra, Brisbane and Sydney – Get up to speed with the latest and coolest in Java tools and best practices! Sign up on the 2009 season of the Java Power Tools Bootcamps.

VN:F [1.0.9_379]
Rating: 4.0/5 (1 vote cast)

>