Posts by Willem Cheizoo

Nifty JUnit : Working with temporary files

Posted on by  
Willem Cheizoo

I get this question quite often and I struggled with it many times before: How do I work with temporary files in my JUnit testcases? I would like to explain two simple ways of working with temporary files in JUnit.

Create a temporary file with the Java File API and mark it directly as deleteOnExit(). The created file will be deleted when the JVM exits.

Continue reading →

Prevent 'No plugin found' in multi-module maven

Posted on by  
Willem Cheizoo

Defining a maven plugin on a submodule in a multi-module maven project can give us a 'No plugin found' error. Especially if we have a multi-module project and we want to apply a maven plugin in only one specific module this error occur pretty often. Let's say we have a multi-module root pom which looks like this.

 4.0.0

  com.jdriven.blog
  maven-plugin-multimodule
  0.1-SNAPSHOT
  pom

  module1 
    module2 

Continue reading →

Nifty JUnit : How to test for an exception

Posted on by  
Willem Cheizoo

Testing for exceptions in JUnit is something we have to deal with! We want to test if an exception occurs in a particular situation, or even if the exception contains a particular message. The question is: How to test for an exception in Junit? What we see very often is this:

import org.junit.Test;

public class JDrivenServiceTest {

    JDrivenService service = new JDrivenService();

    @Test(expected = ArticleNotFoundException.class)
    public void testPublishArticle_WithException() throws Exception {
        service.publishArticle(null);
    }
}

Continue reading →

Compare JAR files content; decompiling class files

Posted on by  
Willem Cheizoo

When I was recently working on a large restructuring and refactoring where I also replaced Ant by Maven, it was really necessary to compare the complete content of two different JAR files. It was required to know that the result of the restructuring and refactoring hadn't changed the artifacts, thus the JAR files. In the JAR files were different Class files present. When I compared the content of the two JAR files (with a binary compare) all the content was radically changed. This was partly because the compiler compiled the Class files at a different timestamp.

Since I wanted the best possible comparison between the two JAR files I needed to compare all Class files in the JAR by decompiling and comparing. This should give me a clearer and more honest picture of the differences. For this action I used Beyond Compare. By using an additional File Format (Java Class to Source) I was able to completely compare the decompiled Class files of the two JARS.

Continue reading →

Spring Tip: Check for existence of Bean during startup

Posted on by  
Willem Cheizoo

Sometimes we are working on an application where we have no control over specific Spring bean implementations. Nevertheless we want to check for the existence (or duplication) of these bean implementations during start-up of the application server. Since Spring version 3.0 it is possible to use Spring's ApplicationListener. This interface is based on the EventListener and uses the standard Observer design pattern. In 3 steps we can easily check for the existence of a specific Spring bean.

  1. We create an ApplicationListener<ContextRefreshedEvent> implementation. The method onApplicationEvent will be called when a refresh of the ApplicationContext occurs.

    package com.jdriven.blog.applicationlistener;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationListener;
    import org.springframework.context.event.ContextRefreshedEvent;
    
    public class BeanContextStartupListener
       implements ApplicationListener<ContextRefreshedEvent>
    {
       public void onApplicationEvent( ContextRefreshedEvent event )
       {
          ...
       }
    }
    
  2. We need to register the BeanContextStartupListener as a Spring Component. In this sample we just simply enable component scanning in the spring xml configuration as follows.

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    
        <context:component-scan base-package="com.jdriven.blog.applicationlistener" />
    </beans>
    

    We also add the @Component to the class definition:

    import org.springframework.stereotype.Component;
    
    @Component
    public class BeanContextStartupListener
       implements ApplicationListener<ContextRefreshedEvent>
    
  3. We implement the onApplicationEvent method of the BeanContextStartupListener and throw an Exception when no bean exists within the current ApplicationContext.

    public void onApplicationEvent( ContextRefreshedEvent event )
    {
       Map serviceImplementations = event.getApplicationContext().getBeansOfType( JDrivenService.class );
       if ( serviceImplementations.isEmpty() ) {
          throw new IllegalStateException("JDrivenService was not instantiated during startup.");
       }
    }
    

Continue reading →

shadow-left