Skip to main content

Executing JUnit Runners Outside Of JUnit

I received a comment on one of my previous posts asking how to run a test class using SpringJUnit4ClassRunner from normal application code (i.e. within a main method). I'll admit, I didn't know how to do this, but my interest was piqued.

I ran off to an existing test class I had laying around. Within a few minutes, I had a working prototype:

public static void main(final String[] args) throws Exception {
 final Class testClass = Class.forName(args[0]);
 final Runner r = new SpringJUnit4ClassRunner(testClass);
 final RunNotifier n = new RunNotifier();
 final RunListener l = new RunListener() {
  @Override
  public void testRunStarted(Description d) {
   System.out.println("Tests are starting");
  }
  @Override
  public void testIgnored(Description d) {
   System.out.println("Test is ignored");
  }
  @Override
  public void testStarted(Description d) {
   System.out.println("Starting a test");
  }
  @Override
  public void testFailure(Failure f) {
   System.out.println("WOAH! Test failed");
  }
  @Override
  public void testAssumptionFailure(Failure f) {
   System.out.println("WOAH! Assertion failed");
  }
  @Override
  public void testFinished(Description d) {
   System.out.println("Test has finished");
  }
  @Override
  public void testRunFinished(Result r) {
   System.out.println("Tests have finished");
  }
 }
 n.addListener(l);
 r.run(n);
}

Comments

Popular posts from this blog

Using MonoDevelop to Create an ASP.NET Web Service

NOTE : instructions below are for MonoDevelop 2.6 Beta 2 - built on 2011-04-06 03:37:58+0000 Getting Started Create a new ASP.NET Web Application in MonoDevelop: From the menu, select: File → New → Solution… Expand C# . Select ASP.NET → Web Application . Enter a name for the ASP.NET project that will be created in the solution in Name: . Change the root location for the solution in Location: , if desired. Change the name of the root solution in Solution Name: , if desired. The Results – I What you have after executing the new ASP.NET Web Application project wizard is a solution containing one ASP.NET Web Application project. In the default project view in MonoDevelop, you'll find the following items: Default.aspx – This is the default web form rendered and presented in the browser when http://<server>:<port>/ is accessed. Default.aspx.cs – This C# file contains the developer-created common code and event handlers whic...