Spring Test Scope

A Spring Test Scope

License

License

MIT
GroupId

GroupId

com.github.marschall
ArtifactId

ArtifactId

spring-test-scope
Last Version

Last Version

1.1.0
Release Date

Release Date

Type

Type

jar
Description

Description

Spring Test Scope
A Spring Test Scope
Project URL

Project URL

https://github.com/marschall/spring-test-scope
Source Code Management

Source Code Management

https://github.com/marschall/spring-test-scope

Download spring-test-scope

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.marschall/spring-test-scope/ -->
<dependency>
    <groupId>com.github.marschall</groupId>
    <artifactId>spring-test-scope</artifactId>
    <version>1.1.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.marschall/spring-test-scope/
implementation 'com.github.marschall:spring-test-scope:1.1.0'
// https://jarcasting.com/artifacts/com.github.marschall/spring-test-scope/
implementation ("com.github.marschall:spring-test-scope:1.1.0")
'com.github.marschall:spring-test-scope:jar:1.1.0'
<dependency org="com.github.marschall" name="spring-test-scope" rev="1.1.0">
  <artifact name="spring-test-scope" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.marschall', module='spring-test-scope', version='1.1.0')
)
libraryDependencies += "com.github.marschall" % "spring-test-scope" % "1.1.0"
[com.github.marschall/spring-test-scope "1.1.0"]

Dependencies

provided (5)

Group / Artifact Type Version
org.springframework : spring-beans jar
org.springframework : spring-core jar
org.springframework : spring-context jar
org.springframework : spring-test jar
org.springframework : spring-jcl jar

test (3)

Group / Artifact Type Version
org.junit.jupiter : junit-jupiter-api jar
org.junit.jupiter : junit-jupiter-engine jar
org.apache.logging.log4j : log4j-slf4j-impl jar

Project Modules

There are no modules declared in this project.

Spring Test Scope Maven Central Build Status javadoc

A simple test scope for Spring.

Sometimes you have some stateful beans, eg. due to caching, that need to be recreated for every test. Using @DirtiesContext would throw the entire application context away, possibly slowing down test execution by a lot. This project introduces a test scope allows for only certain beans to be recreated while the rest of the application context can be reused.

Usage

Add the dependency

<dependency>
  <groupId>com.github.marschall</groupId>
  <artifactId>spring-test-scope</artifactId>
  <version>1.1.0</version>
  <scope>test</scope>
</dependency>

Define your beans as @TestScoped

@Bean
@TestScoped
public TestScopedBean testScopedBean() {
  return new TestScopedBean();
}

If you don't want to (re)define a bean you can instead use @TestScopedBeans on a test to redefine some beans as test scoped.

@SpringJUnitConfig
@TestScopedBeans("testScopedBean")
class MyTests {

  @Autowired
  private TestScopedBean testScopedBean;

Requirements

  • The code has only been tested with Spring 5.3.
  • Java 11 is required.
  • The code has only been tested with JUnit 5, in theory JUnit 4 and TestNG should work as well

Caveats

The current implementation has some limitations:

  • The presence of a ContextCustomizerFactory may prevent sharing of an application context across test classes.
  • The test scope is opened in #prepareTestInstance and closed in #afterTestExecution. Before and after these methods in the same thread there is not test scope available and accessing test scoped beans will result in an exception. This works fine if a bean is injected into a test but may fail if:
    • a test scoped bean is referenced by an other bean in the application context
    • test instances are reused but injection is not repeated

Versions

Version
1.1.0
1.0.0