JUnit Suites and Vertx-Unit: initializationError ... Method xyz should have no parameters

292 views
Skip to first unread message

Sach Shah

unread,
Aug 14, 2017, 3:35:58 PM8/14/17
to vert.x
Hi All,

I am trying to integrate Vertx-Unit with JUnit in my project.  I was able to get the test suite running when the class with the Tests (i.e. functions with @Test annotations) was itself annotated with @RunWith(VertxUnitRunner.class).  Now I am trying to setup a few JUnit suites so that we can have critical, non-critical, etc. tests run at the appropriate time.

I did this as follows:

My Suite class is:
@SuiteClasses(value = { MonthlyTest.class })
@RunWith(VertxUnitRunner.class)
public class TestSuite {
}

My class with the tests looks like:
public class MonthlyTest {
private static StrategyImpl icos;

@ClassRule
public static RunTestOnContext rule = new RunTestOnContext();

@BeforeClass
public void before(TestContext tc) {
icos = new StrategyImpl();
}

@After
public void after(TestContext tc) {
}

@Test
public void MonthlyNonLeapYear(TestContext tc) {
LocalDate ld = LocalDate.of(2017, Month.JANUARY, 31);
LocalDate expected = LocalDate.of(2017, Month.FEBRUARY, 28);
tc.assertEquals(icos.calculateNextPaymentDate(ld, PaymentFrequency.MONTHLY), expected);
}
.... (continues on)

When I run this using 'mvn clean test', I get the following errors:
Tests in error:
  initializationError(MonthlyTest): Method before() should be static
  initializationError(MonthlyTest): Method before should have no parameters
  initializationError(MonthlyTest): Method after should have no parameters
  initializationError(MonthlyTest): Method MonthlyFirstOfMonth1 should have no parameters
  initializationError(MonthlyTest): Method MonthlyFirstOfMonth2 should have no parameters
  initializationError(MonthlyTest): Method MonthlyFirstOfMonth3 should have no parameters
  initializationError(MonthlyTest): Method MonthlyLeapYear should have no parameters
  initializationError(MonthlyTest): Method MonthlyNonLeapYear should have no parameters
  initializationError(MonthlyTest): Method MonthlyMidMonth1 should have no parameters
  initializationError(MonthlyTest): Method MonthlyMidMonth2 should have no parameters
  initializationError(MonthlyTest): Method MonthlyMidMonth3 should have no parameters

Has anyone been able to successfully combine JUnit tests into suites in the context of Vert.x?  If so, can you please point me to some resources on how to do it?

Thank you,
Sach.

Blake

unread,
Aug 16, 2017, 1:05:46 PM8/16/17
to vert.x
First error is self explanatory: make it static (all BeforeClass, AfterClass, and ClassRule params/methods must be static).

Secondly, you should either extend TestSuite in MonthlyTest, or you should use @RunWith(VertxUnitRunner.class) on the MonthlyTest.

I'm also not sure 

Blake

unread,
Aug 16, 2017, 1:06:49 PM8/16/17
to vert.x
Disregard the "I'm also not sure". Nothing more to post.

Blake

unread,
Aug 16, 2017, 1:37:10 PM8/16/17
to vert.x
Hi Sach,

It's pretty clear I did a bad job reading your message and that you already had the VertxUnitRunner working and my comment wasn't very productive. I think you're going to have to move the @RunWith(VertxUnitRunner.class) into each class that the suite is calling (that needs a TestContext) or you're going to have to write a wrapper that injects it into each Suite that is run. Does having @RunWith(VertxUnitRunner.class) at the top of each class cause you any problems with the suite?



On Monday, August 14, 2017 at 2:35:58 PM UTC-5, Sach Shah wrote:

Alexander Lehmann

unread,
Aug 17, 2017, 9:58:48 AM8/17/17
to vert.x
From the error messages I guess that the test class is run by a normal junit runner without the vertx RunWith class, but I have no idea why. Do you have the @RunWith annotation in all test classes or only in the suite class?

Sach Shah

unread,
Aug 19, 2017, 9:44:02 AM8/19/17
to vert.x
I agree, it is using the normal junit runner despite the Suite's @RunWith annotation.  When moving to a suite-based model, I removed @RunWith from each test class and only kept it in the Suite class.

Sach Shah

unread,
Aug 19, 2017, 9:45:19 AM8/19/17
to vert.x
Thank you for the suggestion.  I tried adding the @RunWith to both the suite and test classes.  When I do so, the individual test classes all run (so can't "select the tests to run" using the suite) but the suite still fails.

Sach Shah

unread,
Aug 19, 2017, 11:29:37 AM8/19/17
to vert.x
Hi All,

I wanted to post a general reply to how we decided to solve this problem.  We are building the application in Java and are using Maven as our build tool.  We decided to abandon the JUnit-specific Suite approach and have gone with the Maven Surefire plugin's approach of using the @Category annotation on test classes and then configuring the POM file to include / exclude tests we want to run.  Here is an example.

The test class, with a new @Category annotation:
@Category({ CriticalTest.class })
@RunWith(VertxUnitRunner.class)
public class MonthlyTest {
private StrategyImpl icos;
private Vertx vertx;

@Before
public void before(TestContext tc) {
vertx = Vertx.vertx();
icos = new StrategyImpl().setVertx(vertx);
}

@After
public void after(TestContext tc) {
vertx.close(tc.asyncAssertSuccess());
}

@Test
public void monthlyNonLeapYear(TestContext tc) {
LocalDate ld = LocalDate.of(2017, Month.JANUARY, 31);
LocalDate expected = LocalDate.of(2017, Month.FEBRUARY, 28);
tc.assertEquals(icos.calculateNextPaymentDate(ld, PaymentFrequency.MONTHLY), expected);
}
... (continues on)

And the POM file, in the build/plugins section.
<!-- Change groups to pick suites. -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>common-junit48</artifactId>
<version>${maven-surefire-plugin.version}</version>
</dependency>
</dependencies>
<configuration>
<parallel>classes</parallel>
<useUnlimitedThreads>true</useUnlimitedThreads>
<trimStackTrace>false</trimStackTrace>
<groups>com.xyz.test.CriticalTest,com.xyz.test.NonCriticalTest</groups>
</configuration>
</plugin>

In this instance, CriticalTest and NonCriticalTest are simply empty interfaces.  The test classes do not need to extend these interfaces, just use the @Category annotation with them.

More information at http://maven.apache.org/surefire/maven-surefire-plugin/examples/junit.html, section Using JUnit Categories.  

Thank you to everyone who replied and helped solve this issue.

Regards,
Sach.


On Monday, August 14, 2017 at 2:35:58 PM UTC-5, Sach Shah wrote:
Reply all
Reply to author
Forward
Message has been deleted
0 new messages