ant and junit 5

Ant now supports JUnit 5. The CodeRanch software uses Ant (because internet connections vary around the world). This blog post describes how I upgraded.

What isn’t supported

While doing this, I learned that not everything supported in JUnit 4 for Ant is currently supported with JUnit5. In particular:

Preparing my environment

Updating Jars for Ant

Ant’s JUnitLauncher page gives a list of the required jars. I decided to download them directly from a Maven repository rather than using the copies in my local repository so I have the latest ones. I grabbed the jars needed for both JUnit 4 and 5 so I could test transitioning.

  • junit-platform-commons.jar
  • junit-platform-engine.jar
  • junit-platform-launcher.jar
  • opentest4j.jar
  • junit-vintage-engine.jar
  • junit-jupiter-api.jar
  • junit-jupiter-engine.jar
  • junit-jupiter-params.jar (I do a lot of junit parameterized testing)
  • apiguardian-api (dependency for junit parameterized testing)
  • junit.jar (use legacy junit jar for migration)
  • hamcrest-all.jar (not sure why needed now and not before)

I copied all these jars to the lib directory of my Ant install. If you forgot to do this before the next step, update the Eclipse preferences again now. Otherwise, you will get this message when running a build.

java.lang.NoClassDefFoundError: org/junit/platform/launcher/core/LauncherFactory

Switch Eclipse to the JUnit 5 runner but run JUnit 4 tests

This is easy.

  • Updated Eclipse preferences to point to it (Ant > Runtime – Ant Home)
  • Add the JUnit 5 library to your project’s classpath
  • Run > Run Configurations
  • Select the launch config for your test runner(s)
  • Change the test runner pull down to select JUnit 5
  • Observe the tests still pass

Switch Ant to the JUnit 5 runner but run JUnit 4 tests

Unlike Eclipse, this is not easy.

Updated Ant file

    • removed JaCoco wrapper for JUnit Ant task (was using this to give Sonar the test coverage – need to investigate the replacement)
    • removed code for setting custom system property – replaced with code described in setting System Property blog post
    • replaced the <junit> tags with <junitlauncher> tags
    • removed the attributes that are not supported by the new tag: showoutput=”no” fork=”yes” forkmode=”once”
    • changed printsummary attribute value from “yes” to “true”
    • replace batchtest tag with testclasses tag and change attribute to outputdir
    • switch formatter tag to listener tag using new attributes
    • added to nested classpath in junitlauncher task <pathelement location=”${junit.jars.dir}”/>
    • changed fileset to use .class instead of .java in matching
    • removed code to pass in all existing system properties. (since JUnit being run in the same VM, this is no longer necessary)
      <!-- Pass along all the system properties to the junit task -->
      <syspropertyset>
        <propertyref builtin="all"/>
        </syspropertyset>
      

Actually migrating the code to JUnit 5

While JUnit 5 can run JUnit 5 tests, I decided to migrate them all.

  1. Migrate core assertions/imports
    1. Ran the program I wrote to migrate most of the pieces.

Migrating the unit tests

  1. Created launch configurations to replace old runners
    1. Removed “all test runner”. Can do this in modern IDEs without the old Classpath Suite
    2. Right clicked Eclipse project and choose run as junit test. Saved this as my new JUnit 5 launch config favorite so can run all tests in one click.
    3. Repeated right clicking /src/test/java (we have separate folders for unit and integration tests) to create a launch config for only unit tests). I should change this to the Maven naming convention of IT
  2. Migrated Mockito code (we had about 50 affected classes in JForum)
    1. In Eclipse project, did search for @RunWith(MockitoJUnitRunner.class) and @RunWith(MockitoJUnitRunner.Silent.class)
    2. Right click in search view > Replace All
    3. Replace with: @ExtendWith(MockitoExtension.class)
    4. Right click project > Source > Organize imports
  3. Migrated parameterized tests by hand (we only had 1)
  4. Migrated the one test with a timeout by hand
  5. Removed a custom assertArrayEquals() method we had written (presumably before JUnit had it). The migration program changed the order of parameters in the method call, but not the custom method so it didn’t compile.
  6. Changed one assertThat manually. It was using a matcher in an odd way.
  7. Now that the code compiles, ran the unit tests. 43/2416 failed.
  8. We were missing a setup call to load a property file in a commonly used superclass. (This appeared to work before due to a different order of test runs). Fixing that one test brought it to 19 failures.
  9. The remembered I forgot to migrated tests that used (expected=MyException.class) to use the new assertions – found these by searching for FIXME). That was the rest.
  10. Finally, I had two tests that relied on special encoding which the converter program broke. I rolled back these parts manually.

Migrating the integration tests

  1. We add a JUnit Runner for all our functional test. It loaded an in memory database (or real database depending on your configuration). I migrated this to an extension. The code is a lot clearer as an extension which is nice.
  2. Then I search/replaced @RunWith(JavaRanchFunctionalTestRunner.class) with @ExtendWith(CodeRanchFunctionalTestExtension.class) and did an organized imports on the folder for the functional tests.

Removing JUnit 4

After migrating, I removed JUnit 4 and the vintage engine from:

  • Eclipse project classpath
  • Jar file in project
  • Ant install’s lib directory

 

 

ant and junit5 – simulating sysproperty

In JUnit 5, you use the junitlauncher Ant task rather than the junit Ant task. Unfortunately, this isn’t a drop in replacement. For example, junitlauncher doesn’t offer the option to fork and run the JUnit tests. As a result, it also doesn’t have the nested sysproperty tag so you can pass system properties. This is a problem.

For the CodeRanch, we set a system property for the default file encoding. Since developers are around the world, we can’t assume everyone “just has” the encoding set.

Disclaimer

Since JUnit 5 functionality for Ant was introduced this year, I’m hoping what I did in this post is a short term workaround.

Option 1 – pass to Ant

You can pass the the properties to Ant itself as described on Stack Overflow. For example:

__JAVA_OPTIONS=-Dfile.encoding=ISO8859_1

pros:

  • simple

cons:

  • subpar – all the developers need to remember to do this. The reason we had it in Ant in the first place is so folks wouldn’t need to remember
  • for some use cases, the desired system properties could be derived and not know when calling Ant

Option 2- Nashorn Code

Since JUnit 5 is being run in the same process as Ant itself, you need to set the system property in memory. Luckily, Ant allows you to run scripting in various languages. I chose Nashorn because it is built into Java. (There are other variants of this; see below)

<script language="javascript">
  <![CDATA[
    var imports = new JavaImporter(java.lang.System);
    imports.System.setProperty('file.encoding', 'ISO8859_1')
  ]]>
</script>

Pros

  • Short and simple
  • Nashorn is deprecated for removal starting Java 11. This means at some point, it can be removed. (I’m hopeful that the Ant task itself will support system properties by then.

Cons

  • Requires Java 8 or higher
  • The System property is set for the remainder of the build (you could write another code block to null it out after the test if this is a problem)

Variants of option 2

If you are running Ant with a version of Java below Java 8, you could use this technique, but use Rhino instead. I didn’t test this, but I think the code is


importClass(java.lang.System);
System.setProperty('file.encoding', 'ISO8859_1')


JUnit 5 itself uses Java 8 so nobody would be in the situation of pre-Java 8 and trying to use this blog post.

If you are running Ant with a version of Java where Nashorn has been removed, you could use Groovy or Jython as the embedded language. The code is simpler. I didn’t chose this because it requires adding another jar to the Ant directory. I prefer to minimize these set up extensions. Especially for a feature like this which is likely to be temporary.

System.setProperty('file.encoding', 'ISO8859_1')

my nine month experiment with safari books online

I’ve completed an experiment of using Safari online for nine months. I learned it’s not for me. I also learned specifically why. So keep reading. You may find that one or more of these don’t apply to you.

My bias

I love paper books. We spend so much time with screens; I like to read on paper. It’s relaxing and uses the eyes differently. Also for computer books, I learn tactically. I remember where things are on pages. I use page numbers to see where I am in the chapter and create mental anchors. I read mainly on the subway while commuting.

I do read books electronically on occasion; usually when editing/proofing books that aren’t in print yet. These are in the form of Word docs or PDFs.

What I tried to read (and what I thought)

  1. Core Java SE 9 for the Impatient” – I picked this as my first book to attempt to read. I love Cay Horstmann’s work. And I’m pretty familiar with what is in Java 9 so I figured this wouldn’t be a terribly hard read. Learning something new while getting used to the Safari reader seemed overly ambitious.
    • I read two chapters before I got too fed up with the O’Reilly “Queue” reader.
    • My biggest problem was that it was hard to figure out where I was within a chapter. There are scroll bars, but I want to see the mapping to chapter subheadings like in the table of contents. I can view these subheadings and go to them, but not see where I am. Pagination matters!
  2. Effective Java” – Ok so maybe the problem was that the previous book was too easy and I couldn’t get into it. I did want to read the revamped Effective Java book. And it has very short sections – there are 90 tips in about 400 pages. So I figured I wouldn’t be impacted with the problems I had with Core Java. Well, I had two new ones.
    • The code blocks don’t fit on the  screen so you have to use two finger horizontal scroll to read the code. Alternatively, you can click a link to view the code as an image. The image is all the code in the book in various fonts. It’s just as hard to follow. Besides either of these approaches break flow and make it harder to read/understand the text and how it relates to code.
    • I also learned that even with the short sections for each item, I was losing the spatial memory of being in a specific place on the page for reading. (This problem isn’t as bad with e-book PDFs when I review because there are page numbers and page break so you still have some level of anchoring.)
    • Oh and you can highlight text for notes in the iPad app, but the only way I can find to view the highlights/notes in one place is on the web site. Grumble.
    • I still want to read this book, but I’ve given up on reading the Safari version and ordered a paper version. And I paid for the paper version. I might have been able to get a review copy in December from CodeRanch, but I didn’t ask then because I thought I’d read it on Safari.
  3. My books – I flipped through parts of my books. I know the contents of these books well having written them. And I was just checking out the format so the anchoring/pagination problems weren’t a big deal. Or so I thought.
    • First of all, publishers report errata by *page number*.  Getting rid of the page numbers does the reader a disservice. Especially on certification study guides where errata matter a lot.
    • The rendering is bad. I’ve seen the official e-book of my titles. The official e-books look nice. The Safari book is nothing like them. On the assessment, the answers have numbers instead of letters. (The answer key of course uses letters so this is annoying.)
    • The code doesn’t fit on the screen without scrolling despite the fact that there over an inch of whitespace on the side of each code block in the rendered version.
    • The excessive whitespace margins are annoying in and of themselves. It means less text appears on the screen. Computer book authors put effort into making sure certain things are visible at the same time for comparison/reference when learning. All this is lost.
  4. Introduction to DevOps with Chocolate, Lego and Scrum Game” – Last try. This book is relatively short (140 pages), doesn’t have code and is light reading. I still had to horizontal scroll in a couple places for tables. At least this book I was able to get through in Safari online.

What I didn’t try

I didn’t watch any videos. I prefer to learn by reading vs video. (probably because I want to be reading when I’m not at my computer and use my computer time for actually coding or typing.) I could watch a video on my iPad, but then I’d have to juggle another item to take notes. Yuck.

I didn’t read the e-books in a browser on my computer. A quick scan says the code does fit on the screen there, but there still isn’t pagination.

Other opinions

During this experiment, I asked others what they thought of Safari Online. Two mentors at the robotics lab felt it was too expensive. I also asked at CodeRanch in this post. Highlights:

  • Bear (a very senior developer and successful author who reads e-books exclusively) said he wasn’t impressed during the free trial and PDFs are far superior.
  • Tim C (who has a free subscription at work) uses it occasionally for copy/paste but prefers paper book.
  • two people comment the material they need is already online for free

What is good about Safari Online

I imagine the videos are good. And being able to reference so many books or read as much as you want is a nice benefit.

Why Safari Online isn’t for me

For books the decision to not have pagination ruins it for me. As does the poor highlighting/note taking ability. DRM is all well and good but I find the iPad app to be far inferior to a PDF as far as e-books go.

The other problem is the cost. I typically read 10 computer books a year (cover to cover). Granted some are books I borrowed from others or are books I get for free for reviews. But even if I bought them all, that would cost roughly the same as a Safari online membership. ($39/month.)

Personally, I’d rather have the physical books to show to others, reference my notes in, etc. I’m going to request my license either be transferred to someone else or not renewed for next year.

Disclosure: my employer paid for the year of Safari Online so this experiment only cost my time.