The import cucumber cannot be resolved in Runner Class for Gradle Project - gradle

I don't whats wrong with my Gradle Dependencies but when I try to import import cucumber.api.junit.Cucumber; then I get error The import cucumber cannot be resolved
Below is my Gradle file:
apply plugin: 'java'
version = '1.0-SNAPSHOT'
dependencies {
compile group: 'info.cukes', name: 'cucumber-java', version: '1.2.5'
testCompile group: 'info.cukes', name: 'cucumber-junit', version: '1.2.5'
compile group: 'info.cukes', name: 'cucumber-core', version: '1.2.5'
testCompile group: 'info.cukes', name: 'cucumber-picocontainer', version: '1.2.5'
compile group: 'info.cukes', name: 'cucumber-guice', version: '1.2.5'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
repositories {
mavenCentral()
}
My Runner Class is:
package Runner;
import org.junit.runner.RunWith;
import cucumber.api.junit.CucumberOptions;
import cucumber.api.junit.Cucumber;
#RunWith(Cucumber.class)
Cucumber.options(
features = "\\src\\test\\java\\Feature",
glue = {"\\src\\main\\java\\Steps\\"},
tags = {"#ignored"},
format = {"json:target/cucumber/wikipedia.json","html:target/cucumber/wikipedia.html","pretty"}
)
public class RunnerMain {
}
When I comment #RunWith, CucumberOptions and remove the imports, then run command gradle build then I don't see any error and all goes well.
I don't know what wrong I am doing here.

Replace Cucumber.options with #CucumberOptions and try once

Related

equinox with slf4j can't find logback provider

So I have this OSGi project built with equinox with this gradle structure:
> Task :projects
------------------------------------------------------------
Root project 'project'
------------------------------------------------------------
Root project 'project'
No sub-projects
Included builds
+--- Included build ':callable-processor'
+--- Included build ':osgi-sun-misc'
+--- Included build ':path-collector'
+--- Included build ':robot-api'
+--- Included build ':robot-equinox'
+--- Included build ':robot-signal'
+--- Included build ':robot-instance'
+--- Included build ':utils'
\--- Included build ':reactive'
Main bundle is robot-equinox.
On the root (project), I have the following build.gradle configuration:
// project
ext.baseVersion = '2.0.0'
ext.buildTag = "SNAPSHOT"
group 'project'
version "${baseVersion}-${buildTag}"
project {
quality = ALFA
}
configurations {
bundleInstall
bundleStart
misc
}
dependencies {
bundleInstall 'ch.qos.logback:logback-classic:1.3.5'
bundleInstall 'ch.qos.logback:logback-core:1.3.5'
bundleInstall 'org.slf4j:slf4j-api:2.0.5'
bundleInstall 'org.apache.aries.spifly:org.apache.aries.spifly.dynamic.bundle:1.3.5'
bundleInstall 'prodist:callable-processor'
bundleInstall 'prodist:osgi-sun-misc'
bundleInstall 'prodist:path-collector'
bundleInstall 'prodist:reactive'
bundleInstall 'prodist:robot-api'
bundleStart 'ch.qos.logback:logback-classic:1.3.5'
bundleStart 'ch.qos.logback:logback-core:1.3.5'
bundleStart 'org.slf4j:slf4j-api:2.0.5'
bundleStart 'prodist:robot-equinox'
}
And under robot-equinox I have the following configuration:
dependencies {
implementation group: 'prodist', name: 'callable-processor'
implementation group: 'prodist', name: 'path-collector'
implementation group: 'prodist', name: 'robot-api'
implementation group: 'prodist', name: 'reactive'
implementation group: 'prodist', name: 'robot-instance'
implementation group: 'prodist', name: 'utils'
compileOnly group: 'org.osgi', name: 'osgi.core', version: '6.0.0'
testImplementation group: 'org.hamcrest', name: 'hamcrest', version: '2.2'
testImplementation group: 'org.mockito', name: 'mockito-core', version: '3.2.4'
testImplementation group: 'org.reactivestreams', name: 'reactive-streams-tck', version: '1.0.3'
testImplementation group: 'org.testng', name: 'testng', version: '7.1.0'
testCompileOnly group: 'org.osgi', name: 'osgi.core', version: '6.0.0'
}
I'll give a sample of my log implementation on the main class, which is present under 'project'.
Application.java
package project.robot.equinox;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.*;
import java.security.SecureRandom;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.IntStream;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
public class Application {
private static final Logger logger = LoggerFactory.getLogger(Application.class);
public Application ()
{
MDC.put("product.name", ProductInfo.getName());
MDC.put("product.version", ProductInfo.getVersion());
}
public void activate (BundleContext bundleContext)
{
logger.atTrace().log(_S("activate: enter"));
}
...
}
But when I start the Application, I'm getting this message on the osgi console:
osgi> SLF4J: No SLF4J providers were found.
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See https://www.slf4j.org/codes.html#noProviders for further details.
My expectation was that by installing both - slf4j and logback, slf4j would recognize logback implementation and would work.
How can I solve this problem?
Ok, so I found a solution and I'm going to post it here so it help someone else facing this same issue.
In order to help SLF4J find his provider, I create a new bundle osgi-log - a "log bundle" with this configuration:
group 'project'
version '2.0.0'
dependencies {
api group: 'org.slf4j', name: 'slf4j-api', version: '2.0.5'
implementation group: 'ch.qos.logback', name: 'logback-classic', version: '1.3.5'
implementation group: 'ch.qos.logback', name: 'logback-core', version: '1.3.5'
implementation group: 'org.ow2.asm', name: 'asm', version: '5.2'
implementation group: 'org.ow2.asm', name: 'asm-commons', version: '5.2'
implementation group: 'org.ow2.asm', name: 'asm-util', version: '5.2'
implementation group: 'org.apache.aries.spifly', name: 'org.apache.aries.spifly.dynamic.bundle', version: '1.3.5'
}
And then, in each bundle who uses log, I added the following dependency:
dependencies {
(...)
implementation group: 'project', name: 'osgi-log'
}
And when I started my application, the message about provider was gone and log was being registered as expected.
I came to conclusion that Apache Aries spifly offers a ServiceLoader to SLF4J and helps him find provider, which in my case is logback.

Building app with spring-boot + Cassandra, getting "Unable to load class 'com.datastax.oss.driver.api.mapper.entity.naming.GetterStyle'"

Facing isssue while integration with datastax with cassandra
gradle file :
plugins {
id 'org.springframework.boot' version '2.3.12.RELEASE'
id 'io.spring.dependency-management' version '1.0.13.RELEASE'
id 'java'
id 'org.sonarqube' version '3.2.0'
}
group = 'in.demo'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
def javaDriverVersion ='4.14.1'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation ('org.springframework.boot:spring-boot-starter')
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation('org.springframework.boot:spring-boot-starter-web') {
exclude module: 'spring-boot-starter-tomcat'
exclude group: 'com.fasterxml.jackson.core', module: 'jackson-core'
exclude group: 'com.fasterxml.jackson.core', module: 'jackson-databind'
exclude group: 'com.fasterxml.jackson.core', module: 'jackson-annotations'
}
implementation('org.springframework.boot:spring-boot-starter-jetty')
implementation('org.springframework.boot:spring-boot-starter-webflux')
runtimeOnly('org.springframework.boot:spring-boot-devtools')
// datastax driver
implementation group: 'com.datastax.oss', name: 'java-driver-core', version: javaDriverVersion
implementation group: 'com.datastax.oss', name: 'java-driver-query-builder', version: javaDriverVersion
//implementation group: 'com.datastax.oss', name: 'java-driver-mapper-runtime', version: javaDriverVersion
implementation group: 'com.datastax.oss', name: 'native-protocol', version: '1.5.0'
annotationProcessor group: 'com.datastax.oss', name: 'java-driver-mapper-processor', version: javaDriverVersion
//compileOnly group: 'com.datastax.oss', name: 'java-driver-mapper-processor', version: '4.14.1'
compileOnly group: 'com.datastax.oss', name: 'java-driver-mapper-runtime', version: javaDriverVersion
implementation 'org.mapstruct:mapstruct:1.3.1.Final'
annotationProcessor 'org.mapstruct:mapstruct-processor:1.3.1.Final'
}
tasks.named('test') {
useJUnitPlatform()
}
#Data
#AllArgsConstructor
#NoArgsConstructor
#Entity(defaultKeyspace = "quest_test")
public class TestTable {
private UUID id;
private String name;
}
import com.datastax.oss.driver.api.mapper.annotations.Dao;
import com.datastax.oss.driver.api.mapper.annotations.Select;
#Dao
public interface TestTableDao {
#Select
TestTable findById();
}
import com.datastax.oss.driver.api.mapper.annotations.DaoFactory;
import com.datastax.oss.driver.api.mapper.annotations.Mapper;
#Mapper
public interface TestTableMapper {
#DaoFactory
TestTableDao testTableDao();
}
getting error
Unable to load class 'com.datastax.oss.driver.api.mapper.entity.naming.GetterStyle'.
i tried to interaction with different project also but still not able to solve issue
. for testing part i added library mappstruct which is generation code on compile time
With drivers 4x+, you need to declare an annotation processor for the mappers code to be generated at build time.
Gradle file
dependencies {
annotationProcessor group: 'com.datastax.oss', name: 'java-driver-mapper-processor', version: javaDriverVersion
compile group: 'com.datastax.oss', name: 'java-driver-mapper-runtime', version: javaDriverVersion
}
Documentation
Here more code to work with Driver 4x
You might be interested in the full-fledged Spring PetClinic application running with your technical stack.

running app as jar gives fileNotFoundException

Im attempting to run my spring app as a jar, but i get the below error
java.io.FileNotFoundException: class path resource [org/springframework/web/socket/config/annotation/AbstractWebSocketMessageBrokerConfigurer.class] cannot be opened because it does not exist
It runs fine from intelij(as in my main class can runs using the play button). I find the file under project structure --> Libraries and under external libraries (albeit depracted)
Ive tried to look into it, but everywhere (list of examples below) seem to talk about xml files instead of a class
Spring ClassPathResource - cannot be opened because it does not exist
Class path resource cannot be opened because it does not existHow to avoid "class path resource [...] cannot be opened because it does not exist" in a spring boot application?
java.io.FileNotFoundException: class path resource can't be opened
java.io.FileNotFoundException: class path resource cannot be opened because it does not exist
My gradle file:
plugins {
id 'org.springframework.boot' version '2.2.4.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'war'
id 'groovy'
// id 'java'
}
war {
enabled = true
}
jar {
manifest {
attributes 'Main-Class': 'co.za.ebucks.HandlingRewardsFormSubmission'
}
}
group = 'co.za.ebucks'
version = '1.0.0'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
ext {
seleniumVersion = '3.141.59'
}
configurations {
developmentOnly
runtimeClasspath {
extendsFrom developmentOnly
}
}
test {
useTestNG()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-tomcat'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-webflux'
// implementation 'org.springframework.session:spring-session-core'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-integration'
implementation 'org.springframework.boot:spring-boot-starter-websocket'
implementation 'org.springframework:spring-messaging'
implementation 'org.springframework.integration:spring-integration-file'
implementation group: 'org.webjars.bower', name: 'angular-ui-router', version: '0.2.8'
implementation group: 'org.webjars', name:'angularjs', version: '1.3.0-beta.11'
implementation group: 'org.webjars', name: 'bootstrap', version: '3.1.1-1'
// implementation 'org.webjars.bower:angular-ui-router'//, version: '1.0.20'
implementation 'commons-io:commons-io'//, version: '2.4'
implementation 'org.apache.commons:commons-dbcp2'//, version: '2.7.0'
implementation 'org.hibernate.validator:hibernate-validator'//, version: '6.1.2.Final'
// implementation 'nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect'//, version: '2.4.1'
implementation 'javax.servlet:javax.servlet-api'//, version: '4.0.1'
// compile group: 'org.apache.tomcat', name: 'tomcat-jni', version: '9.0.33'
implementation group: 'de.codecentric', name: 'spring-boot-admin-starter-client', version: '2.2.2'
implementation group: 'de.codecentric', name: 'spring-boot-admin-server', version: '2.2.2'
implementation group: 'org.testng', name: 'testng', version: '7.1.0'
implementation group: 'org.codehaus.groovy', name: 'groovy-all', version: '3.0.0'
implementation group: 'com.google.inject', name: 'guice', version: '4.2.2'
implementation group: 'com.google.guava', name: 'guava', version: '28.1-jre'
implementation group: 'cglib', name: 'cglib', version: '3.3.0'
implementation group: 'org.postgresql', name: 'postgresql', version: '42.2.10'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
testImplementation 'io.projectreactor:reactor-test'
implementation "org.seleniumhq.selenium:selenium-support:${seleniumVersion}"
implementation group: 'io.appium', name: 'java-client', version: '7.3.0'
implementation group: 'org.seleniumhq.selenium', name: 'selenium-java', version: "${seleniumVersion}"
implementation group: 'org.seleniumhq.selenium', name: 'selenium-server', version: "${seleniumVersion}"
implementation group: 'org.seleniumhq.selenium', name: 'selenium-chrome-driver', version: "${seleniumVersion}"
testImplementation group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.13.0'
testImplementation group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.13.0'
testImplementation group: 'org.testng', name: 'testng', version: '7.1.0'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
}
And what Im sure is the class in question:
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry
import org.springframework.web.socket.server.support.DefaultHandshakeHandler;
#Configuration
#EnableWebSocketMessageBroker
class WebSocketDefaultConfig extends AbstractWebSocketMessageBrokerConfigurer {
#Override
void configureMessageBroker(MessageBrokerRegistry config) {
// config.enableStompBrokerRelay("/topic/", "/queue/");
config.enableSimpleBroker("/topic/", "/queue/");
config.setApplicationDestinationPrefixes("/app");
}
#Override
void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/tailfilesep")
// .setAllowedOrigins("*")
// .setHandshakeHandler(new DefaultHandshakeHandler())
.withSockJS()
}
}
Kindly assist me, this has passed frustration. Why is that class not in the jar?
The only thing that worked was creating a new project - copy and pasted all the code, build files everything... lol and its fine.
Intelij corruption somewhere along the line

Kotlin Gradle build is not running

I have a Spring Boot application backed with Kotlin as the language, and Gradle as the build system. So basically I'm trying to build a fat jar out of the application source and dependencies, which can be run using Java command line tool.
Gradle build script:
apply plugin: 'java'
apply plugin: 'kotlin'
apply plugin: "kotlin-spring"
apply plugin: 'org.springframework.boot'
group 'myapp'
version '1.0'
buildscript {
ext.kotlin_version = '1.2.21' // Required for Kotlin integration
ext.spring_boot_version = '1.5.4.RELEASE'
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // Required for Kotlin integration
classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlin_version" // See https://kotlinlang.org/docs/reference/compiler-plugins.html#kotlin-spring-compiler-plugin
classpath "org.springframework.boot:spring-boot-gradle-plugin:$spring_boot_version"
}
}
sourceSets {
main.java.srcDirs += 'src/main/kotlin/'
test.java.srcDirs += 'src/test/kotlin/'
}
jar {
zip64 true
manifest {
attributes 'Main-Class': 'app.main.ApplicationKt'
}
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}
sourceCompatibility = 1.8
repositories {
mavenCentral()
jcenter()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"
compile("org.jetbrains.kotlin:kotlin-reflect")
compile group: 'com.typesafe', name: 'config', version: '1.3.2'
compile 'org.springframework.boot:spring-boot-starter-web'
compile group: 'org.apache.camel', name: 'camel-spring-boot-starter', version: '2.20.2'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-redis', version: '2.0.0.RELEASE'
compile group: 'org.apache.camel', name: 'camel-quartz2', version: '2.20.2'
compile group: 'org.apache.camel', name: 'camel-http4', version: '2.20.2'
compile group: 'org.apache.camel', name: 'camel-docker', version: '2.20.2'
compile group: 'org.apache.camel', name: 'camel-aws', version: '2.20.2'
compile group: 'com.jayway.jsonpath', name: 'json-path', version: '2.4.0'
compile group: 'redis.clients', name: 'jedis', version: '2.9.0'
compile group: 'joda-time', name: 'joda-time', version: '2.9.9'
compile group: 'net.gpedro.integrations.slack', name: 'slack-webhook', version: '1.4.0'
compile group: 'org.json', name: 'json', version: '20180130'
compile group: 'org.jfree', name: 'jfreechart', version: '1.5.0'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
If I run the project using gradle command then it runs fine. But when I build the jar and try ti run it complains with the below error:-
Error: Could not find or load main class app.main.ApplicationKt
Application.kt:-
#SpringBootApplication
#ImportResource("classpath*:camel-context.xml")
#ComponentScan("app")
class Application
fun main(args:Array<String>){
SpringApplication.run(Application::class.java, *args)
}
Not sure where exactly I'm doing anything wrong.
In Application.kt, you do not have a package declaration. From the documentation:
All the contents (such as classes and functions) of the source file are contained by the package declared. So, in the example above, the full name of baz() is foo.bar.baz, and the full name of Goo is foo.bar.Goo.
If the package is not specified, the contents of such a file belong to "default" package that has no name.
You can look into the build/classes directory to inspect what packages the classes were compiled into.
To fix this issue, add package app.main to the top of the Application.kt file.
for some reason I encountered the same error but my package was declared. I copied the package deleted the line and pasted, saved, reloaded the project and the application ran. I donĀ“t have a logical explanation for this, but that is how I solved this error.

WebApplicationInitializer is not launched on jetty+selenium

i am trying to run selenium test on jetty using gradle and my gradle configuration is as follows:
gradle.build:
apply plugin: 'java'
apply plugin: 'eclipse-wtp'
apply plugin: 'war'
apply plugin: 'findbugs'
//apply from:'http://github.com/breskeby/gradleplugins/raw/master/emmaPlugin/emma.gradle'
apply from: 'emma.gradle'
apply plugin: 'jetty'
sourceCompatibility = 1.7
version = ''
sourceSets {
selenium
}
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.gradle.api.plugins:gradle-cargo-plugin:0.6'
}
}
repositories {
mavenCentral()
mavenRepo url: 'http://repository.primefaces.org'
mavenRepo url: 'http://repository.jboss.org/nexus/content/groups/public'
mavenRepo url: 'http://repository.jboss.org/maven2'
mavenRepo url: 'http://maven.springframework.org/release'
mavenRepo url: 'http://repo1.maven.org/maven2'
mavenRepo url: 'http://git.solutionstream.com/nexus/content/repositories/thirdparty'
}
dependencies {
//JSF
compile group: 'com.sun.faces', name: 'jsf-api', version: '2.1.22'
compile group: 'com.sun.faces', name: 'jsf-impl', version: '2.1.22'
compile 'org.ocpsoft.rewrite:rewrite-servlet:2.0.3.Final'
compile 'org.ocpsoft.rewrite:rewrite-config-prettyfaces:2.0.3.Final'
compile 'javax.el:el-api:2.2'
runtime 'org.glassfish.web:el-impl:2.2'
//Servlet
compile group: 'javax.servlet', name: 'jstl', version: '1.2'
providedCompile group: 'org.jboss.spec', name: 'jboss-javaee-6.0', version: '1.0.0.Final'
compile 'taglibs:standard:1.1.2'
compile group: 'org.springframework', name: 'spring-web', version: '3.2.2.RELEASE'
//Omnifaces
compile 'org.omnifaces:omnifaces:1.5'
//Prime Faces
compile group: 'org.primefaces', name: 'primefaces', version: '4.0-SNAPSHOT'
compile 'org.primefaces.themes:bootstrap:1.0.10'
// DB
compile group: 'org.springframework.data', name: 'spring-data-jpa', version: '1.3.1.RELEASE'
compile group: 'org.springframework', name: 'spring-aspects', version: '3.2.2.RELEASE'
compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.9'
compile group: 'javax.inject', name: 'javax.inject', version: '1'
compile group: 'javax.enterprise', name: 'cdi-api', version: '1.0-SP4'
compile 'cglib:cglib-nodep:2.2.2'
//Hibernate / JPA
compile 'org.hibernate:hibernate-core:4.1.0.Final'
compile 'org.hibernate:hibernate-entitymanager:4.1.0.Final'
compile 'org.hibernate.javax.persistence:hibernate-jpa-2.0-api:1.0.1.Final'
//JSR-303
compile 'org.hibernate:hibernate-validator:4.3.1.Final'
// Spring Security
compile 'org.springframework.security:spring-security-core:3.1.4.RELEASE'
compile 'org.springframework.security:spring-security-web:3.1.4.RELEASE'
compile 'org.springframework.security:spring-security-config:3.1.4.RELEASE'
//Utility
compile 'com.google.guava:guava:14.0.1'
compile 'commons-lang:commons-lang:2.6'
compile 'org.apache.commons:commons-email:1.3.1'
compile 'com.typesafe:config:1.0.0'
compile 'joda-time:joda-time:2.2'
compile 'org.apache.geronimo.javamail:geronimo-javamail_1.4_mail:1.8.3'
compile 'org.slf4j:slf4j-api:1.7.2'
compile 'org.slf4j:jcl-over-slf4j:1.7.2'
compile 'org.slf4j:slf4j-log4j12:1.7.2'
//Mustache Templates
compile 'com.github.jknack:handlebars:1.0.0'
//Projects
//compile project(":ExtraValidators")
////TESTING DEPENDENCIES
testCompile 'com.googlecode.jmockit:jmockit:1.2'
testCompile group: 'junit', name: 'junit', version: '4.11'
testCompile 'com.h2database:h2:1.3.172'
//Spring Testing
testCompile 'org.springframework:spring-test:3.2.3.RELEASE'
/* Selenium */
seleniumCompile 'org.seleniumhq.selenium:selenium-java:2.33.0'
seleniumCompile 'junit:junit:4.11'
}
task wrapper(type: Wrapper){
gradleVersion = '1.5'
}
eclipse {
classpath {
downloadSources=true
plusConfigurations += configurations.seleniumCompile
}
}
task jettyDaemon(type: org.gradle.api.plugins.jetty.JettyRun) {
daemon = true
}
task selenium(type: Test, dependsOn: jettyDaemon) {
testClassesDir = sourceSets.selenium.output.classesDir
classpath = sourceSets.selenium.runtimeClasspath
}
WebApplicationInitializer:
package com.myapp.web.config;
import javax.faces.application.ProjectStage;
import javax.servlet.FilterRegistration;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.request.RequestContextListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.filter.DelegatingFilterProxy;
import com.myapp.data.config.SpringConfig;
import com.myapp.data.config.SpringJNDIDataConfig;
import com.myapp.data.config.SpringJNDIJPAConfig;
import com.myapp.data.config.SpringSecurityConfig;
import com.myapp.data.config.SpringWebConfig;
import com.myapp.utils.configuration.ConfigurationUtil;
public class WebappConfig implements WebApplicationInitializer {
protected final Logger logger = LoggerFactory.getLogger(getClass());
#Override
public void onStartup(final ServletContext servletContext) throws ServletException {
if(logger.isDebugEnabled()) {
logger.debug("Starting web context configuration");
}
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
// rootContext.getEnvironment().setActiveProfiles("production");
rootContext.register(SpringConfig.class, SpringSecurityConfig.class, SpringWebConfig.class, SpringJNDIDataConfig.class, SpringJNDIJPAConfig.class);
servletContext.addListener(RequestContextListener.class);
new ContextLoader(rootContext).initWebApplicationContext(servletContext);
addFilters(servletContext, rootContext.getEnvironment());
servletContext.setInitParameter(ProjectStage.PROJECT_STAGE_PARAM_NAME, ConfigurationUtil.config().getString("jsf.stage"));
servletContext.setInitParameter("javax.faces.FACELETS_REFRESH_PERIOD", ConfigurationUtil.config().getString("jsf.refreshPeriod"));
}
private void addFilters(final ServletContext servletContext, final ConfigurableEnvironment configurableEnvironment) {
FilterRegistration.Dynamic securityFilter = servletContext.addFilter("springSecurityFilterChain", new DelegatingFilterProxy("springSecurityFilterChain"));
securityFilter.addMappingForUrlPatterns(null, false, "/*");
}
}
please advise how to make WebApplicationInitializer runs when running the selenium test on jetty.
WebApplicationInitializer requires a Servlet 3 container. However, Gradle's Jetty plugin is based on Jetty 6 which supports only Servlet 2.5.
You have a few options:
Configure your application with the web.xml file instead of WebApplicationInitializer
Use a newer Jetty version with Gradle. There are some workarounds in the comments of GRADLE-1956
Use an alternative container like Tomcat (gradle-tomcat-plugin)
Use an alternative plugin like gradle-cargo-plugin or arquillian-gradle-plugin. AFAIK there's a new gradle deployment plugin in the works that should be base on Arquillian.

Resources