RSS Feed Subscribe to RSS Feed

 

Testing for expected exceptions in JUnit

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.
(more…)

Tags: , , ,

Java8

Java8 isn’t scheduled for for release until March 2014, but early release versions have been available for a while.
Some of the most interesting new features are:

  • Streams
  • Functional interfaces
  • Default methods
  • Lambdas
  • Java Time

(more…)

Tags: ,

Java7 – A look back

I started writing a blog post on what’s new in the upcoming Java8 release, and thought I would start by doing a quick look back at what Java7 brought to us.

Java7 was released back in July 2011, and was described as “more evolutionary than revolutionary”.

“There are some significant improvements, but no really earth-shattering or ground-breaking kinds of features.” – Oracle chief Java architect Mark Reinhold

It didn’t contain the much hyped lambda expressions for example. Still, it did bring a lot of other neat additions to the Java language. You can see the more at http://docs.oracle.com/javase/7/docs/, or read on for my summary.

(more…)

Tags: ,

Using Java8 with Maven in IntelliJ (invalid target release: 1.8)

I recently started trying out the early access version of Java8. When I tried using it with maven in IntelliJ however, I got the following error when I ran the maven install command:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project xyz:

Fatal error compiling: invalid target release: 1.8\

Although I saw some suggestions on stackoverflow here about hardcoded JAVA_HOME paths, the solution was just changing the maven specific version of Java that IntelliJ uses. This can be done via:

File > Settings > Project Settings > Maven > Runner

and selecting the 1.8 version of the JRE.

Tags: , , , ,

Stacks and Queues in Java

I have been working on some Java code recently that required both a stack and a queue. The choices to use aren’t immediately obvious. There is a Queue interface, but no clear concrete implementation to use. There is also a Stack class, but the javadocs point out that other classes “should be used in preference to this class”. So, what implementation do you use for stacks and queues in Java?

I found my quick rule of thumb to be that you can use the ArrayDeque class for both Stack and Queues, but some more details are below.
(more…)

Tags: , , ,

Builder Pattern: Good for code, great for tests

I’ve found the builder design pattern occasionally useful in code, but frequently useful in tests. This article is a quick summary of the pattern in general, followed by look at a working example of using it in tests. See the code in github.
(more…)

Tags: , , ,

SpringMVC file download

This post shows how to download a file using Spring MVC. For example, you have a SpringMVC web app and you want to include a link on a page that downloads a file to be opened on the user’s machine (e.g. a Microsoft Excel).
Read more

Tags: ,

MyMoney – A simple SpringMVC app

I created a small, open source web app called MyMoney for entering and tracking spending details. It allows you to create accounts (for example Cash or Checking) and enter transactions associated with those accounts.

(Previously I had the code deployed on a CloudBees instance at http://mymoney.shaunabram.cloudbees.net, but CloudBees have since unfortunately shut down their free tier). Read more

Tags: , ,

415 ‘Unsupported Media Type’ error when submitting JSON from a JSP

I’ve been experimenting with submitting JSON data from a JSP to a Spring MVC Controller, using the Spring MVC Ajax example as my guide.

But after setting everything up, I continually ran into this error:

NetworkError: 415 Unsupported Media Type
The server refused this request because the request entity is in a format not supported by the requested resource for the requested method().

After much googling, I found a plethora of solutions, none of which worked for me. In the end, I found it was because I had omitted this line from my JSP:

Hope that helps someone else…

Tags: , , ,

Spring MVC Hello World with Continuous Deployment

Oh dear, yet another ‘Hello World!’. But although the functionality is trivial, this little SpringMVC project is complete enough for me to use as a template to bootstrap more complex projects. It consists of:

  • HTML/JSP client
  • SpringMVC server using a Controller/Service/DAO design
  • Maven for build and dependency management

This is an updated version of an older project I created, with the following enhancements:

  • Added a full suite of automated tests (unit, integration and browser based)
  • Added placeholders for JavaScript and images (both can be a little tricky to put in the correct place with SpringMVC)
  • Incorporated into a continuous deployment environment

More details below, but you can find the full source code on this GitHub repository

(Previously I had the code deployed on a CloudBees instance at http://springmvc.shaunabram.cloudbees.net, but CloudBees have since unfortunately shut down their free tier).

Read more

Tags: , , , , , , , , , , ,