Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
154 views
in Technique[技术] by (71.8m points)

java - JUnit can not discover test programatically inside a Spring Boot application packaged as a JAR

I have an existing application that I have wrapped inside a spring boot application to provide it with a REST API. The application uses JUnit at its core to run some tests (I am aware this is not conventional).

Since the introduction of Spring Boot, the application is no longer able to find the jupiter-engine and my test methods.

Interestingly enough, running the application from the IDE (Intellij) works fine. But as a standalone jar (java -jar), it does not.

I looked in the packaged jar, everything is stored in the BOOT-INF directory. Without Spring, the directory structure is flatter without the BOOT-INF directory (obviously).

My test cases are stored in the package scenarios. My application is in com.foo.bar.app. Note that my test cases are in the main package and not in the test package.

(Again, I know it's unconventional, but they are not regular unit tests.)

I used to discover test cases with

        LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder
                .request()
                .selectors(DiscoverySelectors.selectPackage("scenarios"))
                .build();

However, with spring I am only able to discover them as follows.

        LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder
                .request()
                .selectors(DiscoverySelectors.selectPackage(""))
                .build();

Yet, the launcher is not able to launch the TestPlan since it can't find the engine. I am getting the feeling that JUnit is looking in a hardcoded path, which is outside of the BOOT-INF directory.

I did some additional testing and I found that

DiscoverySelectors.selectMethod(className + "#" + method)

fails with

Could not load class with name: scenarios.path.to.class

The test class is packaged together with the application, but it's in the BOOT-INF directory.

Are there some configurations I am missing?

question from:https://stackoverflow.com/questions/65935140/junit-can-not-discover-test-programatically-inside-a-spring-boot-application-pac

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...