Skip to content

Example illustrating how annotation scopes work

Jean Bisutti edited this page Sep 13, 2020 · 1 revision

To configure global annotations, i.e., annotations applying on each test, create a class implementing SpecifiableGlobalAnnotations and locate it in org.quickperf package:

package org.quickperf;

import org.quickperf.config.SpecifiableGlobalAnnotations;
import org.quickperf.sql.annotation.SqlAnnotationBuilder;

import java.lang.annotation.Annotation;
import java.util.Collection;
import java.util.Collections;

/*The configuration class has to be in org.quickperf package*/
public class QuickPerfConfiguration implements SpecifiableGlobalAnnotations {

    public Collection<Annotation> specifyAnnotationsAppliedOnEachTest() {

        // CoreAnnotationBuilder, SqlAnnotationBuilder and JvmAnnotationBuilder help to build and configure global annotations
        Annotation expectedSelectNumber = SqlAnnotationBuilder.expectSelectNumber(3);

        return Collections.singletonList(expectedSelectNumber);

    }

}

import org.junit.Test;

public class AClassWithGlobalScopeAnnotationAppliedTest {

     //@ExpectSelectNumber(3) annotation is applied
     @Test
     public void a_test_method() {
         //...
     }

}
package org.mycompany;

import org.junit.Test;
import org.quickperf.sql.annotation.MaxSqlSelect;

@ExpectSelectNumber(2) // CLASS SCOPE
                      // This annotation overrides the annotation
                      // defined in QuickPerfConfiguration class (GLOBAL SCOPE)
public class AClassWithAnnotationsTest {

    // @ExpectSelectNumber(2) annotation placed on class is applied
    @Test
    public void a_test_method() {
        //...
    }

    @ExpectSelectNumber(1) // METHOD SCOPE
                          // This annotation overrides the annotation placed
                          // on class
    @Test
    public void a_test_method_with_quick_perf_annotation() {
        //...
    }

}

Annotations

πŸ‘‰ Β Core

πŸ‘‰ Β JVM

πŸ‘‰ Β SQL

πŸ‘‰ Β Scopes

πŸ‘‰ Β Create an annotation

Supported frameworks

πŸ‘‰ Β JUnit 4

πŸ‘‰ Β JUnit 5

πŸ‘‰ Β TestNG

πŸ‘‰ Β Spring

How to

πŸ‘‰ Β Detect and fix N+1 SELECT

Project examples

πŸ‘‰ Β Maven performance

πŸ‘‰ Β Spring Boot - JUnit 4

πŸ‘‰ Β Spring Boot - JUnit 5

πŸ‘‰ Β Micronaut Data - JUnit 5

πŸ‘‰ Β Micronaut - Spring - JUnit 5

πŸ‘‰ Β Quarkus - JUnit 5

Miscellaneous

πŸ‘‰ Β FAQ

πŸ‘‰ Β QuickPerf code

Clone this wiki locally