Tuesday, April 22, 2014

Spring Plain Java Test

Invented the following as a way to run tests from main() etc without JUnit, which always annoys me with its lack of control (e.g. can't select which tests to run from multiple packages, unless you want to run all or use Suite/SuiteClasses (or, please tell me how!); can't annotate as Ignore to maven, but tell JUnit to run them manually (or tell me how!); can't run tests found with Find References, etc...or maybe one can do that, I'll have to check...), at least when I use it in Eclipse which I use. Took me a while, so I thought it might save some time for somebody, or even myself if I should find myself working in Spring again in the future.

Sorry if the formatting is whack -- I am using blogger.




package jn.spring.test.support;

import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestContextManager;

/**
 * If no better solution can be found (I've searched for a couple of hours) to
 * inhibit JUnit tests that are not good as automatic tests, this allows Spring
 * context etc to run from any Java code, e.g. main(). 

 * 

 * 
 * NOTE: maybe configure Maven Surefire excludes could be a way. 

 * 


 * 
 * @author jonasnordin4
 * 
 */
public abstract class PlainJavaSpringTestClassBase {
 
 PlainJavaSpringTestClassBase() {
 }
 
 public void _run() {
  Object tt = this;
  Class testClass = tt.getClass();
  runTestClass(testClass, this);
 }
 
 /**
  * Override to implement a test. 
  */
 public abstract void test();
 
 public static void runTestClass(Class testClass, PlainJavaSpringTestClassBase tt) {
  System.out.println("running...");
  TestContextManager tcm = new TestContextManager(testClass);
  System.out.println("tcm: " + tcm);
  try {
   System.out.println("tcm.prepareTestInstance(tt);...");
   tcm.prepareTestInstance(tt);
   System.out.println("did: tcm.prepareTestInstance(tt);");
   tt.test();
  } catch (Exception e) {
   // TODO Auto-generated catch block
   // e.printStackTrace();
   throw new RuntimeException(e);
  } finally {
     try {
       // this seems to work to clean up. E.g. Spring background threads that keep us from exiting.
       tcm.afterTestClass();
     } catch (Exception e) {

        e.printStackTrace();

     }
     
   }
   
 }
 
 /**
  * No persistence, so set-up runs quickly. 
  * 
  * @author jonasnordin4
  *
  */
 @ContextConfiguration(locations = {
   "classpath*:com/projectname/config/spring-common*.xml",
   // "classpath*:com/projectname/config/spring-cache.xml",
   // "classpath*:com/projectname/config/spring-persistence.xml",
   // "classpath*:com/projectname/config/spring-vfs.xml",
   "classpath*:com/projectname/config/spring-*-test.xml" 
   })
 public abstract static class QuickPlainJavaSpringTest extends
   PlainJavaSpringTestClassBase {
  
 }
 
 @ContextConfiguration(locations = {
   "classpath*:com/projectname/config/spring-common*.xml",
   // "classpath*:com/projectname/config/spring-cache.xml",
    "classpath*:com/projectname/config/spring-persistence.xml",
   // "classpath*:com/projectname/config/spring-vfs.xml",
   "classpath*:com/projectname/config/spring-*-test.xml" 
   })
 public abstract static class PersistencePlainJavaSpringTestClassBase extends
   PlainJavaSpringTestClassBase {
  
  
 }
 
 /**
  * See if this technique works. It seems to work well. 
  * 
  * @author jonasnordin4
  *
  */
 public static class PlainJavaMetaTest {
  
  /**
   * See if '@ContextConfiguration' is inherited (it is). 

   * 

   * 
   * @author jonasnordin4
   *
   */
  public static class TheTestClass extends QuickPlainJavaSpringTest {
   @Override
   public void test() {
    
   }
  }
  
  public void run() {
   final TheTestClass it = new TheTestClass();
   it._run();
  }
  
  public static void main(String...args) {
   new PlainJavaMetaTest().run();
   PersistencePlainJavaSpringTestClassBase p = new PersistencePlainJavaSpringTestClassBase() {
    public void test() {
     
    };
   };
   p._run();
   // System.exit(0); // needed, or some Spring thread lives on. No, see tcm.afterTestClass(); above.
  }
  
 }
}

No comments:

Post a Comment