Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
davideas opened this issue Aug 19, 2016 · 18 comments

Comments

@davideas
Copy link

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.

@sbrannen
Copy link
Owner

Nothing has changed with regard to support for XML configuration files.

Thus, you must have configured something incorrectly.

Where does your testContext.xml file reside?

Is it in the root of your classpath (e.g., src/test/resources/testContext.xml)?

@sbrannen
Copy link
Owner

As a side note, the declaration of transactionManager = "transactionManager" is superfluous since transactionManager is the default name.

@davideas
Copy link
Author

davideas commented Aug 21, 2016

Thanks for the answer.
If I use @RunWith(SpringJUnit4ClassRunner.class) it works as usual, if I use @ExtendWith(SpringExtension.class) it doesn't initialize beans.

Yes it is in my classpath, I use the default folder structure and IDEA lint validations is ok with "classpath:testContext.xml", if I change it, it complains with Could not autowire. No beans of 'Service' type found.

+--main
|  +--java
|  +--resources
|  +--webapp
|  |  +--WEB-INF
|  |  |  +--applicationContext.xml
+--test
|  +--java
|  |  +--my.package
|  +--resources
|  |  +--testContext.xml

Maven test-compile produces:

+--target
|  +--my.package
|  +--testContext.xml

New maven dependencies for JUnit5:

<junit.jupiter.version>5.0.0-M2</junit.jupiter.version>
<junit.vintage.version>4.12.0-M2</junit.vintage.version>
<junit.platform.version>1.0.0-M2</junit.platform.version>
...
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.19.1</version>
    <dependencies>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-surefire-provider</artifactId>
            <version>${junit.platform.version}</version>
        </dependency>
    </dependencies>
</plugin>
...
<!-- JUnit -->
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <version>${junit.jupiter.version}</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.junit.vintage</groupId>
    <artifactId>junit-vintage-engine</artifactId>
    <version>${junit.vintage.version}</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.springframework.test</groupId>
    <artifactId>spring-test-junit5</artifactId>
    <version>1.0.0.BUILD-SNAPSHOT</version>
    <scope>test</scope>
</dependency>

PS. Thanks for the side note

@sbrannen
Copy link
Owner

I just introduced a test exactly like you have described in 36b00d6.

The above passes without any errors when using @RunWith(JUnitPlatform.class) within Eclipse and when using the JUnit Platform Gradle plugin within the Gradle build.

Thus, generally speaking, XML configuration is certainly supported.

@sbrannen
Copy link
Owner

If you could create a simple project that reproduces the problem you're encountering (e.g., in a public Git repository), I would be happy to look into it.

@davideas
Copy link
Author

Thanks Sam, ok during next week I will try to prepare it, so I can also test it separately from my project.

@sbrannen
Copy link
Owner

Sounds good. Thanks!

@davideas
Copy link
Author

davideas commented Aug 21, 2016

Sam, thanks to your new test, I was able to check better your code and the problem is that I have old junit-4.12.jar in the classpath even if I've removed from maven dependencies (I don't know why it is still present) AND the existing imports were still present:

import org.junit.Before;
import org.junit.Test;

I couldn't be aware of them because it compiles as it should be, but they don't work with JUnit5, so we have to import:

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Maybe you can specify this in your README to remind us that we need to be sure to use Annotations from the new org.junit.jupiter.api.* classes especially if we come from the old version.

@davideas
Copy link
Author

Another little question, how to force execution orders?

@sbrannen
Copy link
Owner

Sam, thanks to your new test, I was able to check better your code and the problem is that I have old junit-4.12.jar in the classpath even if I've removed from maven dependencies (I don't know why it is still present) AND the existing imports were still present:

junit-4.12.jar is brought in as a transitive dependency via junit-vintage-engine. So if you're not using JUnit 3 or JUnit 4, you can safely remove the junit-vintage-engine dependency.

@sbrannen
Copy link
Owner

Another little question, how to force execution orders?

As of JUnit 5.0 M2, there is no support yet for controlling the order in which tests in JUnit Jupiter are executed. See the following issues for details:

@sbrannen
Copy link
Owner

sbrannen commented Aug 21, 2016

Maybe you can specify this in your Readme to remind us that we need to be sure to use Annotations from the new org.junit.jupiter.api.* classes especially if we come from the old version.

Sure, I can add a note on that.

@sbrannen
Copy link
Owner

Closing this issue since the original poster has resolved the issue.

@davideas
Copy link
Author

Thanks for the support!

@sbrannen
Copy link
Owner

You're welcome!

@sbrannen
Copy link
Owner

Note added to README in 57aaa1f.

@davideas
Copy link
Author

I write here for another thing:
SpringExtensionTestSuite.java (class is in your project) should execute all tests? why it says:

INFORMAZIONI: Discovered TestEngines with IDs: [junit-jupiter, junit-vintage]
Empty test suite.
Empty test suite.

Indeed I also have empty suite:

@RunWith(JUnitPlatform.class)
@IncludeEngines("junit-jupiter")
@SelectPackages("eu.davidea.tamarindo")
public class AllTests {
}

@sbrannen
Copy link
Owner

Your last question is not related to the SpringExtension, so please ask a question under the junit5 tag on Stack Overflow: http://stackoverflow.com/tags/junit5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants