problem in spring boot test by use #Import - maven

I work with spring boot test and use junit4. I want use #Import for TestConfiguration classes in test class. But when i run maven test get this exception:
[main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper.buildDefaultMergedContextConfiguration - Neither #ContextConfiguration nor #ContextHierarchy found for test class [com.service.OrchestratedServiceTest], using SpringBootContextLoader

Related

Spring boot error: Unable to load class 'com.mysema.codegen.model.Type'Unable to load class

Spring boot error: Unable to load class 'com.mysema.codegen.model.Type'Unable to load class 'com.mysema.codegen.model.Type'
SpringBoot: 2.6.8,
querydslVersion = '4.3.1'
Solution:
// update the query dsl version, e.g:
implementation 'com.querydsl:querydsl-core:5.0.0'

Junit tests start failing after adding #ComponentScan in the Spring boot application class file

I am using Spring boot version 2.3.3.Release
Junit platform launcher 1.5.2
If I add #ComponentScan under #SpringBootApplication, the tests fail with the error :-
Caused by org.springframework.dao.InvalidDataAccessApiUsageException at RepositoryConfigurationExtensionSupport.java:367
My tests have the config like :-
#ExtendsWith(SpringExtension.class)
#WebMvcTest(value = Abcd.class)
And i use :-
#MockBean for service layer and
#Autowired for MockMvc bean
EDIT 1:
I added the ComponentScan to include an external library. The jar is in a lib folder and i use gradle compile filetree to include it.

Error configuring mongo and neo4j with spring boot 1.5.7

I am using MongoDb and Neo4j in my Spring Boot Application. I have recently updated by Spring Boot Gradle Plugin from 1.2.6 to 1.5.7.
I am having two config files one for mongo and other for neo4j. After updating the version of spring boot I found that #EnableMongoRepositories and #EnableNeo4jRepositories are showing following errors in their respective config files:
No constructor with 1 argument defined in class
'org.springframework.data.mongodb.repository.support.MongoRepositoryFactoryBean'
No constructor with 1 argument defined in class
'org.springframework.data.neo4j.repository.support.GraphRepositoryFactoryBean'
I am having following annotations in neo4j config:
#Configuration
#EnableNeo4jRepositories("<packagename>.neo4j.repository")
#EnableTransactionManagement
public class DatabaseConfigurationNeo4j
extends Neo4jConfiguration { ... }
and following annotations in mongo config:
#Configuration #Profile("!" + Constants.SPRING_PROFILE_CLOUD)
#EnableMongoRepositories("<packagename>.repository")
#Import(value = MongoAutoConfiguration.class)
#EnableMongoAuditing(auditorAwareRef = "springSecurityAuditorAware")
public class DatabaseConfiguration extends AbstractMongoConfiguration
{ ... }
If I remove these #EnableRepositories line from the files, these errors are removed but when I run it, the repositories bean are not creating. I think these lines are necessary but dont know how to remove this error.
Thank you.
updating spring-neo4j to 4.2.8 (not 5.0.0 as neo4j 5.0.0 will work with spring-boot 2.0 M and above) and spring-mongo to 1.10.6 solved the configuration issues.

WebTestClient does not work when using spring-webflux

I'm using Spring webflux via Spring boot 2.0.0.M3. Below is the dependencies of my project,
dependencies {
compile 'org.springframework.boot:spring-boot-starter-actuator',
'org.springframework.cloud:spring-cloud-starter-config',
'org.springframework.cloud:spring-cloud-sleuth-stream',
'org.springframework.cloud:spring-cloud-starter-sleuth',
'org.springframework.cloud:spring-cloud-starter-stream-rabbit',
'org.springframework.boot:spring-boot-starter-data-mongodb-reactive',
'org.springframework.boot:spring-boot-starter-data-redis-reactive',
'org.springframework.boot:spring-boot-starter-integration',
"org.springframework.integration:spring-integration-amqp",
"org.springframework.integration:spring-integration-mongodb",
'org.springframework.retry:spring-retry',
'org.springframework.boot:spring-boot-starter-webflux',
'org.springframework.boot:spring-boot-starter-reactor-netty',
'com.fasterxml.jackson.datatype:jackson-datatype-joda',
'joda-time:joda-time:2.9.9',
'org.javamoney:moneta:1.0',
'com.squareup.okhttp3:okhttp:3.8.1',
'org.apache.commons:commons-lang3:3.5'
compileOnly 'org.projectlombok:lombok:1.16.18'
testCompile 'org.springframework.boot:spring-boot-starter-test',
'io.projectreactor:reactor-test',
'org.apache.qpid:qpid-broker:6.1.2',
'de.flapdoodle.embed:de.flapdoodle.embed.mongo'
}
The app is running well via ./gradlew bootRun or running main app directly.
However I failed to start integration test due to below error.
Caused by: org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat
I'm wondering why WebTestClient still tries to use embedded tomcat even though we are using webflux that uses reactive-netty by default.
Is it something misconfiguration or bug of spring boot test?
Below is code snippet of my test case,
#RunWith(SpringRunner.class)
#SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class NoteHandlerTest {
#Autowired
private WebTestClient webClient;
#Test
public void testNoteNotFound() throws Exception {
this.webClient.get().uri("/note/request/{id}", "nosuchid").accept(MediaType.APPLICATION_JSON_UTF8)
.exchange().expectStatus().isNotFound();
}
}
The error of launching tomcat was caused by test dependency org.apache.qpid:qpid-broker:6.1.2, which depends on javax.serlvet-api:3.1 break the tomcat's startup.
Excluding below useless modules to make tomcat launching again,
configurations {
all*.exclude module: 'qpid-broker-plugins-management-http'
all*.exclude module: 'qpid-broker-plugins-websocket'
}

Spring-boot #ImportResource ignored in Weblogic

I'm facing a strange issue while deploying a spring-boot legacy-war in weblogic-12c. The same war/application works in both mvn spring-boot:run (embedded-tomcat) / Standalone tomcat war deployment.
The #ImportResource configured in the main application is not getting loaded which is causing few bean-injection inconsistencies. Is there any known issue to be worked-around for deploying in weblogic12c?
Note: I've already tried below:
1. extends SpringBootServletInitializer implements WebApplicationInitializer
2. Separate inner-config class
#Configuration #ImportResource({
"classpath*:**/**my-applicationContext.xml"}) #ComponentScan(basePackages =
{"com.myapp"
})
SLF4J exclude in weblogic.xml
<wls:prefer-application-packages>
<wls:package-name>org.slf4j</wls:package-name>
<wls:package-name>com.google.common.*</wls:package-name>
</wls:prefer-application-packages>
The issue was with the Antlr pattern used for loading xml-appcontexts. Strangely the same expression worked in embedded tomcat / standalone tomcat.
The fix was to expand few of the *
#Configuration
#ImportResource({
"classpath*:mypackage/**my-applicationContext.xml"})
#ComponentScan(basePackages = {"com.myapp" })
Debug info:
AbstractBeanDefinitionReader (Line 216) - spring-beans-4.2.4

Resources