Skip to content

Category Archives: UnitTest

Mock Unit Testing Services and Daos with Mockito and Spring 3

If you haven’t been mocking yet, check this out. There are other tools in Groovy using DSLs but if you want to stick to Java, Mockito is by far the best BDD-style mock unit testing tool. Traditional examples provide a List mocking example or foo/bar example which I find it mostly annoying since it doesn’t [...]

Spring Security 3 Unit Testing Example with DbUnit

This tutorial extends on the baseline laid by Spring Security Tutorial maven archetype. I’ve modified the spring security config file and will be unit testing a simple ProfileService (CRUD) on a Profile entity (User object). The tests include login/logout and spring services security unit testing. application-security.xml <?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns="http://www.springframework.org/schema/security" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd [...]

Unit Testing JavaScript with HtmlUnit and Screw.Unit

Writing apps with today's rich client libraries such as Extjs , YUI , and JQuery really shows how much potential is still left in web development. But it takes a lot of code, and the amount of technical debt can get heavy, quickly. Good tests can help reduce this technical debt, and there are a lot of testing [...]

Grails and HtmlUnit

I was struggling to add HtmlUnit to Grails to do some testing when I came upon this. The solution was to remove the HtmlUnit included jar, xml-apis-1.3.04. The error looked like this: loader constraint violation: loader (instance of &amp;lt;bootloader&amp;gt;) previously initiated loading for a different type with name "org/w3c/dom/NamedNodeMap" java.lang.LinkageError: loader constraint violation: loader (instance of ) [...]

JavaScript Unit Testing

TechTalk: Testing Rich Client Web Applications by Jeff Hemminger from Object Partners on Vimeo.

Headless Javascript Unit Testing

I gave a presentation at work a couple of months ago covering JavaScript scoping considerations for a team of Java developers new to JavaScript. During the presentation, I used Rhino to run the examples. I have seen other projects that use Rhino for unit testing, but they seemed a little heavy weight for what I [...]

EasyMock IllegalStateException

I had a case this morning reworking a test after I’d modified a method signature, from: expect(mockRestProxy.getProxiedResource((String) anyObject())) .andReturn(“{policies}”); to expect(mockRestProxy.getProxiedResource((String) anyObject(), userIdentity)) .andReturn(“{policies}”); I received a stacktrace like this: java.lang.IllegalStateException: 2 matchers expected, 1 recorded. at org.easymock.internal.ExpectedInvocation.createMissingMatchers(ExpectedInvocation.java:41) at org.easymock.internal.ExpectedInvocation.<init>(ExpectedInvocation.java:33) at org.easymock.internal.ExpectedInvocation.<init>(ExpectedInvocation.java:26) at org.easymock.internal.RecordState.invoke(RecordState.java:64) at org.easymock.internal.MockInvocationHandler.invoke(MockInvocationHandler.java:24) at org.easymock.internal.ObjectMethodsFilter.invoke(ObjectMethodsFilter.java:45) at $Proxy0.getProxiedResource(Unknown Source) … To quote the [...]

Automated Unit Testing with DBUnit, Hsqldb in Spring & Hibernate projects

If your project has a lot of unit testing in Java and you use continuous builds (CruiseControl) to automate, it is often difficult to automate tests which connect to Database (Dao and integration tests). For most companies, having a dedicated Oracle schema for testing is expensive. Also, sometimes, developers need their own instances of schemas [...]

Limitations of AbstractTransactionalDataSourceSpringContextTests

This has apparently gotten a fair amount of attention over the last year or so, but I never encountered it until today- call me lucky. Here’s the situation, you’re using Spring MVC and you have a controller that extends another controller (A->B). Your unit test extends AbstractTransactionalDataSourceSpringContextTests, and makes use of autowiring. You write your [...]

Java Integration Testing with HSQLDB

I’m a fairly recent convert to unit testing, but still when I’m under pressure to get something finished, I skip right to integration tests. Despite the guilt I still use the junit framework for these integration tests… I know the unit testing purists will be rolling their eyes at this point, but this is something [...]