GithubHelp home page GithubHelp logo

sbrannen / spring-test-junit5 Goto Github PK

View Code? Open in Web Editor NEW
141.0 21.0 45.0 495 KB

Spring TestContext Framework Extension for JUnit Jupiter (a.k.a., JUnit 5)

License: Apache License 2.0

Java 100.00%

spring-test-junit5's Introduction

Spring JUnit 5 Testing Support

This project served as the official prototype for JUnit 5 testing support in the Spring TestContext Framework and has been incorporated into Spring Framework 5.0 in conjunction with SPR-13575. Consequently, no further work is planned in this repository in terms of new features: new features are only supported in Spring Framework 5.0+. Note, however, that this project can in fact be used for JUnit Jupiter testing support in conjunction with Spring Framework 4.3.x.

Using the SpringExtension

Currently, all that's needed to use the Spring TestContext Framework with JUnit 5 is to annotate a JUnit Jupiter based test class with @ExtendWith(SpringExtension.class) and whatever Spring annotations you need (e.g., @ContextConfiguration, @Transactional, @Sql, etc.), but make sure you use @Test, @BeforeEach, etc. from the appropriate org.junit.jupiter.api package. See SpringExtensionTests for an example of this extension in action, and check out the source code of SpringExtension if you're interested in the implementation details.

Composing Annotations from Spring & JUnit

Spring has supported composed annotations for several years now, and as of JUnit 5 annotations in JUnit can also be used as meta-annotations. We can therefore create custom annotations that are composed from Spring annotations and JUnit 5 annotations. Take a look at @SpringJUnitJupiterConfig for an example, and check out ComposedSpringExtensionTests for an example of @SpringJUnitJupiterConfig in action.

License

This project is released under version 2.0 of the Apache License.

Artifacts

There are currently no downloadable artifacts for this project; however, you may opt to install spring-test-junit5 in your local Maven repository or include a dependency on spring-test-junit5 via JitPack. See the following sections for further details.

Local Maven Installation

If you install in a local Maven repository (see below) the generated artifact will correspond to the following.

  • Group ID: org.springframework.test
  • Artifact ID: spring-test-junit5
  • Version: 1.0.0.BUILD-SNAPSHOT

JitPack

If you'd like to build against a release tag for spring-test-junit5, you may be interested in using JitPack. For example, to build against the 1.5.0 tag, the following Maven coordinates will work.

  • Group ID: com.github.sbrannen
  • Artifact ID: spring-test-junit5
  • Version: 1.5.0

JitPack with Gradle

repositories {
	mavenCentral()
	maven { url 'https://jitpack.io' }
}

// ...

dependencies {
	// ...
	testCompile('com.github.sbrannen:spring-test-junit5:1.5.0')
	// ...
}

JitPack with Maven

<repositories>
	<repository>
		<id>jitpack.io</id>
		<url>https://jitpack.io</url>
	</repository>
</repositories>

<!-- ... -->

<dependencies>
	<dependency>
		<groupId>com.github.sbrannen</groupId>
		<artifactId>spring-test-junit5</artifactId>
		<version>1.5.0</version>
		<scope>test</scope>
	</dependency>

	<!-- ... -->

</dependencies>

Building from Source

This project uses a Gradle-based build system. In the instructions below, ./gradlew is invoked from the root of the project and serves as a cross-platform, self-contained bootstrap mechanism for the build.

Prerequisites and Dependencies

Be sure that your JAVA_HOME environment variable points to the jdk1.8.0 folder extracted from the JDK download.

Compile and Test

Compile code, run tests, and build JARs, distribution ZIP files, and docs:

./gradlew build

Install spring-test-junit5 in local Maven repository

./gradlew publishToMavenLocal

Running Tests with Gradle

./gradlew test

Building and Testing with JDK 9, 10, & 11

spring-test-junit5 can also be built with and tested against JDK 9.0.4, JDK 10.0.2, and JDK 11.0.2.

Running Tests in the IDE

In order to execute all of the tests within an IDE as a single suite, simply run SpringExtensionTestSuite as a JUnit 4 test class.


spring-test-junit5's People

Contributors

sbrannen avatar ttddyy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

spring-test-junit5's Issues

Spring-Enabled Extensions

In our project we use JUnit4 Rules with autowired fields. You can achieve this by having a Rule-Bean which then gets added to the test via autowiring:

public class MyTest() {
  @Rule
  @Autowired
  public TestRule myRule;
}

This works like charm - except for ClassRules.

Now it would be interesting if spring-test-junit5 enables this approach for Extensions.

I assume that the org.junit.gen5.engine.junit5.extension.ExtensionRegistry needs to be adopted in registerExtension in some way. The problem I see: each new test context requires its own Spring enabled extensions.

Any ideas?

No tests are run with junit-platform 1.2.0 and jupiter 5.2.0

I tried to upgrade from junit-platform 1.0.3 and jupiter 5.0.3 to 1.2.0 and 5.2.0 respectively. See zalando/logbook#262. But it looks like the extension no longer works. At least during my spring tests I see this:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.21.0:test (default-test) on project logbook-spring-boot-starter: No tests were executed! (Set -DfailIfNoTests=false to ignore this error.) -> [Help 1]

https://travis-ci.org/zalando/logbook/jobs/379081016#L1972

Spring annotation should not be required in nested test class

@SpringBootTest(...)
@ExtendWith(SpringExtension.class)
class OuterTests {
   @BeforeEach
   void init(@Autowired MyComponent component) {}

  @Test void test1() {}

  @Nested class InnerTests {
    @Test void test2() {}
  }
}

test2 will fail due to component lookup failure in init unless the nested test class is also annotated with @SpringBootTest(...). Should not be necessary IMO.

Support for nested tests

I've been playing with JUnit 5 and spring-test-junit5. I then tried to use the nested tests feature, but my tests fail. This also happens when I run directly from the command line using gradle.

	@ExtendWith(SpringExtension.class)
	@ContextConfiguration(classes = TestConfig.class)
	@DisplayName("In cat club")
	public class NestedCatTests {
	    @Autowired
	    Cat cat;
	
	    @Autowired
	    List<Cat> cats;
	
	    @Test
	    @DisplayName("Catbert is the cat")
	    void catWasAutowired() {
	        assertEquals(
	                "Catbert",
	                cat.getName()
	        );
	    }
	
	    @Nested
	    @DisplayName("Catbert")
	    class IsMemberOfClub {
	        @Test
	        @DisplayName("is a member")
	        void isMemberOf() {
	            assertTrue(cats.contains(cat));
	        }
	    }
	}

I get the following exception:

	java.lang.IllegalStateException: Failed to load ApplicationContext
	
	at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
	at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)
	at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117)
	at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
	at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230)
	at org.springframework.test.context.junit.jupiter.SpringExtension.postProcessTestInstance(SpringExtension.java:91)
	at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.lambda$null$1(ClassTestDescriptor.java:196)
	at org.junit.jupiter.engine.descriptor.JupiterTestDescriptor.executeAndMaskThrowable(JupiterTestDescriptor.java:102)
	...
	at java.lang.reflect.Method.invoke(Method.java:497)
	at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
	
	Caused by: java.lang.IllegalStateException: Neither GenericXmlContextLoader nor AnnotationConfigContextLoader was able to load an ApplicationContext from [MergedContextConfiguration@4816278d testClass = NestedCatTests.IsMemberOfClub, locations = '{}', classes = '{}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{}', contextCustomizers = set[[empty]], contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader', parent = [null]].
	at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:263)
	at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98)
	at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116)
	... 44 more

I've managed to get it to work if I add @ContextConfiguration(classes = TestConfig.class) to the nested class. I think however that support could be improved to resolve the configuration from the enclosing class.

SpringExtension speed up

Hi @sbrannen ,

Since I read SpringExtension source code, i found each test class will new TestContextManager, and then will restart spring boot. Are there any way to make testClass shared one TestContextManager ? Because restart spring boot will take long time, then make test slow, because there are a lot of integration tests in our project.

private static TestContextManager getTestContextManager(ExtensionContext context) {
		Assert.notNull(context, "ExtensionContext must not be null");
		Class<?> testClass = getRequiredTestClass(context);
		Store store = context.getStore(NAMESPACE);
		return store.getOrComputeIfAbsent(testClass, TestContextManager::new, TestContextManager.class);
	}

Could help me figure out how to speed up integration tests?

Better Spock integration with the changes for junit5?

Thank you @sbrannen for your proof of concept.

Is there a chance to make the Spock integration better with the changes for junit5?

Would be awesome!

Best regards, Leif

See spockframework/spock#76
/*
* Note: We cannot access Spring from this method, per this quote
* from the docs at https://code.google.com/p/spock/wiki/SpringExtension:
*
* "Note: Due to the way Spring's TestContext framework is designed,
* @shared fields cannot currently be injected. This also means that
* setupSpec() and cleanupSpec() cannot get access to Spring beans."
*/

How to use with @ContextConfiguration(locations = {"classpath:testContext.xml"})

Hello, I was trying out this extension but it seems that Spring is not initialized if used with .xml.
I launched your JUnit tests in your project with @ContextConfiguration(classes = TestConfig.class) and they works.

My bean with @Autowired has null pointer.
Here my code snipped:

@ExtendWith(SpringExtension.class)
@ContextConfiguration(locations = {"classpath:testContext.xml"})
@Transactional(transactionManager = "transactionManager")
public class ServiceTest {

    @Autowired
    private IService service;
}

Service is null when used in the tests.

beforeAll(ContainerExtensionContext context) not being overridden throwing AbstractMethodError in M5

In the 1.0.0.M4 release it appears you are overriding beforeAll(), afterAll(), etc with a ContainerExtensionContext object.

        /**
	 * Delegates to {@link TestContextManager#beforeTestClass}.
	 */
	@Override
	public void beforeAll(ContainerExtensionContext context) throws Exception {
		getTestContextManager(context).beforeTestClass();
	}

In 1.0.0.M5 it appears you switched this to the ExtensionContext object.

        /**
	 * Delegates to {@link TestContextManager#beforeTestClass}.
	 */
	@Override
	public void beforeAll(ExtensionContext context) throws Exception {
		getTestContextManager(context).beforeTestClass();
	}

Causing it to throw an AbstractMethodError for: org.springframework.test.context.junit.jupiter.SpringExtension.afterAll(Lorg/junit/jupiter/api/extension/ContainerExtensionContext;

Switching to M4 release everything works as expected.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.