-
Notifications
You must be signed in to change notification settings - Fork 4
Example illustrating how annotation scopes work
Jean Bisutti edited this page Jul 29, 2019
·
7 revisions
A class implementing SpecifiableGlobalAnnotations provides global annotations, that is to say annotations applying on each test.
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() {
/* To build an instance of ExpectSelectNumber annotation without SqlAnnotationBuilder
ExpectSelectNumber expectedSelectNumber = new ExpectSelectNumber() {
@Override
public Class<? extends Annotation> annotationType() {
return ExpectSelectNumber.class;
}
@Override
public int value() {
return 3;
}
};
*/
Annotation expectedSelectNumber = SqlAnnotationBuilder.expectSelectNumber(3);
return Collections.singletonList(expectedSelectNumber);
}
}
The class implementing SpecifiableGlobalAnnotations has to be in org.quickperf package.
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() {
//...
}
}
π Β Core
π Β JVM
π Β SQL
π Β Scopes
π Β Create an annotation
π Β JUnit 4
π Β JUnit 5
π Β TestNG
π Β Spring
π Β Detect and fix N+1 SELECT
π Β Maven performance
π Β Spring Boot - JUnit 4
π Β Spring Boot - JUnit 5
π Β Micronaut Data - JUnit 5
π Β Micronaut - Spring - JUnit 5
π Β Quarkus - JUnit 5
π Β FAQ
π Β QuickPerf code