Yes... I've heard about Spock ;-) and most likely dozens of other testing frameworks which were created after JUnit started to exist. JUnit is old, it's sometimes a bit grumpy, nevertheless it is stil very concise and lightweight library. It is enough to spend 10 minutes with JUnit documentation to understand the basic elements of test, how to write an assertion and how to launch your first test. In a day to day job there are several other JUnit features which might be very handy, there are also a few curosities worth to mention. In this presentation I will try to cover most of them including: parameterized tests, theories (org.junit.experimental.theories), rules (@Rule) and common matchers. As was already said JUnit is not a newbie in a testing framework world, over the years several additional libraries were added, I will show you a few interesting once, including randomized testing, benchmarking and REST interactions testing.
This presentation was developed with the support of Poznań JUG, Tricity JUG and Szczecin JUG who were hosting this lecture in the past. Discussion which we had during and after the meetings was a great aid and influenced heavily current content of this presentation.
Adam Dudczak - Adam Dudczak (@maneo), software engineer in Allegro group, working with Java (and JUnit) since 2004. Currently member of Allegro search team working on a better search experience in Allegro.pl. One of the leaders of Poznań JUG (www.jug.poznan.pl) and co-organizer of GeeCON conference (geecon.org). Father and husband, occasionally blogs at dudczak.info/dry.
2. WHO AM I?
Software Engineer in Allegro group
Workingwith Java(and JUnit) since 2004
One of the leaders of
Co-organizer of conference
Poznań JUG
GeeCON
18. JUNIT-PARAMS
More examples can be found at iSamples_of_Usage_Test.java
PersonTest.java
Similar to approach in TestNG
Testcode is clear and concise -YEAH!!
20. ORG.JUNIT.EXPERIMENTAL.THEORIES
Theorydescribes features of class/method and verifies them
usinggiven setof inputdata
Theoryis executed as one test
If assertion fails for anysetof inputdatawhole theoryfails
Example in ErrorRate_05_Theory_Test.java
26. @RULE
Resuable @Before/@After... and more
Example in JunitRulesShowcaseTest
JUnithas severalbuilt-in @Rules, ex.:
ExternalResource, ExpectedException, TestName...
35. ALTERNATIVE APPROACH - FEST/ASSERTJ
Alternative (butin factamainstream) wayof building
assertions
gives access to hundreds of assertionsAssertJ
assertThat(frodo.getName()).isEqualTo("Frodo");
assertThat(frodo).isNotEqualTo(sauron)
.isIn(fellowshipOfTheRing);
assertThat(sauron).isNotIn(fellowshipOfTheRing);
37. CATEGORIES/SUITES AND BUILD TOOLS
Suite is abitAnt-ish -use Categories
Categories are supported byboth andMaven Gradle
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<groups>com.ex.FastTests,com.ex.RegressionTests</groups>
</configuration>
</plugin>
</plugins>
</build>
38. USE CATEGORIES ON THE @SUITE LEVEL
publicclassHeavyIntegrationTest{
@Test
@Category(HeavyWeight.class)
publicvoidshouldCalculateErrorRate(){
assumeTrue(isHeavyWeightServerRunning());
//heavestuffwithheavyWeightserverhere
}
...
39. USE CATEGORIES ON THE @SUITE LEVEL
@RunWith(Categories.class)
@IncludeCategory(HeavyWeight.class)
@SuiteClasses({
ErrorRate_01_SimpleTest.class,
HeavyIntegrationTest.class
})
publicclassSuiteWithCategories{
//categorymarkerinterface
publicinterfaceHeavyWeight{}
...