SpringBootTest annotation doesn't work in maven build command - spring-boot

I add annotation SpringBootTest on my test class, it works very well in IDE(intellij), but when I run mvn test command, it seems SpringBootTest annotation doesn't work, because I didn't see spring context is initialized from logs and the auto-wired object is null. Does anyone know what might be wrong?
BTW, I use spring-boot 2.7.1

This is maven-surefire-plugin issue, spring boot 2.7.1 use junit5 while the maven-surefire-plugin I used doesn't support junit5. Upgrading maven-surefire-plugin can solve this problem

Related

#DirtiesContext not working as expected after upgrading to Spring Boot 2.2.2

I am trying to upgrade my application from Spring Boot v2.1.8 to v2.2.2. However, after upgrading some tests start failing.
The pattern of failing tests strongly indicates that #DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD), applied on test class, is failing to clear context after each test case.
I am unable to narrow down further. Will appreciate any help.
I had the same issue today. With spring boot 2.2.7 not playing nice with Dirties context. In my case, I was using DBUnit to test and it wasn’t resetting the DB.
Adding this annotation fixed it for me:
#AutoConfigureTestDatabase(replace = Replace.ANY)

IllegalStateException at start up Spring JUnit test

I've made a simple Maven Spring REST project. (this one: https://spring.io/guides/gs/rest-service/)
Afterwards I made a simple JUnit test using MockMVC to test the REST functionality. In this case: does my code respond to /greeting?
When I run my test (using IntelliJ) I get this IllegalStateException.
How can I resolve this?
You're probably using incompatible versions of spring-test and spring-boot. spring-test calls SpringApplicationContextLoader.getResourceSuffixes (plural form) since v4.1.0.RC2. spring-boot's SpringApplicationContextLoader.getResourceSufffix (singular) throws that exception since v1.3.0.RC1.
You would have to either upgrade to spring-test >= 4.1.0 or downgrade to spring-boot < 1.3.0

JMeter, JUnit and Spring Java configuration

Is it possible to run JMeter with the JUnit plugin/sampler and Spring Java configuration? When I try to do this, the Spring autowired beans are not being created and although the test case runs, because the beans have not been created, I get null pointer exceptions.
I am using the Spring annotation
#SpringJUnit4ClassRunner and #ContextConfiguration to configure the JUnit test (which works). The goal is to be able to write JUnit test cases that can be measured for performance using JMeter.
Yes, running JMeter with JUnit and Spring is a problem.
JMeter does not use standard JUnit runner; its specified at
User manual, point 7. There are subtle differences between standard JUnit test runners and JMeter's implementation. Because of this, #SpringJUnit4ClassRunner is becomes ignored. Workaround is to load beans as normal i.e using ApplicationContext.getBean().

Gradle AspectJ plugin issue

I am facing a major issue with my Spring application since I migrated from Maven to Gradle: aspectj no longer works.
For instance classes annotated with #Configurable (which rely on aspectj) don't get their dependencies injected.
In order to replace the aspectj-maven-plugin, I tried using this plugin: from spring security code base but it seems it is not compatible with Gradle 2.1...
I also tried applying the solution described here: http://architects.dzone.com/articles/working-gradle-spring-aspects to no avail.
I am just curious to know how people who use Gradle + Spring manage to leverage aspectj...
Can anyone please kindly advise?

Intellij spring boot integration

For some reason Intellij 13.1.3 doesn't detect the spring boot beans. Spring boot version is 1.1.1-Release. When i try to inject ObjectMapper in some component i get an error in the IDE "No bean of type ObjectMapper defined", however the application compiles and runs just fine and the ObjectMapper beans is visible in the /beans json.
I have added my application-context and my Application.java files to a spring facet and the IDE recognizes all my other beans that I manually defined, it just ignores the ones that come with Spring boot #EnableAutoConfiguration. Any idea how to solve that since its kinda tedious not to have the correct linking in the IDE and get errors all over the place. I would assume thats a common issue?
Thanks!
As an update for this answer: IntelliJ now supports Spring Boot. Must be at least R14. R15 is including some more integration.
Spring Boot is not supported yet, please watch http://youtrack.jetbrains.com/issue/IDEA-119230

Resources