Skip to content

JUnit 4

Jean Bisutti edited this page Sep 29, 2021 · 26 revisions

⚠️ If you use Spring, please go here.

🚩 Table of contents

QuickPerfJUnitRunner

SQL annotations

Project examples

QuickPerfJUnitRunner

QuickPerfJUnitRunner adds QuickPerf features to default JUnit runner.

To use it, you have to this dependency

<dependency>
  <groupId>org.quickperf</groupId>
  <artifactId>quick-perf-junit4</artifactId>
  <version>1.1.0<version>
  <scope>test</scope>
</dependency>

With this dependency, you have access to core and JVM annotations, except the JVM profiling annotations,.

Java code example with QuickPerfSpringRunner

	import org.junit.runner.RunWith;
	import quickperf.junit4.QuickPerfJUnitRunner;

	@RunWith(QuickPerfJUnitRunner.class)
	public class AccountTest {

	}

SQL annotations

In addition to the dependency mentioned in the QuickPerfJUnitRunner part, you have to add the dependency below to can use SQL annotations.

<dependency>
  <groupId>org.quickperf</groupId>
  <artifactId>quick-perf-sql-annotations</artifactId>
  <version>1.1.0</version>
  <scope>test</scope>
</dependency>

You also need to build a datasource proxy with QuickPerfSqlDataSourceBuilder and use this proxy in your test.

Look at the example below.

Java code example using QuickPerfSqlDataSourceBuilder and a SQL annotation

	import org.junit.runner.RunWith;
	import quickperf.junit4.QuickPerfJUnitRunner;

        import net.ttddyy.dsproxy.support.ProxyDataSource;
        import org.hibernate.jpa.HibernatePersistenceProvider;
        import org.junit.Before;
        import org.quickperf.sql.entities.Book;

        import javax.persistence.EntityManagerFactory;
        import javax.persistence.spi.PersistenceProvider;
        import javax.persistence.spi.PersistenceUnitInfo;
        import javax.sql.DataSource;
        import java.util.HashMap;
        import java.util.Properties;

        import org.quickperf.sql.config.QuickPerfSqlDataSourceBuilder;

        import org.quickperf.sql.annotation.SelectNumber;

	@RunWith(QuickPerfJUnitRunner.class)
	public class bookTest {

          private EntityManagerFactory emf;

          @Before
          public void before() {
             PersistenceProvider persistenceProvider = new HibernatePersistenceProvider();
             PersistenceUnitInfo info = buildPersistenceUnitInfo();
             emf = persistenceProvider.createContainerEntityManagerFactory(info, new HashMap<>());
          }

         private PersistenceUnitInfo buildPersistenceUnitInfo() {
             DataSource baseDataSource = TestDataSourceBuilder.aDataSource().build();
             ProxyDataSource proxyDataSource = QuickPerfSqlDataSourceBuilder.aDataSourceBuilder().buildProxy(baseDataSource);
             Properties hibernateProperties = HibernateConfigBuilder.anHibernateConfig().build();
             return PersistenceUnitInfoBuilder.aPersistenceUnitInfo()
                                              .build(proxyDataSource
                                                   , hibernateProperties
                                                   , Book.class);
         }

         @ExpectMaxSelect(1)
         @Test
         public void should_retrieve_books_from_database() {
         // ... 
        }

}

Project examples

JVM & JUnit 4

Hibernate & JUnit 4

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