The Source for Java Technology Collaboration
User: Password:



Tom White

Tom White's Blog

Hamcrest

Posted by tomwhite on December 22, 2006 at 12:27 PM | Comments (5)

In Literate Programming with jMock I enthused about jMock's idea of constraints and flexible assertions. Now the jMock team has released version 1.0 of Hamcrest, the constraints part of jMock.

Hamcrest matchers (what were called constraints in jMock) are actually useful for more than just writing unit tests, but it is their application in writing assertions where they really shine and will probably see most use.

So now I can write

assertThat(a, equalTo(b))

or even

assertThat(a, is(equalTo(b)))

rather than JUnit's

assertEquals(b, a)

Apart from being more readable, assertThat takes any matcher as the second argument, so I can combine matchers or even write my own rather than creating an ever growing listof assertXxx methods. For example, I can say such things as

assertThat(collection, hasItem(anyOf(is(item1), is(item2))));

And since the matchers (and the assertThat method) are accessed using static imports, I can use them in any test framework I like.

So why not read the tutorial and give Hamcrest a go?


Bookmark blog post: del.icio.us del.icio.us Digg Digg DZone DZone Furl Furl Reddit Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment

  • doesnt work with code completion.

    Posted by: colt_nz on December 22, 2006 at 02:20 PM


  • There's a wider problem here with code completion for static imports: supporting completion for all accessible (and not yet implemented) members is expensive. There's a proposal for Eclipse so users can have a list of their favorite static imports, but until this is available you have to manually add the following two imports to get completion:


    import static org.hamcrest.MatcherAssert.assertThat;
    import static org.hamcrest.Matchers.*;

    You could set up a template for unit tests to make this go a bit quicker.

    (I don't know how others IDEs compare.)

    Tom

    Posted by: tomwhite on December 23, 2006 at 01:20 PM

  • Dear Tom,

    I've been working on a similar idea (thanks to Nat Pryce) for TestNG-Abbot ( http://code.google.com/p/testng-abbot/ ). Just yesterday I checked in the package 'org.testng.abbot.assertions.Assert' ( http://testng-abbot.googlecode.com/svn/trunk/testng-abbot/src/java/org/testng/abbot/assertions/ ) that will let you do this:

    assertThat(a).isEqualTo(b)

    Which IMHO reads better, requires only one static import and is "autocomplete"-friendly. I will appreciate your feedback :)

    Alex.

    Posted by: alruiz15 on December 28, 2006 at 07:29 AM

  • (Same comment, just better formatting) :)

    Dear Tom, I've been working on a similar idea (thanks to Nat Pryce) for TestNG-Abbot. Just yesterday I checked in the package org.testng.abbot.assertions.Assert that will let you do this:


    assertThat(a).isEqualTo(b)


    Which IMHO reads better, requires only one static import and is "autocomplete"-friendly.


    I will appreciate your feedback :)


    Alex.

    Posted by: alruiz15 on December 28, 2006 at 09:18 AM

  • I've been working on a similar idea (thanks to Nat Pryce) for TestNG-Abbot ( http://code.google.com/p/testng-abbot/ ). Just yesterday I checked in the package 'org.testng.abbot.assertions.Assert' ( http://testng-abbot.googlecode.com/svn/trunk/testng-abbot/src/java/org/testng/abbot/assertions/ ) that will let you do this: assertThat(a).isEqualTo(b) Which IMHO reads better, requires only one static import and is "autocomplete"-friendly. I will appreciate your feedback :) Alex

    music-gratuite
    musique-gratuite
    musiques-gratuites
    nero
    nero-gratuit
    noire
    nu
    nude
    nudiste
    orgasme
    orgie
    parole-gratuit
    paroles-gratuit
    partition
    partitions
    penis
    photo
    photo2
    photo-gay-gratuit
    photo-porno-gratuit
    photo-sexe-gratuit
    photo-sex-gratuit
    pied
    pipe
    poitrine
    porn
    porn-gratuit
    porno
    porno2
    pornographie
    porno-gratuit
    pps-ppt
    programme
    pussy
    rasee
    real-player
    recette
    recettes
    rousse
    sado
    safari
    salope
    sex
    sex2
    sexe
    sexe2
    sexe-amateur-gratuit
    sexe-gratuit
    sex-gratuit
    sex-gratuit-beurette
    sexuelle
    sexy
    shareaza
    skype
    sms-gratuit
    sodomie
    soluce
    solution-jeu
    spectacle
    sport
    sportive
    spybot
    suce
    suceuse
    sudoku-gratuit
    tarot
    tarot-gratuit
    tatouage
    tatouages
    teen
    tele
    telechargement
    telechargement2
    telechargement-antivirus
    telechargement-chanson
    telechargement-chansons
    telechargement-divx
    telechargement-emule
    telechargement-film
    telechargement-film-gratuit
    telechargement-gratuit
    telechargement-kazaa
    telechargement-logiciel
    telechargement-logiciel-gratuit
    telechargement-logiciels
    telechargement-messenger
    telechargement-movie
    telechargement-mp3
    telechargement-msn
    telechargement-music
    telechargement-musique
    telechargement-nero
    telechargement-parole
    telechargement-paroles
    telechargement-porn
    telechargement-porno
    telechargement-sex
    telechargement-sexe
    telechargement-x
    telechargement-xxx
    telecharger
    telecharger2
    telecharger3
    telecharger-antivirus
    telecharger-antivirus2
    telecharger-chanson
    telecharger-chansons
    telecharger-divx
    telecharger-emule
    telecharger-film
    telecharger-gratuit
    telecharger-jeu-gratuit
    telecharger-kazaa
    telecharger-logiciel
    telecharger-logiciels
    telecharger-messenger
    telecharger-movie
    telecharger-mp3
    telecharger-msn
    telecharger-music
    telecharger-musique
    telecharger-nero
    telecharger-parole
    telecharger-paroles
    telecharger-porn
    telecharger-porno
    telecharger-sex
    telecharger-sexe
    telecharger-x
    telecharger-xxx
    television
    tennis
    tit
    toon
    tourisme
    touristique
    tout-gratuit
    traducteur-gratuit
    transsexuelle
    truc
    tuning
    tv
    vacances
    video
    video2
    video-comique
    video-gratuit
    video-porno
    video-porno2
    video-porno-gratuit
    videos
    videos2
    videos-comiques
    video-sexe-gratuit
    video-sex-gratuit
    video-x-gratuit
    vlc
    voiture
    voyage
    voyager
    voyeur
    wallpaper
    webcam
    winamp
    winmx
    winrar
    winzip
    x
    x2
    x-gratuit
    xxx
    xxx-gratuit
    yoga
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    120
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    130
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149

    Posted by: josemelen on March 17, 2007 at 09:01 AM



Only logged in users may post comments. Login Here.


Powered by
Movable Type 3.01D
 Feed java.net RSS Feeds