-
Notifications
You must be signed in to change notification settings - Fork 4
Spring
Add one of the following dependencies below.
Spring 5
<dependency>
<groupId>org.quickperf</groupId>
<artifactId>quick-perf-junit4-spring5</artifactId>
<version>1.1.0</version>
<scope>test</scope>
</dependency>
Spring 4
<dependency>
<groupId>org.quickperf</groupId>
<artifactId>quick-perf-junit4-spring4</artifactId>
<version>1.1.0</version>
<scope>test</scope>
</dependency>
Spring 3
<dependency>
<groupId>org.quickperf</groupId>
<artifactId>quick-perf-junit4-spring3</artifactId>
<version>1.1.0</version>
<scope>test</scope>
</dependency>
In the test class, use @RunWith(SpringRunner.class)
instead of@RunWith(QuickPerfSpringRunner.class)
.
QuickPerfSpringRunner adds QuickPerf features to SpringRunner (also called SpringJUnit4ClassRunner).
Java code example with QuickPerfSpringRunner
import org.junit.runner.RunWith;
import quickperf.spring.QuickPerfSpringRunner;
@RunWith(QuickPerfSpringRunner.class)
public class AccountTest {
}
First, add QuickPerf dependencies for JUnit 5.
Second, transform the test into a QuickPerf test with JUnit 5.
After applying the JUnit 4 or the JUnit 5 configuration below, you can use the JVM annotations, except the JVM profiling annotations. To use the last ones, please read this.
QuickPerf executes annotations after the loading of the SpringContext. For example, if you use ProfileJvm annotation, QuickPerf will not profile the Spring context loading.
QuickPerf needs to intercept the SQL statements sent to the database.
The sections hereafter describe several ways to allow QuickPerf to intercept the SQL statements with Spring.
With Spring Boot 1, add the following dependency
<dependency>
<groupId>org.quickperf</groupId>
<artifactId>quick-perf-springboot1-sql-starter</artifactId>
<version>1.1.0</version>
<scope>test</scope>
</dependency>
With Spring Boot 2, use this dependency
<dependency>
<groupId>org.quickperf</groupId>
<artifactId>quick-perf-springboot2-sql-starter</artifactId>
<version>1.1.0</version>
<scope>test</scope>
</dependency>
If something is missing for some tests using SQL annotations, the console will display configurations to add:
Do you use @DataJpaTest? This annotation disables Spring auto-configuration.
So, QuickPerf Spring auto-configuration is disabled.
To allow QuickPerf to intercept the SQL queries, you have two possibilities:
1) Import QuickPerfSqlConfig (recommended):
import org.quickperf.spring.sql.QuickPerfSqlConfig;
...
@Import(QuickPerfSqlConfig.class)
public class TestClass {
2) Force to enable Spring auto-configuration by adding
@OverrideAutoConfiguration(enabled = true) on the test class
With Spring 4 add
<dependency>
<groupId>org.quickperf</groupId>
<artifactId>quick-perf-sql-spring4</artifactId>
<version>1.1.0</version>
<scope>test</scope>
</dependency>
With Spring 5 add
<dependency>
<groupId>org.quickperf</groupId>
<artifactId>quick-perf-sql-spring5</artifactId>
<version>1.1.0</version>
<scope>test</scope>
</dependency>
Import org.quickperf.spring.sql.QuickPerfSqlConfig
configuration on the test class.
import org.quickperf.spring.sql.QuickPerfSqlConfig;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.context.annotation.Import;
@RunWith(QuickPerfSpringRunner.class)
@Import(QuickPerfSqlConfig.class)
@DataJpaTest()
public class PlayerRepositoryTest {
}
You have to execute the test in a dedicated JVM, for example, using HeapSize
annotation.
If you don't do this, QuickPerf will display the following message on the console to help you in the configuration:
Execute the test in a dedicated JVM by adding @HeapSize(value = ..., unit = AllocationUnit.MEGA_BYTE).
A heap size value around 50 megabytes may allow the test to run.
import football.FootballApplication;
import football.dto.PlayerWithTeamName;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.quickperf.jvm.allocation.AllocationUnit;
import org.quickperf.jvm.annotations.HeapSize;
import org.quickperf.spring.junit4.QuickPerfSpringRunner;
import org.quickperf.sql.annotation.ExpectSelect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
@RunWith(QuickPerfSpringRunner.class)
@SpringBootTest(classes = {FootballApplication.class}
, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT
)
public class PlayerControllerTest {
@LocalServerPort
private int port;
@Autowired
private TestRestTemplate restTemplate;
@ExpectSelect(1)
@HeapSize(value = 50, unit = AllocationUnit.MEGA_BYTE)
@Test
public void should_get_all_players() {
// GIVEN
String url = "http://localhost:" + port + "/players";
// WHEN
ParameterizedTypeReference<List<PlayerWithTeamName>> paramType = new ParameterizedTypeReference<List<PlayerWithTeamName>>() {};
ResponseEntity<List<PlayerWithTeamName>> playersResponseEntity = restTemplate
.exchange(url, HttpMethod.GET, null, paramType);
// THEN
assertThat(playersResponseEntity.getStatusCode()).isEqualTo(HttpStatus.OK);
List<PlayerWithTeamName> players = playersResponseEntity.getBody();
assertThat(players).hasSize(2);
}
}
<bean id="QuickPerfProxyBeanPostProcessor" class = "org.quickperf.spring.sql.QuickPerfProxyBeanPostProcessor" />
You can't yet use QuickPerf annotations when Spring is used together with TestNG. If you are interested in this feature, you can help us to develop it. Don't hesitate to leave a comment on this issue.
π Β 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