2

It' possible to use Junitperf with junit4? I've a simplet Junit4 test class with several tests and I want to do a TimedTest on single test of that class. How can I do that?

To be more clear my Junit4 class is something like:

public class TestCitta {

    @Test
    public void test1 {}

        @Test
    public void test2 {}
}

with junit3 i shold write something like:

public class TestCittaPerformance {

    public static final long toleranceInMillis = 100;

    public static Test suite() {

        long maxElapsedTimeInMillis = 1000 + toleranceInMillis;

        Test testCase = new TestCitta("test2");

        Test timedTest = new TimedTest(testCase, maxElapsedTimeInMillis);

        return timedTest;
    }

    public static void main(String args[]) {
        junit.textui.TestRunner.run(suite());
    }
}

with Junit4?

Konrad Krakowiak
  • 12,285
  • 11
  • 58
  • 45

3 Answers3

8

I had the same problem but was not lucky trying to make it run in different build environments. So I used the @Rule feature available since JUnit 4 to inject performance test invocation and requirements checking using annotations. It turned out to become a small library which replaced JUnitPerf in this project and I published it under the name . If you are interested in this approach, you can find it at https://github.com/lucaspouzac/contiperf.

Ruslan López
  • 4,433
  • 2
  • 26
  • 37
Volker
  • 81
  • 1
  • 1
5

If you have junit4 already, why dont you use contiperf. It will do what you are looking for and with annotations.

POM goes like this.

 <dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.10</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>org.databene</groupId>
  <artifactId>contiperf</artifactId>
  <version>2.0.0</version>
  <scope>test</scope>
</dependency>

The test class goes like this

public class PersonDAOTest {
@Rule
public ContiPerfRule i = new ContiPerfRule();

And the actual test goes like this

@Test
@PerfTest(invocations = 1, threads = 1)
@Required(max = 1200, average = 250)
public void test() {
davyjonestech
  • 51
  • 1
  • 2
1

Or you can use the annotation: @Test(timeout=1000)

Mr.Wizard
  • 24,179
  • 5
  • 44
  • 125
sandeep
  • 11
  • 1