Spring 3.2 unit testing with Java based configuration - java

I am using Spring 3.2 with Java based configuration and have some problems with my unit tests (JUnit 4.8.1). So this is a test runner:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(classes={TestConfig.class})
public class ManualTest
{
#Autowired
...
Howeever, I am receiving this error:
Caused by: java.lang.IllegalStateException: CGLIB is required to process #Configuration classes. Either add CGLIB to the classpath or remove the following #Configuration bean definitions: [testConfig]
at org.springframework.context.annotation.ConfigurationClassPostProcessor.enhanceConfigurationClasses(ConfigurationClassPostProcessor.java:327)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanFactory(ConfigurationClassPostProcessor.java:222)
As the Spring blog states, Spring 3.2 is inlining CGLIB 3. So why do I receive this error?
I am using Gradle 1.3 as build management tool and STS as IDE. When calling gradle eclipse gradle pulls in the dependencies twice: one time as plain jar and one time as library:
First as plain jar:
and than as library:
In the plain jar section I had still Spring 3.1 configured, while in the library section there was Spring 3.2. So I removed the plain jars and everything was working.
This is my project build.gradle
configurations
{
driver
}
dependencies
{
driver 'com.oracle:ojdbc6:11.2.0'
compile "org.springframework:spring-jdbc:$springVersion"
testCompile 'com.oracle:ojdbc6:11.2.0'
testCompile "org.springframework:spring-test:$springVersion"
testCompile "commons-dbcp:commons-dbcp:$dbcpVersion"
testCompile "junit:junit:$junitVersion"
testCompile "org.slf4j:slf4j-log4j12:$slf4jVersion"
}
sourceSets
{
main
{
java
{
srcDirs 'src/main/java', "$buildDir/generated-sources/"
}
}
}
And the build.gradle from the master project
configure(allprojects)
{
ext.dbcpVersion = '1.4'
ext.springVersion = '3.2.0.RELEASE'
ext.junitVersion = '4.8.1'
ext.slf4jVersion = '1.7.2'
}
subprojects
{
// Artifact settings
group = 'xxx'
version = '1.0-SNAPSHOT'
// Standard plugins
apply plugin: 'java'
apply plugin: 'eclipse'
// Repositories
repositories
{
mavenLocal()
maven
{
url "http://repo.springsource.org/release"
}
mavenCentral()
}
// Standard dependencies
dependencies
{
}
}

I deleted all Eclipse projects and settings and all Gradle temporary files. Then I tried to import the project in Eclipse (Import Gradle project..). This failed with an exception. Then I deleted the Gradle settings within the Eclipse project and after that the import worked.
So I will not use gradle eclipse with version 1.3.
Also the additional source set path did not make its way into the Eclipse project as source path.

I had the same issue. Just add this dependency to your pom.xml file:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
And your unit tests and runtime code should work properly without cglib errors.

Related

Why is springBoot {} not found?

I have a build script that looks something like this:
plugins {
id("org.springframework.boot") version "2.2.2.RELEASE" apply false
id("io.spring.dependency-management") version "1.0.9.RELEASE" apply false
id("java")
}
repositories {
mavenCentral()
}
allprojects {
// ...
}
project("core") {
apply(plugin = "org.springframework.boot")
apply(plugin = "io.spring.dependency-management") // plugin to manage spring dependencies
dependencies {
implementation("org.springframework.boot:spring-boot-starter-web")
testImplementation("org.springframework.boot:spring-boot-starter-test")
}
springBoot {
mainClassName = "com.example.App"
}
}
However, when building, gradle complains that
springBoot {
^ Unresolved reference: springBoot
If I remove the apply false on the spring plugins in the plugins {} block everything works fine.
What I don't understand is that why springBoot{} can't be resolved even if I have called apply(plugin = ) for spring boot in the "core" subproject?
My understanding is that in plugins {} I imported the plugins into the project but not apply it yet. Later in core subproject I apply the plugins and configure spring boot.
From the grade doc https://docs.gradle.org/current/userguide/kotlin_dsl.html#type-safe-accessors
The build script can not use type-safe accessors in this case because the apply() call happens in the body of the build script. You have to use other techniques instead, as demonstrated here:
Type-safe accessors are unavailable for model elements contributed by the following:
Plugins applied via the apply(plugin = "id") method
The project build script
Script plugins, via apply(from = "script-plugin.gradle.kts")
Plugins applied via cross-project configuration
You have to use configure option like below,
configure<SpringBootExtension> {
mainClassName = “ com.example.App”
}

Gradle project is not able to find JavaFX Application class despite adding JavaFX plugin

I am new to both Gradle and JavaFX. I have added the JavaFX plugin to my build.gradle following this and this. However, my main class Library.java is not able to detect the Application class of JavaFX when I am trying to extend it.
build.gradle
plugins {
// Apply the java-library plugin to add support for Java Library
id 'java-library'
id 'application'
id 'org.openjfx.javafxplugin' version '0.0.8'
}
repositories {
// Use jcenter for resolving dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
mavenCentral()
}
dependencies {
// This dependency is exported to consumers, that is to say found on their compile classpath.
api 'org.apache.commons:commons-math3:3.6.1'
// This dependency is used internally, and not exposed to consumers on their own compile classpath.
implementation 'com.google.guava:guava:28.0-jre'
// Use JUnit test framework
testImplementation 'junit:junit:4.12'
}
jar {
manifest {
attributes 'Main-Class': 'Chess.Library'
}
}
mainClassName = 'Chess.Library'
Screenshot of Library Class
There's no Application from javafx package at all. What am I missing here?
I am using Spring Tool Suite 4.0 as my IDE with Buildship Gradle plugin if that's of any help. I am also running on Oracle Java 13
Edit 1:
I have added the changes suggested and this is how my build.gradle now looks
plugins {
// Apply the java-library plugin to add support for Java Library
id 'java-library'
id 'application'
id 'org.openjfx.javafxplugin' version '0.0.8'
}
repositories {
// Use jcenter for resolving dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
mavenCentral()
}
dependencies {
// This dependency is exported to consumers, that is to say found on their compile classpath.
api 'org.apache.commons:commons-math3:3.6.1'
// This dependency is used internally, and not exposed to consumers on their own compile classpath.
implementation 'com.google.guava:guava:28.0-jre'
// Use JUnit test framework
testImplementation 'junit:junit:4.12'
}
jar {
manifest {
attributes 'Main-Class': 'Chess.Library'
}
}
javafx {
version = "13"
modules = [ 'javafx.controls' ]
}
mainClassName = 'Chess.Library'
But the problem is still there
I also checked my Project and External Dependencies, there are all the libraries except for javafx
I fixed the issue myself although not sure what was causing it, but my project's buildpath had an unbounded Java 13. Fixing that and restarting the IDE took care of it

Hibernate core being included from spring boot

I have a gradle project that has the following dependencies:
dependencies {
compile("com.googlecode.json-simple:json-simple:1.1.1")
compile("org.hibernate:hibernate-c3p0:5.2.12.Final")
compile("mysql:mysql-connector-java:5.1.44")
compile("org.springframework.boot:spring-boot-starter-aop")
compile("org.springframework.boot:spring-boot-starter-web")
compile group: 'org.springframework.boot', name: 'spring-boot-starter-log4j2'
}
And has the following to apply the spring boot plugin:
apply plugin: 'org.springframework.boot'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.9.RELEASE")
}
}
The problem I am having is that when I include the spring boot plugin, an older version of hibernate-core seems to be being imported into my project (5.0.12.Final). But my code uses the 5.2.12.Final hibernate-core library.
I can't understand exactly why the hibernate core library comes with the spring boot plugin, as I can't see it listed in its dependencies on maven central, however when I remove that dependency, the older version of hibernate seems to disappear.
I've tried excluding the module when declaring the dependency but that doesn't seem to be syntactically correct when excluding in the buildscript section.
Has anyone else had this problem? Any workarounds to exclude that version? Or maybe my setup all together is wrong.. Any help would be much appreciated :)
Finally figured this one out after many hours/
Seems obvious now after a bit more research, the problem was due to spring boots own dependency management, so all I have to do is specify the version of a particular module I want to use (if spring boot already includes it), and it worked! Here is what I added to my build.xml file
dependencyManagement {
dependencies {
dependency 'org.hibernate:hibernate-core:5.2.12.Final'
}
}

spring-boot gradle plugin messes ivy dependency configuration?

Below is my gradle build script:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.6.RELEASE")
}
}
apply plugin: 'spring-boot'
apply plugin: 'base'
task wrapper(type: Wrapper) {
gradleVersion = '2.12'
}
repositories {
ivy {
url 'my.url'
}
}
dependencies {
archives group: 'my.group', name: 'artifact.name', version: '16.06.29.5144', configuration: 'dist'
}
In it I try to add one dependency to archives configuration. This is dependency published into Ivy repo and it has several configuration, among them a dist configuration. But it does not have default configuration.
Now, if I run gradlew dependencies I get the following error:
Execution failed for task ':dependencies'.
Could not resolve all dependencies for configuration 'detachedConfiguration4'.
> Module version :gtest:unspecified, configuration 'detachedConfiguration4' declares a dependency on configuration 'default' which is not declared in the module descriptor for my.group:artifact.name:16.06.29.5144
When I remove spring-boot plugin, then error disappears and I see expected output:
archives - Configuration for archive artifacts.
\--- my.group:artifact.name:16.06.29.5144
Any ideas why spring-boot plugin breaks dependency on custom configuration?
Your custom artifact looks to be another trigger of a bug/limitation in Gradle. The failure's occurring due to some logic in the dependency management plugin that Spring Boot's plugin uses that, among other things, ensures that any exclusions declared in dependencies' poms are applied as intended.
You can work around the problem, at the cost of perhaps having to declare some additional exclusions, by telling the dependency management plugin not to apply Maven exclusion semantics:
dependencyManagement {
applyMavenExclusions false
}

Gradle doesn't seem to be resolving my project dependency properly

I'm pretty new to Gradle and am having an issue getting my a module that is dependant on another module to build properly.
So I have the following configuration for my modules.
subprojects {
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
testCompile 'junit:junit:4.10'
}
}
project(':web-service') {
apply plugin: 'war'
dependencies {
compile project(':core')
compile('com.sun.jersey:jersey-server:1.7')
compile('com.googlecode.json-simple:json-simple:1.1.1')
}
}
project(':core') {
dependencies {
compile('log4j:log4j:1.2.17')
}
}
If I try to build my core project everything succeeds as expected.
However, if I try to build the web-service project with the following command:
gradle :web-service:build
It appears to build the core project first as expected but then encounter build errors that indicate that classes that exist in the core module cannot be found.
What gives?
Turns out this was completely my fault. I dug deeper on the error messages that I was getting and found some package does not exist messages at the top. Turns out that my directory structure was not inline with my package names.

Categories

Resources