Skip to content

JUnit 4

Jean Bisutti edited this page Jun 21, 2020 · 26 revisions

⚠️ If you use Spring, please go here.

🚩 Table of contents

QuickPerfJUnitRunner

SQL annotations

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.0.0-RC<7version>
  <scope>test</scope>
</dependency>

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

⚠️ If you use Gradle, please read this.

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.0.0-RC7</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() {
         // ... 
        }

}

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