I am trying to write an integration for Spring Boot application:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
#SpringBootApplication
#ComponentScan(basePackages = { "com.lapots.tree.model.web" })
public class TreeModelApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(TreeModelApplication.class, args);
}
#Bean
public ServletRegistrationBean servletRegistrationBean() throws Exception {
ServletRegistrationBean registrationBean = new ServletRegistrationBean(new RootServlet(), "/tree-model-app");
registrationBean.setLoadOnStartup(1);
registrationBean.setAsyncSupported(true);
return registrationBean;
}
}
The test looks like this
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.web.client.RestTemplate;
import static org.junit.jupiter.api.Assertions.assertEquals;
#SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
classes = TreeModelApplication.class)
#ExtendWith(SpringExtension.class)
public class TreeModelApplicationIntegrationTest {
private static final String PING_URL = "http://localhost:8080/tree-model-app/ping";
private RestTemplate restTemplate = new RestTemplate();
#Test
public void routerReturnsTrueOnPing() {
String response = restTemplate.getForObject(PING_URL, String.class);
assertEquals("false", response, "Ping response should be the same as expected.");
}
}
However when I run gradlew test - nothing happens - build successful and reports are empty. What is the problem?
My build.gradle is
buildscript {
ext {
springBootVersion = '2.0.0.BUILD-SNAPSHOT'
}
repositories {
mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
jcenter()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
maven { url 'https://jitpack.io' }
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-actuator')
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.springframework.boot:spring-boot-starter-webflux')
compile('org.springframework.boot:spring-boot-starter-websocket')
compile('org.springframework.boot:spring-boot-starter-jetty')
runtime('org.springframework.boot:spring-boot-devtools')
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile("org.springframework:spring-test")
testCompile("org.junit.platform:junit-platform-runner:$junitPlatformVersion")
testCompile("org.junit.jupiter:junit-jupiter-engine:$junitJupiterVersion")
testCompile("org.junit.jupiter:junit-jupiter-api:$junitJupiterVersion")
testCompile("com.github.sbrannen:spring-test-junit5:1.0.0.M4")
}
So the issue is gradle plugin was not set up to run jUnit 5 tests
Here the conf needed pasted directly from JUnit5 website :
buildscript {
repositories {
mavenCentral()
// The following is only necessary if you want to use SNAPSHOT releases.
// maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
}
dependencies {
classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0-M4'
}
}
apply plugin: 'org.junit.platform.gradle.plugin'
Try to add this #RunWith(SpringRunner.class) to the test class declaration
Related
The application run as expected when running as bootRun task under Gradle Tasks in Eclipse, however, right clicking on project --> Run As --> Spring Boot App doesn't replace the value of the property in the following prototype.
build.gradle file
import org.apache.tools.ant.filters.ReplaceTokens
plugins {
id 'org.springframework.boot' version '2.6.1'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
id 'eclipse'
}
group = 'com.sample.auto.expanson'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
test {
useJUnitPlatform()
}
// enable auto property expansion for passing gradle property to spring boot
// https://www.baeldung.com/spring-boot-auto-property-expansion
processResources {
duplicatesStrategy = 'include'
with copySpec {
from 'src/main/resources'
include '**/application*.properties'
include '**/application*.yaml'
include '**/application*.yml'
project.properties.findAll().each {
prop ->
if (prop.key != null) {
filter(ReplaceTokens, tokens: [(prop.key): prop.value.toString()])
filter(ReplaceTokens, tokens: [('project.' + prop.key): prop.value.toString()])
}
}
}
}
gradle.properties
expansion.property=Hello Expansion Property!
application.properties
com.test.value=#expansion.property#
DemoApplication.java
package com.sample.auto.expanson.demo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class DemoApplication implements CommandLineRunner {
#Value("${com.test.value}")
String expansionProperty;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
#Override
public void run(String... args) throws Exception {
System.out.println(expansionProperty);
}
}
The bootRun task results in expected output:
Hello Expansion Property!
BUILD SUCCESSFUL in 1s
4 actionable tasks: 4 executed
Running it as Spring Boot App in Eclipse results literal output without the value being replaced.
2021-12-04 21:49:35.598 INFO 27293 --- [ main] c.s.auto.expanson.demo.DemoApplication : Started DemoApplication in 1.097 seconds (JVM running for 2.105)
#expansion.property#
I am new to spring-boot. I have created a simple spring-boot version 2.2.6 project with gradle build. I have created a welcome jsp just to print a header. I get 404 status when i run it on my server with http://localhost:8081/welcome.html Following is my build.gradle, application class, controller class and application.properties file:
//build.gradle
plugins {
id 'org.springframework.boot' version '2.2.6.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
}
group = 'com.banuprojects'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.apache.tomcat.embed:tomcat-embed-jasper'
implementation 'javax.servlet:jstl'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
test {
useJUnitPlatform()
}
//application class
package com.banuprojects.lmsdemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class LmsdemoApplication {
public static void main(String[] args) {
SpringApplication.run(LmsdemoApplication.class, args);
}
}
//controller class
package com.banuprojects.lmsdemo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
#Controller
public class TestController {
#RequestMapping("/welcome.html")
public ModelAndView firstPage() {
return new ModelAndView("welcome");
}
}
//application.properties
spring.mvc.view.prefix:/WEB-INF/jsp/
spring.mvc.view.suffix:.jsp
I am not sure where I have one wrong with the implementation. Any help will be appreciated.
Thank you
As discussed over the comments. After seeing the application properties file, found that wrong port was passed in the URL.
No other technical error found.
I have a spring boot multi-module project with Gradle. While running the application i am getting the below error:-
Description:
A component required a bean named 'org.springframework.boot.context.internalConfigurationPropertiesBinder' that could not be found.
Action:
Consider defining a bean named 'org.springframework.boot.context.internalConfigurationPropertiesBinder' in your configuration.
The project has a root project which has a subproject 'GetAssociationService'
rootproject - build.gradle
plugins {
id 'org.springframework.boot' version '2.2.5.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
id 'com.github.johnrengelman.shadow' version '5.2.0'
}
bootJar { enabled = false }
jar { enabled = true }
subprojects {
group = 'org.qmetech'
version = '1.0.0'
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'com.github.johnrengelman.shadow'
apply plugin: 'java'
}
repositories { mavenCentral ( ) }
ext { set ( 'springCloudVersion' , "Hoxton.SR3" ) }
dependencies {
testImplementation ( 'org.springframework.boot:spring-boot-starter-test' ) {
exclude group: 'org.junit.vintage' , module: 'junit-vintage-engine'
}
}
ext.libraries = [
commonlibraries: [ 'org.springframework.boot:spring-boot-starter:2.2.5.RELEASE' ,
'org.springframework.cloud:spring-cloud-function-context' ,
'org.springframework.cloud:spring-cloud-function-adapter-aws:2.0.1.RELEASE' ,
'com.amazonaws:aws-lambda-java-log4j:1.0.0' ,
'org.springframework.boot:spring-boot-starter-web:2.2.5.RELEASE' ,
'org.springframework.cloud:spring-cloud-starter-function-web:1.0.0.RELEASE' ,
'org.springframework.boot.experimental:spring-boot-thin-layout:1.0.11.RELEASE' ,
'org.springframework.boot:spring-boot-starter-data-mongodb:2.2.5.RELEASE',
'org.springframework.cloud:spring-cloud-function-compiler:2.0.0.RELEASE'] ,
]
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
test { useJUnitPlatform ( ) }
ChildProject - build.gradle
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
springBoot {
mainClassName = 'org.qmetech.GetAssociationService'
}
dependencies {
dependencies {
compile libraries.commonlibraries
implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
}
}
UserRepository.java
package org.qmetech.repository;
import org.qmetech.domain.User;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;
#Repository
public interface UserRepository extends MongoRepository<User, Integer> { }
AssociationService.java
package org.qmetech.service;
import org.qmetech.domain.User;
import org.qmetech.repository.UserRepository;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.function.Function;
#Component
public class AssociationService implements Function<String, List<User>> {
private final UserRepository userRepository;
public AssociationService(UserRepository userRepository) {
this.userRepository = userRepository;
}
#Override
public List<User> apply(String uppercaseRequest) {
List<User> users = userRepository.findAll();
return users;
}
}
The Complete Code can be found here - https://github.com/iftekharkhan09/Services
Can Anyone please tell me what am I doing wrong?
Thanks!
I also had this problem and in my case, there was the problem in 'org.springframework.boot' version. I changed my version to 2.1.6 Release. Then the problem resolved.
Please verify your pom.xml and make sure you are using the matching version of the Spring boot framework for Spring Cloud.
e.g. For Spring Cloud Greenwich you will need Spring boot 2.1.x
Here is Spring cloud documentation : https://spring.io/projects/spring-cloud
Revisit section : Release Trains
I am currently working through the example on scheduling tasks using Spring 4, Java 1.8, and Gradle (https://spring.io/guides/gs/scheduling-tasks/).
However, when running this example, I receive the following error:
Error:(11, 8) java: cannot access org.springframework.web.WebApplicationInitializer
class file for org.springframework.web.WebApplicationInitializer not found
The source code to my Application.java is as follows:
package hello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.scheduling.annotation.EnableScheduling;
#SpringBootApplication
#EnableScheduling
public class Application extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class);
}
}
My gradle file:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.2.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'war'
jar {
baseName = 'gs-scheduling-tasks'
version = '0.1.0'
}
repositories {
mavenCentral()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
compile("org.springframework.boot:spring-boot-starter")
testCompile("org.springframework.boot:spring-boot-starter-test")
providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
}
Why is org.springframework.web.WebApplicationInitializer causing this error when it is not even mentioned in the guide?
Thanks.
Add this dependency to fix this problem. I perfectly worked for me.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
The guide does not extend SpringBootServletInitializer, remove it or add spring-boot-starter-web as a dependency if you want to start a Tomcat webserver inside you application.
You are extending SpringBootServletInitializer which implements WebApplicationInitializer
check http://grepcode.com/file/repo1.maven.org/maven2/org.springframework.boot/spring-boot/1.0.2.RELEASE/org/springframework/boot/context/web/SpringBootServletInitializer.java
Use this Application class instead as the guide is showing
#SpringBootApplication
#EnableScheduling
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class);
}
}
I am new to Spring and i want to load some css and js into a view.
The static content is now in src/main/resources/static/... but when i navigate to localhost:8080/css/test.css it gives me a 404 error..
I don't use any xml configuration files.
My project structure:
Update: Application class
package com.exstodigital.photofactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Update: build.gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.0.RELEASE")
}
}
group 'PTS4'
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'spring-boot'
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-thymeleaf")
compile("org.springframework.boot:spring-boot-devtools")
testCompile("junit:junit")
}
Some controller (just testing stuff):
package com.exstodigital.photofactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
#Controller
//#Controller
public class MainController {
//#ResponseBody
#RequestMapping("/greeting/{name}/{hoi}")
public String greeting(#PathVariable("name") String name, #PathVariable("hoi") String hoi) {
//model.addAttribute("message", "BERICHT");
return name;
}
#RequestMapping("/test")
public String test() {
return "test";
}
}
Your structure is correct. Don't forget to make gradle clean task before run bootRun.