Igor's Blog
Programming, DIY, Games, Hacks, and Tech

In a big code porting project recently I had to move a number of unit tests from one class over to another. I didn't give it a second thought beyond that until it came to running the build. Compilation was successful, however JUnit execution failed on the tests I moved. Odd. At first I thought maybe I accidentally changed some code, but no, all the code was the same. Then I realised what the problem was.

The class that I moved these tests from was written for JUnit 4.x and used the @Test annotation. However the class that originally had these tests was written for JUnit 3.x and was extending the TestCase class and no annotations.

This meant that my perfectly legitimate test case like that looked like this...
 JUnit 4.x test case
@Test(expected = MyException.class)
public void test_myExceptionThrowsTest1() throws MyException {
/* some code here that throws MyException */
}


...would always cause my build to fail when executed inside a test case designed for use with JUnit 3.x like this...
 JUnit 3.x test case
public class MyTest extends TestCase {
...
}




The solution was of course not to mix the two approaches. In my case, I removed the 'extends TestCase' from the class and added @Test annotations to every other test case inside it. Once that was done, all my tests were passing again.

I've come across this article while debugging the issue above, it's worth a read when migrating from JUnit 3.x to 4.x - Junit 3 vs Junit 4 Comparison.

-i

Please leave your comments or feedback below!
comments powered by Disqus
Other posts you may like...

Recent Blog Posts

How to enable the full stack trace in Maven's Surefire plugin for JUnit testing

Twelve elements of the Burst Mining Pool interface explained

TPG FTTB settings for the Billion BiPAC 8700AXL 1600 modem router

Protecting old Atari Lynx game boxes with snug fit plastic sleeves

How to fix SoapUI javax.net.ssl.SSLHandshakeException calling WebLogic 12.2 web services on Java 8

Woolworths (WOW) shares disappeared from Computer Share Investor Centre

Connecting the Dell UltraSharp U3415W monitor to a MacBookPro via USB-C

How to add/change PHP versions appearing in MAMP preferences

Fix the ORA-00904: ORA_ROWSCN: invalid identifier error in SQLDeveloper with a few easy steps

G Suite Gmail is broken on Safari due to new Google Content Security Policy settings

Recent Galleries

Protecting old Atari Lynx game boxes with snug fit plastic sleeves

Monument Valley 2 is released and does not disappoint

Space Food - Chocolate Ice Cream with Chocolate Chips

Legeod Star Wars AT-DP kit

DIY spare parts computer build with a RAIDMAX Anura case

Fake 'Lepin' brand Lego packaging

Hardwood garden bench with clear resin void filler

Fixing a 3D printer extruder that stopped heating up

Easily increase disk space in a Lenovo Ideapad 100S 14" laptop with an M.2 SSD

Making a multi-piece 3D printed solder spool holder stand

My Other Web Sites

Igor and Elise's Travels
Riverside Expressway Cam
300 George St Blogumentary

My Online Tools

UUID to OID Converter
Guru JSON-RPC Tester
Extrudifier Object Designer
Travel ┬ÁBlog

Blogs and Friends

Matt Moores Blog
Georgi's FlatPress Guide
Perplexing Permutations
The Security Sleuth
Ilia Rogatchevski
Travelling Fairy

Blog Activity

Blog Activity
Don't forget to
my Facebook page for more great articles!
Don't show this again