Cucumber Java with Selenium - Runnable Method is missing - java

I'm facing the next issue working with Cucumber in Maven project.
This is because I don't have a Runnable method for do this.
I was looking for information about, but it has been hard for me.
java.lang.Exception: No runnable methods
at org.junit.runners.BlockJUnit4ClassRunner.validateInstanceMethods(BlockJUnit4ClassRunner.java:191)
at org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(BlockJUnit4ClassRunner.java:128)
at org.junit.runners.ParentRunner.validate(ParentRunner.java:416)
at org.junit.runners.ParentRunner.<init>(ParentRunner.java:84)
at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:65)
at org.junit.runners.parameterized.BlockJUnit4ClassRunnerWithParameters.<init>(BlockJUnit4ClassRunnerWithParameters.java:27)
at org.junit.runners.parameterized.BlockJUnit4ClassRunnerWithParametersFactory.createRunnerForTestWithParameters(BlockJUnit4ClassRunnerWithParametersFactory.java:16)
at org.junit.runners.Parameterized.createRunnersForParameters(Parameterized.java:313)
at org.junit.runners.Parameterized.<init>(Parameterized.java:248)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:49)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
This is my Runner Cucumber Class:
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;
#RunWith(Cucumber.class)
#CucumberOptions(features={"src/features/Login.feature"}
,format = {"pretty", "html:target/cucumber"}
,glue = {"cucumber.CucumberStepDefs"}
)
/*#Suite.SuiteClasses({
CucumberStepDefs.class,
})*/
public class CucumberRunnerTest {
}
This is my Selenium/cucumber class:
import cl.chipacorp.test.SolutAutomatedWebTest;
import cl.chipacorp.util.data.SetDatos;
import cl.chipacorp.util.report.Report;
import cucumber.annotation.en.Given;
import cucumber.annotation.en.Then;
import cucumber.annotation.en.When;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.openqa.selenium.By;
import org.openqa.selenium.ie.InternetExplorerDriver;
import java.util.concurrent.TimeUnit;
//JAVA
#RunWith(value = Parameterized.class)
public class CucumberStepDefs extends CoopeuchAutomatedWebTest {
#Parameterized.Parameter(value = 0)
public String usuario;
#Parameterized.Parameter(value = 1)
public String pass;
#Parameterized.Parameters
public static Object[] data() {
return SetDatos.getInstance().getData(CucumberStepDefs.class);
}
#Given("^Ingresar a la URL de Branch en ambiente QA$")
public void Login() throws Exception {
driver = new InternetExplorerDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
baseUrl = "http://brs1coreqa-01.coopeuch1.cl/";
driver.get(baseUrl + "IBSBranchnet_COOPEUCH_Q5A/WebTeller.htm");
driver.switchTo ().defaultContent ();
driver.switchTo ().frame ("frmbody");
}
#When("^Se desplieguen los campos Username y Password, Ingresar las credenciales correspondientes$")
public void UsuarioPass () throws Exception {
driver.findElement(By.id("TxbTELLUSERID")).clear();
driver.findElement(By.id("TxbTELLUSERID")).sendKeys(usuario);
driver.findElement(By.id("TxbUSERPASSW")).clear();
driver.findElement(By.id("TxbUSERPASSW")).sendKeys(pass);
screenshot.take (this, driver, "LoginR C01");
driver.findElement(By.id("BtnSubmit")).click();
}
#Then("^Usuario accede a Branch con su correspondiente usuario$")
public void Logout () throws Exception {
driver.switchTo ().defaultContent ();
driver.switchTo ().frame ("frmwebteller");
driver.manage().window().maximize();
screenshot.take (this, driver, "LoginR C02");
driver.switchTo ().defaultContent ();
driver.switchTo ().frame ("frmbody").switchTo ().frame ("menu"); //main - suboption0001
driver.findElement(By.cssSelector("b")).click();
screenshot.take (this, driver, "LoginR C03");
driver.findElement(By.linkText("Log Off")).click();
screenshot.take (this, driver, "LoginR C04");
//REPORTE NO BORRAR.
Report.getInstance ().createReport (this, screenshot.getImages (),usuario);
driver.quit();
}
}
By other hand, these are my POM dependencies:
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>${selenium.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
<version>${selenium.version}</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-remote-driver</artifactId>
<version>${selenium.version}</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-firefox-driver</artifactId>
<version>${selenium.version}</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-server</artifactId>
<version>${selenium.version}</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-ie-driver</artifactId>
<version>${selenium.version}</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-edge-driver</artifactId>
<version>${selenium.version}</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-opera-driver</artifactId>
<version>${selenium.version}</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-safari-driver</artifactId>
<version>${selenium.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>${apache.poi.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${apache.poi.version}</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>${testng.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>${appium.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>${cucumber.version}</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>${cucumber.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>gherkin</artifactId>
<version>${gherkin.version}</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.0.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.2.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-core</artifactId>
<version>1.2.5</version>
<scope>test</scope>
</dependency>
I have look for help, but I'm new in Cucumber.
Could you please help me with this?

The problem is that you're not using the Cucumber runner anymore. The Parameterized runner runs your test with a standard BlockJunit4ClassRunner for each set of parameters. Therefore it discovers test by looking for method with the #Test annotation which you don't have anymore.
What you want to do is using the Parameterized runner but let it internally use the Cucumber runner. Since JUnit 4.12 this is possible:
#RunWith(Parameterized.class)
#UseParametersRunnerFactory(CucumberRunnerFactory.class)
public class CucumberStepDefs extends CoopeuchAutomatedWebTest {
public static class CucumberRunnerFactor
implements ParameterizedRunnerFactory {
public Runner createRunnerForTestWithParameters(
TestWithParameters test
) throws InitializationError {
try {
return Cucumber(test.getTestClass().getJavaClass());
} catch (IOException e) {
throw new InitializationError(e);
}
}
}
#Parameterized.Parameters
public static Object[] data() {
return SetDatos.getInstance().getData(CucumberStepDefs.class);
}
#Parameterized.Parameter(value = 0)
public String usuario;
...
}

Related

Mysql client NoSuchMethodError

All of a sudden I receive this message when I try to deploy my Vertx application:
GRAVE: Unhandled exception java.lang.NoSuchMethodError:
'void io.vertx.sqlclient.impl.SocketConnectionBase.(io.vertx.core.impl.NetSocketInternal,
boolean, int, int, int, io.vertx.core.Context)' at
io.vertx.mysqlclient.impl.MySQLSocketConnection.(MySQLSocketConnection.java:46)
at
io.vertx.mysqlclient.impl.MySQLConnectionFactory.lambda$connect$0(MySQLConnectionFactory.java:115)
at io.vertx.core.impl.FutureImpl.tryComplete(FutureImpl.java:131)
Here's the dependencies of my pom file:
<dependencies>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-auth-jwt</artifactId>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-junit5</artifactId>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-core</artifactId>
<version>4.0.0-milestone3</version>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-mysql-client</artifactId>
<version>4.0.0-milestone3</version>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web</artifactId>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web-templ-freemarker</artifactId>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-service-proxy</artifactId>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-codegen</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
And this is the code that deploys the HTTP server and the database:
#Override
public void start(Promise<Void> promise) throws Exception {
// Deploy the database
Promise<String> dbVerticleDeployment = Promise.promise();
vertx.deployVerticle(new DatabaseVerticle(), dbVerticleDeployment);
// When the database has been deployed, proceed with the server
dbVerticleDeployment.future().compose(s -> {
// Deploy the HTTPS server
Promise<String> httpVerticleDeployment = Promise.promise();
vertx.deployVerticle(new HttpsServerVerticle(), httpVerticleDeployment);
// Return the future to notify the completion
return httpVerticleDeployment.future();
}).setHandler(stringAsyncResult -> {
// Error handling
if (stringAsyncResult.succeeded())
promise.complete();
else
promise.fail(stringAsyncResult.cause());
});
}
I don't think that the error is on the code but it's something related to maven imports. I probably have messed up versions?
Here's the database
package com.rosanna.mkscoreboards.database;
import com.rosanna.mkscoreboards.database.service.DatabaseService;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Promise;
import io.vertx.mysqlclient.MySQLConnectOptions;
import io.vertx.mysqlclient.MySQLPool;
import io.vertx.serviceproxy.ServiceBinder;
import io.vertx.sqlclient.PoolOptions;
import java.util.HashMap;
public class DatabaseVerticle extends AbstractVerticle {
// Database connection constants
private static final String HOST_NAME = "localhost";
private static final String DATABASE_NAME = "mkscoreboards";
private static final String DB_USERNAME = "root";
private static final String DB_PASSWORD = "temp_pass";
private static final int MAX_POOL_SIZE = 20;
// Queries
private static HashMap<SqlQuery, String> queries = new HashMap<>();
// Event but identifier
public static final String MKSCOREBOARDS_QUEUE = "mkscoreboards.queue";
#Override
public void start(Promise<Void> promise) throws Exception {
// Connection setup
var connectOptions = new MySQLConnectOptions()
.setPort(3306)
.setHost(HOST_NAME)
.setDatabase(DATABASE_NAME)
.setUser(DB_USERNAME)
.setPassword(DB_PASSWORD);
var poolOptions = new PoolOptions().setMaxSize(MAX_POOL_SIZE);
// Load the queries
initQueries();
// Create the pooled client
var client = MySQLPool.pool(vertx, connectOptions, poolOptions);
DatabaseService.create(client, queries, ready -> {
if (ready.succeeded()) {
var binder = new ServiceBinder(vertx);
binder.setAddress(MKSCOREBOARDS_QUEUE).register(DatabaseService.class, ready.result());
promise.complete();
} else {
promise.fail(ready.cause());
}
});
}
private void initQueries() {
if (queries.size() == 0) {
queries.put(
SqlQuery.LIST_AVAILABLE_GAMES,
"SELECT * FROM games;"
);
}
}
}
Here's the database service, taken from "Gentle guide to Vertx application"
#ProxyGen
#VertxGen
public interface DatabaseService {
#GenIgnore
static DatabaseService create(MySQLPool dbClient, HashMap<SqlQuery, String> sqlQueries, Handler<AsyncResult<DatabaseService>> readyHandler) {
return new DatabaseServiceImpl(sqlQueries, dbClient, readyHandler);
}
#GenIgnore
static DatabaseService createProxy(Vertx vertx, String address) {
return new DatabaseServiceVertxEBProxy(vertx, address);
}
#Fluent
DatabaseService listAllGames(Handler<AsyncResult<JsonArray>> resultHandler);
}
and
public class DatabaseServiceImpl implements DatabaseService {
private final HashMap<SqlQuery, String> sqlQueries;
private final MySQLPool client;
public DatabaseServiceImpl(HashMap<SqlQuery, String> sqlQueries, MySQLPool client, Handler<AsyncResult<DatabaseService>> readyHandler) {
this.sqlQueries = sqlQueries;
this.client = client;
client.getConnection(result -> {
if (result.failed()) {
readyHandler.handle(Future.failedFuture(result.cause()));
} else {
readyHandler.handle(Future.succeededFuture(this));
}
});
}
#Override
public DatabaseService listAllGames(Handler<AsyncResult<JsonArray>> resultHandler) {
//TODO
return null;
}
}
I have ended up with the fact that this error is related to a bug in vert.x; I have now this error:
java.lang.NoClassDefFoundError: io/vertx/core/impl/NetSocketInternal
at
io.vertx.mysqlclient.impl.MySQLConnectionFactory.lambda$connect$0(MySQLConnectionFactory.java:114)
And the pom is now the following (compare it with the pom in the question for comparing).
<dependencies>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-auth-jwt</artifactId>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-junit5</artifactId>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-core</artifactId>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-mysql-client</artifactId>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web</artifactId>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web-templ-freemarker</artifactId>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-service-proxy</artifactId>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-codegen</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
There must be issues with vertx itself because all versions are the same, I am using vertx 4
use
<!-- https://mvnrepository.com/artifact/io.vertx/vertx-mysql-client -->
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-mysql-client</artifactId>
<version>4.1.2</version>
</dependency>
or the latest
can you check that the various versions of the vertx components you are using are the same ?

java.lang.NoSuchMethodError: org.json.JSONObject.<init>(Ljava/lang/Object;)V

I have a basic SpringBoot 2.1.5.RELEASE app. Using Spring Initializer, JPA, embedded Tomcat, Thymeleaf template engine, and package as an executable JAR file with some RestControllers.
In 1 of the controller this is the body I send:
{
"depositHotel": "xxx",
"destinationHotel": "aaa",
"depositHotelAmount": "0.2",
"destinationHotelAmount": "4",
"destinationAddress": [{
"address": "asdf",
"tag": ""
}],
"refundAddress": [{
"address": "pio",
"tag": ""
}]
}
so I create this class to send it as a RequestBody:
#JsonInclude(JsonInclude.Include.NON_NULL)
#JsonPropertyOrder({
"address",
"tag"
})
public class Address {
public Address() {
super();
}
public Address(String address) {
super();
this.address = address;
}
#JsonProperty("address")
private String address;
#JsonProperty("tag")
private Object tag;
#JsonProperty("address")
public String getAddress() {
return address;
}
#JsonProperty("address")
public void setAddress(String address) {
this.address = address;
}
#JsonProperty("tag")
public Object getTag() {
return tag;
}
#JsonProperty("tag")
public void setTag(Object tag) {
this.tag = tag;
}
}
and
public class HotelswitchHotelOrderRequestBody {
public static class Builder {
private String depositHotel;
private String destinationHotel;
private Float depositHotelAmount;
private Float destinationHotelAmount;
private Address destinationAddress;
private Address refundAddress;
public Builder(String depositHotel, String destinationHotel) {
this.depositHotel = depositHotel;
this.destinationHotel = destinationHotel;
}
public Builder withDepositHotelAmount (Float depositHotelAmount) {
this.depositHotelAmount = depositHotelAmount;
return this;
}
public Builder withDestinationHotelAmount (Float destinationHotelAmount) {
this.destinationHotelAmount = destinationHotelAmount;
return this;
}
public Builder toDestinationAddress (Address destinationAddress) {
this.destinationAddress = destinationAddress;
return this;
}
public Builder toRefundAddress (Address refundAddress) {
this.refundAddress = refundAddress;
return this;
}
public HotelswitchHotelOrderRequestBody build(){
HotelswitchHotelOrderRequestBody order = new HotelswitchHotelOrderRequestBody();
order.depositHotel = this.depositHotel;
order.depositHotelAmount = this.depositHotelAmount;
order.destinationAddress = this.destinationAddress;
order.destinationHotel = this.destinationHotel;
order.destinationHotelAmount = this.destinationHotelAmount;
order.refundAddress = this.refundAddress;
return order;
}
}
private String depositHotel;
private String destinationHotel;
private Float depositHotelAmount;
private Float destinationHotelAmount;
private Address destinationAddress;
private Address refundAddress;
private HotelswitchHotelOrderRequestBody () {
//Constructor is now private.
}
public String getDepositHotel() {
return depositHotel;
}
public void setDepositHotel(String depositHotel) {
this.depositHotel = depositHotel;
}
public String getDestinationHotel() {
return destinationHotel;
}
public void setDestinationHotel(String destinationHotel) {
this.destinationHotel = destinationHotel;
}
public Float getDepositHotelAmount() {
return depositHotelAmount;
}
public void setDepositHotelAmount(Float depositHotelAmount) {
this.depositHotelAmount = depositHotelAmount;
}
public Float getDestinationHotelAmount() {
return destinationHotelAmount;
}
public void setDestinationHotelAmount(Float destinationHotelAmount) {
this.destinationHotelAmount = destinationHotelAmount;
}
public Address getDestinationAddress() {
return destinationAddress;
}
public void setDestinationAddress(Address destinationAddress) {
this.destinationAddress = destinationAddress;
}
public Address getRefundAddress() {
return refundAddress;
}
public void setRefundAddress(Address refundAddress) {
this.refundAddress = refundAddress;
}
}
and
public test postOrder ( HotelswitchHotelOrderRequestBody order) {
HttpEntity<String> entity = new HttpEntity<String>(new JSONObject(order).toString(), headers());
ResponseEntity<OrderResponse> response = new RestTemplate()
.exchange(URL,
HttpMethod.POST, entity, new ParameterizedTypeReference<OrderResponse>() {});
return response.getBody();
}
But i have this error:
java.lang.NoSuchMethodError: org.json.JSONObject.<init>(Ljava/lang/Object;)V
at io.bonanza.backend.service.Hotelswitch.HotelswitchHotelService.postOrder(HotelswitchHotelService.java:132)
at io.bonanza.backend.service.Hotelswitch.HotelswitchHotelServiceTests.testPostOrder(HotelswitchHotelServiceTests.java:151)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
pom.xml:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.googlecode.libphonenumber</groupId>
<artifactId>libphonenumber</artifactId>
<version>8.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<!-- Spring Security -->
<!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-test -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<!-- <version>4.5.4</version> -->
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.ws.rs/javax.ws.rs-api -->
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.1.1</version>
</dependency>
<!-- Firebase dependencies -->
<dependency>
<groupId>com.google.firebase</groupId>
<artifactId>firebase-admin</artifactId>
<version>5.4.0</version>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-firestore</artifactId>
<version>0.26.0-beta</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>23.0</version>
</dependency>
</dependencies>
It looks like you have more than one org.json:json dependency on your classpath.
Looking at it:
org.springframework.boot:spring-boot-starter-test depends on
com.jayway.jsonpath:json-path which in turn brings
org.json:json which is may be newer/older than the version on which
io.jsonwebtoken jjwt 0.9.1 is dependent on
You could try excluding this transitive dependency from spring-boot-starter-test/io.jsonwebtoken:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
</exclusion>
</exclusions>
</dependency>
OR/AND
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.1</version>
<exclusions>
<exclusion>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
</exclusion>
</exclusions>
</dependency>
But it's possible that there is something within json-path which depends on something from the older json library, which is no longer in the newer version, so proceed with caution and test everything thoroughly.
There is also a chance that something else brings org.json:json.
To verify, please run mvn dependency:tree and search in the produced output for org.json:json.
Please check this link :
spring-boot issue 8706
Exclusion of android-json from spring-boot-starter-test worked for me.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>com.vaadin.external.google</groupId>
<artifactId>android-json</artifactId>
</exclusion>
</exclusions>
</dependency>
I found this on another page. I tried the checked answer here and it didn't work with mine, but when I inserted this into my pom file it worked. I'm running unit tests with mockito and one of the endpoints uses JSONObject.put and was giving me the no such method error.
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-test
</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
</exclusion>
<exclusion>
<groupId>com.vaadin.external.google</groupId>
<artifactId>android-json</artifactId>
</exclusion>
</exclusions>
</dependency>
Your destinationAddress and refundAddress are passed as list of address or address array in the JSON while in the HotelswitchHotelOrderRequestBody your destinationAddress and refundAddress are refered to as single Address Element. Update your pojo or JSON and try again. This might be an issue.
You need to convert your POJO to JSONObject using ObjectMapper or include json dependency
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180130</version>
</dependency>
The issue should be the destinationAddress and refundAddress is mismatch between your JSON and POJO.
Try to change your JSON like this:
{
"depositHotel": "xxx",
"destinationHotel": "aaa",
"depositHotelAmount": "0.2",
"destinationHotelAmount": "4",
"destinationAddress": {
"address": "asdf",
"tag": ""
},
"refundAddress": {
"address": "pio",
"tag": ""
}
}
If you dont want to change your JSON, change your Builder POJO to use Address[] instead of Address object.
You should not need the JSONobject
HttpEntity<HotelswitchHotelOrderRequestBody> entity = new HttpEntity<HotelswitchHotelOrderRequestBody>(order, headers());
if all you want to do is to convert your object to a string then use an object mapper.
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(car)

Error: cannot access org.openqa.selenium.HasInputDevices, class file not found

I run:
mvn clean test
and I get this error:
[ERROR] Failed to execute goal
org.apache.maven.plugins:maven-compiler-plugin:3.2:testCompile
(default-testCompile) on project suman: Compilation failure [ERROR]
/home/oleg/WebstormProjects/oresoftware/sumanjs/sce-test/suman/src/test/java/suman/SeleniumTest.java:[21,15]
cannot access org.openqa.selenium.HasInputDevices [ERROR] class file
for org.openqa.selenium.HasInputDevices not found
I am looking at this:
https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/interactions/HasInputDevices.html
Looks like my FirefoxDriver or ChromeDriver is causing the issue.
Here is my pom.xml file:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>suman</groupId>
<artifactId>suman</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>suman</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>htmlunit-driver</artifactId>
<version>2.29.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.21.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
<version>2.0rc3</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-firefox-driver</artifactId>
<version>2.0rc3</version>
</dependency>
</dependencies>
</project>
I have two test scripts, one uses ChromeDriver, the other uses FirefoxDriver, they look like so:
package suman;
import java.util.concurrent.TimeUnit;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.*;
import org.openqa.selenium.interactions.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class SeleniumTest {
private static FirefoxDriver driver;
private WebElement element;
#BeforeClass
public static void openBrowser(){
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
#Test
public void valid_UserCredential(){
System.out.println("Starting test " + new Object(){}.getClass().getEnclosingMethod().getName());
driver.get("http://www.store.demoqa.com");
driver.findElement(By.xpath(".//*[#id='account']/a")).click();
driver.findElement(By.id("log")).sendKeys("testuser_3");
driver.findElement(By.id("pwd")).sendKeys("Test#123");
driver.findElement(By.id("login")).click();
try{
element = driver.findElement (By.xpath(".//*[#id='account_logout']/a"));
}catch (Exception e){
}
Assert.assertNotNull(element);
System.out.println("Ending test " + new Object(){}.getClass().getEnclosingMethod().getName());
}
#Test
public void inValid_UserCredential()
{
System.out.println("Starting test " + new Object(){}.getClass().getEnclosingMethod().getName());
driver.get("http://www.store.demoqa.com");
driver.findElement(By.xpath(".//*[#id='account']/a")).click();
driver.findElement(By.id("log")).sendKeys("testuser");
driver.findElement(By.id("pwd")).sendKeys("Test#123");
driver.findElement(By.id("login")).click();
try{
element = driver.findElement (By.xpath(".//*[#id='account_logout']/a"));
}catch (Exception e){
}
Assert.assertNotNull(element);
System.out.println("Ending test " + new Object(){}.getClass().getEnclosingMethod().getName());
}
#AfterClass
public static void closeBrowser(){
driver.quit();
}
}
Does anyone know how I can fix this compilation error?
You are getting this error because dependencies in your pom.xml are not compatible.
Dependency list should be like this.
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
<version>3.9.1</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.9.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-firefox-driver</artifactId>
<version>3.9.1</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-server</artifactId>
<version>3.9.1</version>
</dependency>
</dependencies>

Mocking static method call with Powermock

I need to write a unit test case for a method, present in Utilities.java, which does
client = ABCClient.Factory.newInstance(connectionProperties);
Here is what I did in my test case
#PrepareForTest({ABCClient.class,ABCClient.Factory.class})
#Test
public void testGetABCClient() throws Exception
{
PowerMockito.mockStatic(ABCClient.class);
PowerMockito.mockStatic(ABCClient.Factory.class);
PowerMockito.when(ABCClient.Factory.newInstance(any(Properties.class)))
.thenThrow(new RuntimeException());
try
{
Properties p = new Properties();
idpsUtilities.getABCClient(p);
}
catch (Exception ex)
{
throw ex;
}
}
I get this error
org.powermock.api.mockito.ClassNotPreparedException:
[Ljava.lang.Object;#448ff1a8 The class ABCClient not prepared for
test.
What did I do wrong?
Here is my pom
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Edgware.RELEASE</spring-cloud.version>
<spring-boot.version>1.5.9.RELEASE</spring-boot.version>
<packaging.type>jar</packaging.type>
<hystrix.version>1.4.0.RELEASE</hystrix.version>
<powermock.version>1.7.1</powermock.version>
</properties>
<dependencies>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-testng</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.7.5</version>
<scope>test</scope>
</dependency>
</dependencies>

Spark : java.lang.NoClassDefFoundError: com/mongodb/hadoop/MongoInputFormat

I'm trying to read data from mongodb with spark using mongo-hadoop connector.
I tried different versions of hadoop-mongo connector jar but still getting this error.
There's no error during the compile time
What can i do to resolve this??
Thanks in advance.
Exception in thread "main" java.lang.NoClassDefFoundError: com/mongodb/hadoop/MongoInputFormat
at com.geekcap.javaworld.wordcount.Mongo.main(Mongo.java:47)
Caused by: java.lang.ClassNotFoundException: com.mongodb.hadoop.MongoInputFormat
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
My Code
import com.mongodb.hadoop.BSONFileOutputFormat;
import com.mongodb.hadoop.MongoInputFormat;
import com.mongodb.hadoop.MongoOutputFormat;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Queue;
import org.apache.hadoop.conf.Configuration;
import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.FlatMapFunction;
import org.bson.BSONObject;
public class MongoTest {
// Set configuration options for the MongoDB Hadoop Connector.
public static void main(String[] args) {
SparkConf conf = new SparkConf().setMaster("local").setAppName("App1");
JavaSparkContext sc = new JavaSparkContext(conf);
Configuration mongodbConfig;
mongodbConfig = new Configuration();
mongodbConfig.set("mongo.job.input.format", "com.mongodb.hadoop.MongoInputFormat");
mongodbConfig.set("mongo.input.uri","mongodb://localhost:27017/MyCollectionName.collection");
JavaPairRDD<Object, BSONObject> documents = sc.newAPIHadoopRDD(
mongodbConfig, // Configuration
MongoInputFormat.class, // InputFormat: read from a live cluster.
Object.class, // Key class
BSONObject.class // Value class
);
documents.saveAsTextFile("b.txt");
}
}
pom.xml dependencies:
<!-- Import Spark -->
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.11</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver</artifactId>
<version>3.0.4</version>
</dependency>
<dependency>
<groupId>hadoopCom</groupId>
<artifactId>com.sample</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>/home/sys6002/NetBeansProjects/WordCount/lib/hadoop-common-2.7.1.jar</systemPath>
</dependency>
<dependency>
<groupId>hadoopCon1</groupId>
<artifactId>com.sample1</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>/home/sys6002/Downloads/mongo-hadoop-core-1.3.0.jar</systemPath>
</dependency>
</dependencies>
After several trails & changes, got this worked.
<dependencies>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-sql_2.11</artifactId>
<version>1.5.1</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.11</artifactId>
<version>1.5.1</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
<dependency>
<groupId>org.mongodb.mongo-hadoop</groupId>
<artifactId>mongo-hadoop-core</artifactId>
<version>1.4.1</version>
</dependency>
</dependencies>
Java code
Configuration conf = new Configuration();
conf.set("mongo.job.input.format", "com.mongodb.hadoop.MongoInputFormat");
conf.set("mongo.input.uri", "mongodb://localhost:27017/databasename.collectionname");
SparkConf sconf = new SparkConf().setMaster("local").setAppName("Spark UM Jar");
JavaRDD<User> UserMaster = sc.newAPIHadoopRDD(conf, MongoInputFormat.class, Object.class, BSONObject.class)
.map(new Function<Tuple2<Object, BSONObject>, User>() {
#Override
public User call(Tuple2<Object, BSONObject> v1) throws Exception {
//return User
}
}

Categories

Resources