Scorned Rider
Scorned Rider

Reputation: 11

Junit Tests failing when running it through jenkins

I am getting the following error when I am running my Tests in Jenkins. They run fine in Eclipse.

junit.framework.AssertionFailedError: No tests found in SCSystemTestCase

SCSystemTestCase is a class which extends TestCase and is used by other Junit Tests to run tests. Snippet of SCSystemTestCase is as shown below

import java.io.File;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Vector;

import junit.framework.TestCase;
import junit.framework.AssertionFailedError;

import org.jtestcase.JTestCase;
import org.jtestcase.JTestCaseException;
import org.jtestcase.TestCaseInstance;
import org.junit.Test;


public class SCSystemTestCase extends TestCase
{

  protected HashMap<String, Vector<String>> params = null;
  protected String testCaseFName = "";
  protected String testGoal = "";
  protected String testCaseFFolder = "";
  protected String testCaseClass = "";
  protected String testCaseLocationprefix = "SC";
  protected String testCaseLocation = "";
  protected String jerseyEndpoint = "";
  protected String requestMethod = "";
  protected String requestPath = "";
  protected String responseData = "";
  protected String description = "";
  protected String dataDir = "";
  protected String testCaseName;
  protected String testCaseMethod;
  protected TestCaseInstance testCaseCur;  
  protected Vector<?> testCases = null;

  private JTestCase thisJtestCase; 

  public SCSystemTestCase(String s, String t)
  {
    super(s);
    testCaseName = s;
    testCaseMethod = t;

  }

  public SCSystemTestCase(String s)
  {    
    super(s);
  }

  public SCSystemTestCase()
  {
  }

  public void setup() throws Exception
  {
    try
    {
      testCaseLocation = testCaseFFolder + File.separator + testCaseFName;
      setThisJtestCase(new JTestCase(testCaseLocation, testCaseClass));
    }
    catch (JTestCaseException jte)
    {
      System.out.println(jte.getMessage());
    }
  }


  @Override
  protected void runTest() throws Throwable
  {
    setup();
    triggerTest();
  }

  @Test
  protected void triggerTest() throws Exception
  {
    StringBuffer errorBucket = new StringBuffer();
   Hashtable<?,?> globalParams = null;
   // Hashtable<String, String> globalParams = null;
    try
    {
      testCases = getThisJtestCase().getTestCasesInstancesInMethod(testCaseMethod);
      globalParams = getThisJtestCase().getGlobalParams();
      jerseyEndpoint =  (String) globalParams.get("jerseyEndpoint");
      requestMethod =   (String) globalParams.get("requestMethod");
      requestPath =     (String) globalParams.get("requestPath");
      dataDir =         (String) globalParams.get("dataDir");

      for (int i = 0; i < testCases.size(); i++)
      {

        testCaseCur = (TestCaseInstance) testCases.elementAt(i);
        if (testCaseCur.getTestCaseName().equals(testCaseName))
        {
          System.out.println("Starting test: " + testCaseCur.getTestCaseName());
          System.out.println("======================================================");
          params = testCaseCur.getTestCaseParams();
          testGoal = (String) ((params.get("testGoal") != null) ? params.get("testGoal") : " Not Specified ");
          System.out.println("TEST-GOAL: " + testGoal);
          boolean isNegative = (Boolean) ((params.get("isNegative") != null) ? params.get("isNegative") : false);

          try
          {
            XMLDataParser test = new XMLDataParser();
            test.testExecuteTestSteps(params);
          }
          catch (Throwable t)
          {
            t.printStackTrace();
            if (!isNegative)
            {
              System.out.println("It is NOT a negative test, why did it throw an Exception!");
              errorBucket.append("\n-----" + testCaseMethod + "." + testCaseCur.getTestCaseName() + "-----");
              errorBucket.append("\nIt is NOT a negative test, why did it throw an Exception!\n");
            }
            else
            {
              System.out.println("It is a negative test!");
            }
          }
          finally
          {
            System.out.println("--Ending test: " + testCaseCur.getTestCaseName() + "--");
          }
        }
      }

    }
    catch (Throwable t)
    {
      t.printStackTrace();
    }
    finally
    {
      System.out.println("======================================================");
    }

    if (errorBucket.length() > 0)
    {
      throw new AssertionFailedError(errorBucket.toString());
    }

  }

//  protected abstract void testExecuteTestSteps() throws Exception, Throwable;


  public JTestCase getThisJtestCase()
  {
    return thisJtestCase;
  }

  public void setThisJtestCase(JTestCase thisJtestCase)
  {
    this.thisJtestCase = thisJtestCase;
  }

Could you please help me resolve this? This class does not have its own TestSuite() or tests. build.gradle file and eclipse both are using Junit4.10. There are no @Test annotations in any of the tests which extend this.Do I need to change the junit version in the gradle file to junit 3? If yes, which version should I use?

Upvotes: 1

Views: 2991

Answers (1)

Stefan Birkner
Stefan Birkner

Reputation: 24510

You mix JUnit 3 and JUnit 4. I think that Eclipse chooses to run your test as JUnit 4 test and Gradle decides that it is a JUnit 3 test. This is the reason for the different behaviour.

If you want to use JUnit 3, than you have to change the SCSystemTestCase

  • Rename the method setup as setUp
  • Delete the runTest method (it must not be overridden according to its Javadoc)
  • Remove the @Test annotation
  • Rename the method triggerTest as testSomething (it's important that the methods name starts with test).
  • In general a JUnit 3 test must not import any class of the org.junit package or one of its sub-packages.

But I strongly recommend using JUnit 4. For this you have to change the SCSystemTestCase

  • Remove the extends TestCase part of your class declaration
  • Remove the super calls in your constructors
  • Delete the runTest method.
  • Every test class (a class that inherits from SCSystemTestCase) must have a default constructor (a constructor without arguments)
  • In general a JUnit 4 test must not import any class of the junit.framework package or one of its sub-packages.

Upvotes: 2

Related Questions