3

I was looking for a verification test for custom implemented collections, and stumbled upon this: http://www.gamlor.info/wordpress/2012/09/google-guava-collection-test-suite/

I haven't used junit before (so I'm a total noob with junit). I added junit4 to my test project and... got stuck on how to actually run the test suite created by the Google Guava Collection Test Suite. I run annotated test from my test class just fine, but not the test suite from guava.

The junit docs say that suites are created by annotating a suite class with the Suite annotation, listing the cases they should include, but obviously I can't list a dynamically generated class that way. I would be happy to just create a simple test and run the entire suite as a single test, only... how do I get junit to run the suite instance?

Durandal
  • 19,919
  • 4
  • 36
  • 70
  • 1
    Have you looked at http://stackoverflow.com/questions/3257080/how-do-i-dynamically-create-a-test-suite-in-junit-4 ? I think you should be able to follow those patterns to wrap up a call to the factory method to accomplish what you want. – tuckermi Sep 06 '13 at 01:47
  • Specifically, [this answer](http://stackoverflow.com/a/11932146/733345). – Joe Sep 06 '13 at 16:19
  • 1
    @tuckermi You should post that as an answer (maybe with a summary/quote). – Paul Bellora Sep 07 '13 at 18:29
  • Sure. I will write up something now. – tuckermi Sep 07 '13 at 22:36

1 Answers1

2

If you have a factory method that returns a Test object (let's call it TestMaker.foo(), then you can write something like the following to take the Test and add it to your own TestSuite class:

import junit.framework.JUnit4TestAdapter;
import junit.framework.TestSuite;
import org.junit.runner.RunWith;
import org.junit.runners.AllTests;

@RunWith(AllTests.class)
public class TestRunner {
    public static TestSuite suite() {
        TestSuite ts = new TestSuite();

        ts.addTest(TestMaker.foo());

        return ts;
    }
}

Now, when you run the TestRunner tests, it will also run the Test returned by TestMaker.foo() along with any other tests that might be defined directly in TestRunner.

For related discussion (for example, how to refer to a Test Class rather than pass a Test object), check out: How do I Dynamically create a Test Suite in JUnit 4?

Community
  • 1
  • 1
tuckermi
  • 839
  • 6
  • 17