Thursday, 25 August 2016

Unit tests with mockito - how to verify that a parameter to a mock method call has an expected value

Use ArgumentCaptor, then in your verify() call for the method call you're setting up, use the argumentCaptor instead of the usual .class you'll be passing.

Then in your assert() to check the value, see if the argumentCaptor.getValue().get<field> has the value you're expecting

Basic idea is that you use ArgumentCaptor as the proxy to intercept the value the mock is getting.

Wednesday, 27 January 2016

JSTL transforms null BigDecimal to 0 and null String to ""

Was scratching my head why I was getting zero for a BigDecimal that I was returning as null, then realised I'd forgotten about JSTL behaviour that cleans up nulls to nicer values.

So anything extending type Numeric that is null gets shown as 0.

And anything that is a null String becomes a blank String.

As mentioned here:

After a bit of trial and error I discovered that the JSP tab library framework does a little bit of magic on known classes, such as Strings and Numericobjects. Classes of Numeric types that are null get converted to 0 andStrings get converted into the empty string ""

I discovered that if you change your internal value to anjava.lang.Object instead of a java.math.BigDecimal the magic doesn't know what to do and just passes null to your class.