Home » Tag Archives: JUnit (page 5)

Tag Archives: JUnit

Testing for expected exceptions in JUnit

junit-logo

Unit tests are used to verify that a piece of code operates as the developer expects it to. Sometimes, that means checking that the code throws expected exceptions too. JUnit is the standard for unit testing in Java and provides several mechanisms for verifying exceptions were thrown. This article explores the options and their relative merits.   Take the following ...

Read More »

Access private fields in unit tests

junit-logo

First of all, let me say out louder, you need to design your code to be testable, so you test your private fields through your public methods. But, (“buts” are the reasons why humans are still programming instead of the computer itself, so be happy here) sometimes you want to and should alter some private fields in order to test ...

Read More »

More Units with MoreUnit

junit-logo

Just over a year ago I wrote a post about working with JUnit in Eclipse. One of the commenters recommended MoreUnit in order to increase testing efficiency even more. Giving it a try I was delighted and the plugin’s keyboard shortcuts were immediately memorized by my autonomic nervous system… Additionally after using MoreUnit for a while I recognized a slight ...

Read More »

Introduction To JUnit Theories

junit-logo

Have you ever read a mathematical theory? It typically reads something like this: For all a, b > 0  the following is true: a+b > a and a+b > b Just typically the statements are more difficult to understand. There is something interesting about this kind of statement: It holds for EVERY element (or combination of elements) of a rather ...

Read More »

Parameterized JUnit tests with JUnitParams

junit-logo

Parameterized unit tests are used to to test the same code under different conditions. Thanks to parameterized unit tests we can set up a test method that retrieves data from some data source. This data source can be a collection of test data objects, external file or maybe even a database. The general idea is to make it easy to ...

Read More »

Run, JUnit! Run!!!

junit-logo

JUnit together with JavaScript and SVN are some of the technologies that programmers often start using without even reading a single blog post let alone a book.  Maybe this is a good thing since they look simple enough and understandable so we can use them right away without any manuals, but this also means that they are also underused. In ...

Read More »

JBoss Drools unit testing with junit-drools

jboss-drools-logo

Recently I’ve been working with a project heavily using JBoss Drools. I am not Drools expert – I am not also very convinced to this framework, or maybe only to particular use case in this project – and I found it quite difficult to write simple, maintainable unit tests for Drools based business rules. That’s how junit-drools was born – ...

Read More »

A JUnit Rule to Conditionally Ignore Tests

junit-logo

I always believed that using @Ignore to deactivate tests is a bad idea. Except, maybe as one way to put tests that fail intermittently into quarantine to attend to them later (as Martin Fowler describes it here). This bears the danger that the test suite decays as more and more tests keep getting ignored and forgotten. Therefore you should have ...

Read More »

3 ways of handling exceptions in JUnit. Which one to choose?

junit-logo

In JUnit there are 3 popular ways of handling exceptions in your test code: try-catch idiom With JUnit rule With annotation Which one should we use and when? try-catch idiom This idiom is one of the most popular one, because it was used already in JUnit 3. @Test public void throwsExceptionWhenNegativeNumbersAreGiven() { try { calculator.add("-1,-2,3"); fail("Should throw an exception if ...

Read More »
Do you want to know how to develop your skillset and become a ...

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!
Get ready to Rock!
To download the books, please verify your email address by following the instructions found on the email we just sent you.

THANK YOU!

Close