Wednesday 27 January 2010

HibernateTemplate/ Hibernate Session - difference between .get() and .load()?

In short: .get() retrieves it from db, while load() just gets it from
current session cache. Or that's how I understand this article.

http://gmarwaha.blogspot.com/2007/01/hibernate-difference-between-sessions.html

Thursday 21 January 2010

EasyMock gotchas

1. Using Easymock matchers when passing in known values to mock classes.

When we get errors like:

java.lang.IllegalStateException: 2 matchers expected, 1 recorded.
at org.easymock.internal.ExpectedInvocation.createMissingMatchers(ExpectedInvocation.java:42)
at org.easymock.internal.ExpectedInvocation.<init>(ExpectedInvocation.java:34)


It means we are missing a matcher for our mocked classes.

Take this code, for example:

Map<String, Object> params = new HashMap<String, Object>();
List<Integer> groupIds = Arrays.asList(1,2,3);
params.put("groupIds", groupIds);

expect(mockSimpleJdbcTemplate
.queryForList((String)anyObject(), params))
.andReturn(results);


Replace the last line with:

expect(mockSimpleJdbcTemplate
.queryForList((String)anyObject(), eq(params)))
.andReturn(results);

"If you would like to use matchers in a call, you have to specify matchers for all arguments of the method call."

Otherwise, it should only be known values.

If you are mixing known values with matchers, you will have to use the eq() ("equals) static method

See: http://www.easymock.org/EasyMock2_2_Documentation.html


2. when mocking classes that are not an interface, like
SimpleJdbcTemplate, we need to use the createMock, replay, and verify
methods FROM org.easymock.classextension.EasyMock NOT FROM
org.easymock.EasyMock


otherwise, we get this error when running the test:

java.lang.IllegalArgumentException: not a proxy instance
at java.lang.reflect.Proxy.getInvocationHandler(Proxy.java:637)
at org.easymock.EasyMock.getControl(EasyMock.java:1440)
at org.easymock.EasyMock.reset(EasyMock.java:1397)


Update 11 November 2010:
You no longer need to do the easymock.classextension package in (2) if you are using EasyMock 3. The package and classes still exist in EasyMock 3 only for backward compatibility. See the documentation.

Saturday 16 January 2010

SRSLY? My email to my work address bounced because it had the word "SHIT"

"Delivery to the following recipient failed permanently:

<username removed of course>@optus.com.au

Technical details of permanent failure:
Google tried to deliver your message, but it was rejected by the
recipient domain. We recommend contacting the other email provider for
further information about the cause of this error. The error that the
other server returned was: 550 550 5.7.1 Inappropriate language
used:SHIT (state 18)."

Unfuckingbelievable.

I wonder if Telstra do the same to their work emails.

Monday 11 January 2010

WTF? Error message that has 1st letter removed from each package name.

Got this error message when trying to run a test in IntelliJ IDEA 9.

NoClassDefFoundError: org/pache/ommons/bcp/asicDataSource

I don't really know what's going on, but somehow each of the package
groups has the 1st character missing. Bizarre.

I'll clean out the IDEA cache using "File --> Invalidate Caches" and
see if that fixes it.

Nope, didn't.

Further down the stacktrace it shows the right name:

Caused by: java.lang.ClassNotFoundException:
org.apache.commons.dbcp.BasicDataSource
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)