Skip to content
Permalink
Browse files

Improved handling of JUnit tests + some basic tests for the JUnit exe…

…c support
  • Loading branch information...
lukasj committed Mar 13, 2012
1 parent 9e3605c commit 2932bb81300c543503ffe382f39dd6d92016b8e2
@@ -2,6 +2,7 @@

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import org.junit.runner.RunWith;

/**
*
@@ -13,6 +14,11 @@ public JUnit4TestRecognizer() {
}

public boolean isTest(Class c) {
for (Annotation an: c.getAnnotations()) {
if (RunWith.class.isAssignableFrom(an.annotationType())) {
return true;
}
}
boolean haveTest = false;
for (Method m : c.getMethods()) {
for (Annotation a : m.getDeclaredAnnotations()) {
@@ -32,8 +32,8 @@ public static boolean isJUnitTest(Class c) {
if (!haveJUnit()) {
return false;
}
//only public not-abstract classes are interesting, so filter out the rest
if (Modifier.isAbstract(c.getModifiers()) || !Modifier.isPublic(c.getModifiers()) || c.isInterface() || c.isAnnotation() || c.isEnum()) {
//only public classes are interesting, so filter out the rest
if (!Modifier.isPublic(c.getModifiers()) || c.isInterface() || c.isAnnotation() || c.isEnum()) {
return false;
}
return (junit3 != null && junit3.isTest(c)) || (junit4 != null && junit4.isTest(c));
@@ -0,0 +1,81 @@
package test;

import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import test.junit4.JUnit4Child;
import test.junit4.JUnit4Sample2;
import test.junit4.JUnit4SampleSuite;

/**
*
* @author lukas
*/
public class JUnit4Test extends BaseTest {

@BeforeMethod(dependsOnGroups = {"initTest"})
public void initJUnitFlag() {
getTest().setJUnit(true);
}

@Test
public void testTests() {
addClass("test.junit4.JUnit4Sample2");
assert getTest().isJUnit();

run();
String[] passed = JUnit4Sample2.EXPECTED;
String[] failed = JUnit4Sample2.FAILED;
String[] skipped = JUnit4Sample2.SKIPPED;

verifyTests("Passed", passed, getPassedTests());
verifyTests("Failed", failed, getFailedTests());
verifyTests("Skipped", skipped, getSkippedTests());
}

@Test
public void testSuite() {
addClass("test.junit4.JUnit4SampleSuite");
assert getTest().isJUnit();

run();
String[] passed = JUnit4SampleSuite.EXPECTED;
String[] failed = JUnit4Sample2.FAILED;
String[] skipped = JUnit4SampleSuite.SKIPPED;

verifyTests("Passed", passed, getPassedTests());
verifyTests("Failed", failed, getFailedTests());
verifyTests("Skipped", skipped, getSkippedTests());
}

@Test
public void testSuiteInheritance() {
addClass("test.junit4.JUnit4Child");
assert getTest().isJUnit();

run();
String[] passed = JUnit4Child.EXPECTED;
String[] failed = {};
String[] skipped = {};

verifyTests("Passed", passed, getPassedTests());
verifyTests("Failed", failed, getFailedTests());
verifyTests("Skipped", skipped, getSkippedTests());
}

@Test
public void testTestInheritance() {
addClass("test.junit4.InheritedTest");
addClass("test.junit4.JUnit4Sample1");
assert getTest().isJUnit();
setDebug();

run();
String[] passed = {"t1", "t1"};
String[] failed = {};
String[] skipped = {};

verifyTests("Passed", passed, getPassedTests());
verifyTests("Failed", failed, getFailedTests());
verifyTests("Skipped", skipped, getSkippedTests());
}
}
@@ -121,5 +121,17 @@ public static void ppp(String s) {
System.out.println("[JUnitTest1] " + s);
}

@Test
public void testAbstract() {
addClass("test.sample.JUnitSample4");
run();
String[] passed = {
"testXY", "testXY", "testXY"
};
String[] failed = {
};

verifyTests("Passed", passed, getPassedTests());
verifyTests("Failed", failed, getFailedTests());
}
}
@@ -0,0 +1,9 @@
package test.junit4;

/**
*
* @author lukas
*/
public class InheritedTest extends JUnit4Sample1 {

}
@@ -0,0 +1,12 @@
package test.junit4;

import org.junit.runners.Suite;

/**
*
* @author lukas
*/
@Suite.SuiteClasses({JUnit4Sample1.class})
public class JUnit4Child extends JUnit4SampleSuite {
public static final String[] EXPECTED = {"t1"};
}
@@ -0,0 +1,15 @@
package test.junit4;

import org.junit.Test;

/**
*
* @author lukas
*/
public class JUnit4Sample1 {

@Test
public void t1() {

}
}
@@ -0,0 +1,34 @@
package test.junit4;

import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;

/**
*
* @author lukas
*/
public class JUnit4Sample2 {

public static final String[] EXPECTED = {"t2", "t4"};
public static final String[] SKIPPED = {"t3"};
public static final String[] FAILED = {"tf"};

@Test
public void t2() {
}

@Test
@Ignore
public void t3() {
}

@Test
public void t4() {
}

@Test
public void tf() {
Assert.fail("a test");
}
}
@@ -0,0 +1,20 @@
package test.junit4;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;

/**
*
* @author lukas
*/
@RunWith(Suite.class)
@Suite.SuiteClasses({
JUnit4Sample1.class,
JUnit4Sample2.class
})
public class JUnit4SampleSuite {

public static final String[] EXPECTED = {"t1", "t2", "t4"};
public static final String[] SKIPPED = {"t3"};
public static final String[] FAILED = {"tf"};
}
@@ -0,0 +1,38 @@
package test.sample;

import junit.framework.Assert;
import junit.framework.TestCase;
import junit.framework.TestSuite;

/**
*
* @author lukas
*/
public abstract class JUnitSample4 extends TestCase {

private int i = 0;

public JUnitSample4(String name, int i) {
super(name);
this.i = i;
}

public void testXY() {
Assert.assertEquals(1, 1);
}

public static TestSuite suite() {
TestSuite ts = new TestSuite("Sample Suite");
for (int i = 0; i < 3; i++) {
ts.addTest(new T(i));
}
return ts;
}

private static class T extends JUnitSample4 {

public T(int i) {
super("testXY", i);
}
}
}
@@ -548,6 +548,7 @@
<classes>
<class name="test.JUnitTest1" />
<class name="test.CommandLineTest"/>
<class name="test.JUnit4Test" />
</classes>
</test>

0 comments on commit 2932bb8

Please sign in to comment.
You can’t perform that action at this time.