programmers quotes

February 19, 2009

Five Unit Tests Tips: Parametrized test methods

Filed under: Java,Programming — Tags: , , — Johannes.Brodwall @ 8:27 pm

The following is a trick I don’t use very often, but when I do need it, it comes in very handy. It’s a trick that many developers aren’t aware of, even though it’s been possible to do with JUnit at least since version 3.

Sometimes you want to have a lot of tests that are almost the same, but that contain different arguments. For example, for a yahtzee calculator, you might want to have the following tests:

checkRollScoredAsCategoryGives(1, 2, 3, 4, 5, "straight", 15);
checkRollScoredAsCategoryGives(1, 2, 3, 4, 4, "straight", 0);
checkRollScoredAsCategoryGives(3, 3, 3, 4, 4, "full house", 17);
checkRollScoredAsCategoryGives(1, 2, 3, 4, 4, "full house", 0);
checkRollScoredAsCategoryGives(4, 4, 4, 4, 4, "full house", 0);

This is possible with JUnit, but a little trick. You have to construct a test suite manually and add special subclasses of TestCase to it:

 

public static Test suite() {
    TestSuite suite = new TestSuite(
          YahtzeeScoreTest.class.getName());
    suite.addTest(checkRollScoredAsCategoryGives(
            1, 2, 3, 4, 5, "small straight", 15));
    suite.addTest(checkRollScoredAsCategoryGives(
            1, 2, 3, 4, 4, "small straight", 0));
    suite.addTest(checkRollScoredAsCategoryGives(
            3, 3, 3, 4, 4, "full house", 17));
    suite.addTest(checkRollScoredAsCategoryGives(
            1, 2, 3, 4, 4, "full house", 0));
    suite.addTest(checkRollScoredAsCategoryGives(
            4, 4, 4, 4, 4, "full house", 0));
    return suite;
}

private static Test checkRollScoredAsCategoryGives(
        int die1, int die2, int die3, int die4, int die5,
        final String category, final int expectedResult) {
    final int[] dice = new int[] { die1, die2, die3, die4, die5 };
    return new TestCase("Rolling for " + category) {
        @Override
        public void runTest() throws Throwable {
            YahtzeeScoreboard board = new YahtzeeScoreboard();
            board.scoreRoll(category, dice);
            assertEquals(expectedResult, board.getTotalScore());
        }
    };
}

The code creates an anonymous inner subclass of TestCase that instead of calling all methods annotated with @Test just executes our specific test. These tests are collected in a normal JUnit TestSuite.

The test is plain JUnit and will run in Maven or your favorite IDE, just as any other tests. The fact that the suite is named after the test class will let Eclipse know where to go when you double click on the test from the test runner.

This trick can be very useful for tests of logic that calculates a results or validates input.

February 5, 2009

Language Quotes

Filed under: Programmers Quotes — Tags: , , , — melisa @ 2:00 pm


PL/I and Ada started out with all the bloat, were very daunting languages, and got bad reputations (deservedly).
C++ has shown that if you slowly bloat up a language over a period of years, people don’t seem to mind as much. — James Hague

It should be noted that no ethically-trained software engineer would ever consent to write a DestroyBaghdad procedure. Basic professional ethics would instead require him to write a DestroyCity procedure, to which Baghdad could be given as a parameter.
— Nathaniel S Borenstein

There are only two kinds of programming languages: those people always bitch about and those nobody uses. — Bjarne Stroustrup

Should array indices start at 0 or 1? My compromise of 0.5 was rejected without, I thought, proper consideration. — Stan Kelly-Bootle

Voodoo Programming: Things programmers do that they know shouldn’t work but they try anyway, and which sometimes actually work, such as recompiling everything. — Karl Lehenbauer

Please don’t fall into the trap of believing that I am terribly dogmatical about [the goto statement]. I have the uncomfortable feeling that others are making a religion out of it, as if the conceptual problems of programming could be solved by a single trick, by a simple form of coding discipline!
— Edsger Dijkstra

Historically, languages designed for other people to use have been bad: Cobol, PL/I, Pascal, Ada, C++. The good languages have been those that were designed for their own creators: C, Perl, Smalltalk, Lisp.
— Paul Graham

Computer language design is just like a stroll in the park. Jurassic Park, that is.
— Larry Wall

XML is not a language in the sense of a programming language any more than sketches on a napkin are a language. — Charles Simonyi

Using TSO is like kicking a dead whale down the beach.
— Stephen C Johnson

The object-oriented model makes it easy to build up programs by accretion. What this often means, in practice, is that it provides a structured way to write spaghetti code.
— Paul Graham

Reusing pieces of code is liked picking off sentences from other people’s stories and trying to make a magazine article.
— Bob Frankston

[The BLINK tag in HTML] was a joke, okay? If we thought it would actually be used, we wouldn’t have written it!
— Mark Andreessen

Software is like sex: It’s better when it’s free.
— Linus Torvalds

I had a running compiler and nobody would touch it. They told me computers could only do arithmetic. — Rear Admiral

If you don’t think carefully, you might think that programming is just typing statements in a programming language. — Ward Cunningham

A language that doesn’t have everything is actually easier to program in than some that do. — Dennis M Ritchie

Projects promoting programming in natural language are intrinsically doomed to fail. — Edsger Dijkstra

« Newer Posts