Life as Code

refactoring my life

Archive for the ‘Unit testing’ Category

Get all the types contained in all the assemblies loaded, filtered by namespace

with one comment

This quick snippet will give you all the types declared in all the assemblies loaded, filtered by namespace.

List<Type> types = new List<Type>();
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();

foreach (Assembly asm in assemblies)
{
        IEnumerable<Type> asmTypes = from t in asm.GetTypes() 
              where t.IsClass 
                && (t.Namespace != null && t.Namespace.StartsWith(targetNamespace)) 
              select t;
        types.AddRange(asmTypes);
}

I actually got to this point because of the way NUnit wraps an assembly in another AppDomain.

The snippet need Linq so it’s for 3.5 or newer.

Written by Bogdan

October 29, 2009 at 9:05 AM

Posted in .NET, Development, Unit testing

Tagged with , ,

Transactional unit tests that support scopes using JUnit and Spring

with one comment

Most of the unit tests that you write don’t need support for transactions and scopes, but if you ever want to test your web frontend code properly you’ll hit into this one.

Writing transactional unit test with JUnit, Spring and Hibernate is easy, what you want to do is:

- Add a transaction management bean in your test context:

<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

- Make your test extend AbstractTransactionalJUnit4SpringContextTests
– Annotate you test class with @TransactionConfiguration and @Transactional

If your transaction demarcation is correct( surrounding your dao or service ) you should be done. This is the cleanest way that I found.

Everything until now is fine and dandy. The problem arises when one of the injected beans is bound to a scope. It can be any scope but for this example I’ll take the simplest scope – the ‘session’ scope.

In a non-transactional situation this should be pretty straight forward, just implement Scope and using a ConfigurableBeanFactory register it. The fact that we are extending <a href="http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/test/context/junit4/AbstractTransactionalJUnit4SpringContextTests.html"AbstractTransactionalJUnit4SpringContextTests and that our context is automatically created really kills the simple path.

You’ll get a org.springframework.beans.factory.BeanCreationException with a nested “java.lang.IllegalStateException: No Scope registered for scope ‘session'”.

But do not despair, there is simple solution available and it’s called CustomScopeConfigurer.

The fastest and cleanest way to use it is:
– Create a simple class that will be used as our scope manager. Let’s call it MockSessionScope.

code

- In your test context, create a CustomScopeConfigurer that will automatically register your scope on context creation:

<bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
<property name="scopes">
<map>
<entry key="session">
<bean class="com.gridpulse.xandria.translator.MockSessionScope"/>
</entry>
</map>
</property>
</bean>

Run your tests and you’re done!

Written by Bogdan

August 21, 2009 at 4:45 PM

Follow

Get every new post delivered to your Inbox.

Join 260 other followers