JUnit test for testing my API client is not working, “Error: Test Engine with ID 'junit-jupiter' failed to...












2















I am new at testing with Junit and using mockito. I have created a class for my client which is supposed to send requests to an API. I have also created a class to write a simple test. When i run the test i get errors in the console and i do not know how to solve them. I have tested the API that i am sending requests to by using Postman.
Here is my code for the Client:



import io.netty.handler.codec.http.HttpResponse;
import io.vertx.core.AsyncResult;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.json.JsonObject;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;
import io.vertx.ext.web.client.HttpRequest;
import io.vertx.ext.web.client.WebClient;

public class MyClient {

private WebClient webclient;
private String requestURL;
private static Logger logger = LoggerFactory.getLogger(MyClient.class);

public MyClient(WebClient webclient, String requestURL) {
this.webclient = webclient;
this.requestURL = requestURL;
}

public void invokeCore(JsonObject request, java.util.function.Consumer<JsonObject> func){
webclient.post(requestURL)
.putHeader("content-type", "application/json")
.sendJson(request, ar -> {
if (ar.succeeded()) {
logger.info("succeeded: " + ar.succeeded());
logger.info("statusCode: " + ar.result().statusCode());
logger.info("body: " + ar.result().body());
logger.info("headers: " + ar.result().headers());
JsonObject response = new JsonObject();
// populate it
func.accept(response);
} else {
logger.info("Executed: " + ar.cause());
}
});
}
}


This is what i have written for the test class: I set up a mock WebClient and use it when creating a MyClient instance to send a request to the API.



import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.*;
import java.awt.List;
import javax.xml.ws.Response;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import io.netty.util.concurrent.Future;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.client.HttpRequest;
import io.vertx.ext.web.client.WebClient;
import io.vertx.junit5.VertxExtension;
import io.vertx.junit5.VertxTestContext;

@ExtendWith(VertxExtension.class)
public class MyClientTest {

private MyClient client;

//set up WebClient
private WebClient createMockWebClient(JsonObject mockResponse) {
WebClient mockWebClient = mock(WebClient.class);
HttpRequest<Buffer> mockRequest = mock(HttpRequest.class);

when(mockWebClient.post(any())).thenReturn(mockRequest);
when(mockRequest.putHeader(any(), any())).thenReturn(mockRequest);
doAnswer(new Answer() {

@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
// TODO Auto-generated method stub
java.util.function.Consumer func = invocation.getArgument(1);
func.accept(mockResponse);
return null;
}

}).when(mockRequest).sendJson(any(), any());
return mockWebClient;
}

@Test
@DisplayName("Test response from client")
public void test() {
//request being sent
JsonObject request = new JsonObject().put("SSN", "123456789").put("Address", "123 main st").put("zip", "08888").put("dob", "012387");

//expected response
JsonObject response = new JsonObject().put("clientToken", "11oije311").put("clientID", "123ID");

//test setup
MyClient coreClient = new MyClient(createMockWebClient(new JsonObject()), "http://localhost:8080/core");

//test steps
coreClient.invokeCore(request, resp -> {
assertEquals(resp.getString("clientToken"), response.getString("clientToken"));
//end.finished();
});
}
}


Here is the error that appears in the console when i try to run the test:
enter image description here



And here is the pom.xml for the project:



<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>com.company.projectname</groupId>
<artifactId>myprofile</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>myprofile</name>
<description>Service to retrieve my profile</description>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<vertx.version>3.6.0</vertx.version>
<junit-jupiter.version>5.1.0</junit-jupiter.version>
<assertj-core.version>3.8.0</assertj-core.version>
<vertx.verticle>com.company.projectname.myProfileVerticle</vertx.verticle>
</properties>

<dependencies>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web</artifactId>
<version>${vertx.version}</version>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web-client</artifactId>
<version>${vertx.version}</version>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-junit5</artifactId>
<version>${vertx.version}</version>
<scope>test</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>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj-core.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.23.4</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-help-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<!-- Vert.x Maven Plugin -->
<!-- Auto re-deploy on code change: "mvn clean vertx:run" -->
<!-- Warning!!! Starts detached process, must manually kill when done. -->
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>vertx-maven-plugin</artifactId>
<version>1.0.13</version>
<executions>
<execution>
<goals>
<goal>initialize</goal>
<goal>package</goal>
</goals>
</execution>
</executions>
<configuration>
<redeploy>true</redeploy>
</configuration>
</plugin>
<!-- Fabric8 Maven Plugin -->
<!-- Deploy to openshift or kubernetes: "mvn clean fabric8:deploy" -->
<!-- Note: See src/main/fabric8/deployment.yml for deployment config -->
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>fabric8-maven-plugin</artifactId>
<version>3.5.41</version>
<executions>
<execution>
<id>fabric8</id>
<goals>
<goal>resource</goal>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

<!-- Deploy to Nexus -->
<distributionManagement>
<repository>
<id>projectnamereleases</id>
<url>http://artifactory.company.local/artifactory/projectnameReleases/</url>
</repository>
<snapshotRepository>
<id>projectnamesnapshots</id>
<url>http://artifactory.company.local/artifactory/projectnameSnapshots/</url>
</snapshotRepository>
</distributionManagement>
</project>









share|improve this question























  • I ran this in IntelJ IDEA and works fine. Maybe the issue is with how the test is executed in eclips

    – Dush
    Jan 2 at 6:04













  • It works? Well that is good news! Now, just have to get it to work for me, not sure any other way to execute it in eclipse though. Any ideas?

    – monkey123
    Jan 2 at 6:10











  • Yea it works(but test case fails as I dont have the mockResponse ). Actually I dont know how eclips executes junit test cases. But note that NoClassDefNoFoundError, the class ComparisionFailure.java is only available in Junit 4, and you are using Junit 5.

    – Dush
    Jan 2 at 6:25













  • hope this would help github.com/spring-projects/sts4/issues/71

    – Dush
    Jan 2 at 6:29











  • Thanks! That helped out. Now the test actually runs

    – monkey123
    Jan 2 at 7:08
















2















I am new at testing with Junit and using mockito. I have created a class for my client which is supposed to send requests to an API. I have also created a class to write a simple test. When i run the test i get errors in the console and i do not know how to solve them. I have tested the API that i am sending requests to by using Postman.
Here is my code for the Client:



import io.netty.handler.codec.http.HttpResponse;
import io.vertx.core.AsyncResult;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.json.JsonObject;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;
import io.vertx.ext.web.client.HttpRequest;
import io.vertx.ext.web.client.WebClient;

public class MyClient {

private WebClient webclient;
private String requestURL;
private static Logger logger = LoggerFactory.getLogger(MyClient.class);

public MyClient(WebClient webclient, String requestURL) {
this.webclient = webclient;
this.requestURL = requestURL;
}

public void invokeCore(JsonObject request, java.util.function.Consumer<JsonObject> func){
webclient.post(requestURL)
.putHeader("content-type", "application/json")
.sendJson(request, ar -> {
if (ar.succeeded()) {
logger.info("succeeded: " + ar.succeeded());
logger.info("statusCode: " + ar.result().statusCode());
logger.info("body: " + ar.result().body());
logger.info("headers: " + ar.result().headers());
JsonObject response = new JsonObject();
// populate it
func.accept(response);
} else {
logger.info("Executed: " + ar.cause());
}
});
}
}


This is what i have written for the test class: I set up a mock WebClient and use it when creating a MyClient instance to send a request to the API.



import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.*;
import java.awt.List;
import javax.xml.ws.Response;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import io.netty.util.concurrent.Future;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.client.HttpRequest;
import io.vertx.ext.web.client.WebClient;
import io.vertx.junit5.VertxExtension;
import io.vertx.junit5.VertxTestContext;

@ExtendWith(VertxExtension.class)
public class MyClientTest {

private MyClient client;

//set up WebClient
private WebClient createMockWebClient(JsonObject mockResponse) {
WebClient mockWebClient = mock(WebClient.class);
HttpRequest<Buffer> mockRequest = mock(HttpRequest.class);

when(mockWebClient.post(any())).thenReturn(mockRequest);
when(mockRequest.putHeader(any(), any())).thenReturn(mockRequest);
doAnswer(new Answer() {

@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
// TODO Auto-generated method stub
java.util.function.Consumer func = invocation.getArgument(1);
func.accept(mockResponse);
return null;
}

}).when(mockRequest).sendJson(any(), any());
return mockWebClient;
}

@Test
@DisplayName("Test response from client")
public void test() {
//request being sent
JsonObject request = new JsonObject().put("SSN", "123456789").put("Address", "123 main st").put("zip", "08888").put("dob", "012387");

//expected response
JsonObject response = new JsonObject().put("clientToken", "11oije311").put("clientID", "123ID");

//test setup
MyClient coreClient = new MyClient(createMockWebClient(new JsonObject()), "http://localhost:8080/core");

//test steps
coreClient.invokeCore(request, resp -> {
assertEquals(resp.getString("clientToken"), response.getString("clientToken"));
//end.finished();
});
}
}


Here is the error that appears in the console when i try to run the test:
enter image description here



And here is the pom.xml for the project:



<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>com.company.projectname</groupId>
<artifactId>myprofile</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>myprofile</name>
<description>Service to retrieve my profile</description>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<vertx.version>3.6.0</vertx.version>
<junit-jupiter.version>5.1.0</junit-jupiter.version>
<assertj-core.version>3.8.0</assertj-core.version>
<vertx.verticle>com.company.projectname.myProfileVerticle</vertx.verticle>
</properties>

<dependencies>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web</artifactId>
<version>${vertx.version}</version>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web-client</artifactId>
<version>${vertx.version}</version>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-junit5</artifactId>
<version>${vertx.version}</version>
<scope>test</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>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj-core.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.23.4</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-help-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<!-- Vert.x Maven Plugin -->
<!-- Auto re-deploy on code change: "mvn clean vertx:run" -->
<!-- Warning!!! Starts detached process, must manually kill when done. -->
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>vertx-maven-plugin</artifactId>
<version>1.0.13</version>
<executions>
<execution>
<goals>
<goal>initialize</goal>
<goal>package</goal>
</goals>
</execution>
</executions>
<configuration>
<redeploy>true</redeploy>
</configuration>
</plugin>
<!-- Fabric8 Maven Plugin -->
<!-- Deploy to openshift or kubernetes: "mvn clean fabric8:deploy" -->
<!-- Note: See src/main/fabric8/deployment.yml for deployment config -->
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>fabric8-maven-plugin</artifactId>
<version>3.5.41</version>
<executions>
<execution>
<id>fabric8</id>
<goals>
<goal>resource</goal>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

<!-- Deploy to Nexus -->
<distributionManagement>
<repository>
<id>projectnamereleases</id>
<url>http://artifactory.company.local/artifactory/projectnameReleases/</url>
</repository>
<snapshotRepository>
<id>projectnamesnapshots</id>
<url>http://artifactory.company.local/artifactory/projectnameSnapshots/</url>
</snapshotRepository>
</distributionManagement>
</project>









share|improve this question























  • I ran this in IntelJ IDEA and works fine. Maybe the issue is with how the test is executed in eclips

    – Dush
    Jan 2 at 6:04













  • It works? Well that is good news! Now, just have to get it to work for me, not sure any other way to execute it in eclipse though. Any ideas?

    – monkey123
    Jan 2 at 6:10











  • Yea it works(but test case fails as I dont have the mockResponse ). Actually I dont know how eclips executes junit test cases. But note that NoClassDefNoFoundError, the class ComparisionFailure.java is only available in Junit 4, and you are using Junit 5.

    – Dush
    Jan 2 at 6:25













  • hope this would help github.com/spring-projects/sts4/issues/71

    – Dush
    Jan 2 at 6:29











  • Thanks! That helped out. Now the test actually runs

    – monkey123
    Jan 2 at 7:08














2












2








2








I am new at testing with Junit and using mockito. I have created a class for my client which is supposed to send requests to an API. I have also created a class to write a simple test. When i run the test i get errors in the console and i do not know how to solve them. I have tested the API that i am sending requests to by using Postman.
Here is my code for the Client:



import io.netty.handler.codec.http.HttpResponse;
import io.vertx.core.AsyncResult;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.json.JsonObject;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;
import io.vertx.ext.web.client.HttpRequest;
import io.vertx.ext.web.client.WebClient;

public class MyClient {

private WebClient webclient;
private String requestURL;
private static Logger logger = LoggerFactory.getLogger(MyClient.class);

public MyClient(WebClient webclient, String requestURL) {
this.webclient = webclient;
this.requestURL = requestURL;
}

public void invokeCore(JsonObject request, java.util.function.Consumer<JsonObject> func){
webclient.post(requestURL)
.putHeader("content-type", "application/json")
.sendJson(request, ar -> {
if (ar.succeeded()) {
logger.info("succeeded: " + ar.succeeded());
logger.info("statusCode: " + ar.result().statusCode());
logger.info("body: " + ar.result().body());
logger.info("headers: " + ar.result().headers());
JsonObject response = new JsonObject();
// populate it
func.accept(response);
} else {
logger.info("Executed: " + ar.cause());
}
});
}
}


This is what i have written for the test class: I set up a mock WebClient and use it when creating a MyClient instance to send a request to the API.



import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.*;
import java.awt.List;
import javax.xml.ws.Response;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import io.netty.util.concurrent.Future;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.client.HttpRequest;
import io.vertx.ext.web.client.WebClient;
import io.vertx.junit5.VertxExtension;
import io.vertx.junit5.VertxTestContext;

@ExtendWith(VertxExtension.class)
public class MyClientTest {

private MyClient client;

//set up WebClient
private WebClient createMockWebClient(JsonObject mockResponse) {
WebClient mockWebClient = mock(WebClient.class);
HttpRequest<Buffer> mockRequest = mock(HttpRequest.class);

when(mockWebClient.post(any())).thenReturn(mockRequest);
when(mockRequest.putHeader(any(), any())).thenReturn(mockRequest);
doAnswer(new Answer() {

@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
// TODO Auto-generated method stub
java.util.function.Consumer func = invocation.getArgument(1);
func.accept(mockResponse);
return null;
}

}).when(mockRequest).sendJson(any(), any());
return mockWebClient;
}

@Test
@DisplayName("Test response from client")
public void test() {
//request being sent
JsonObject request = new JsonObject().put("SSN", "123456789").put("Address", "123 main st").put("zip", "08888").put("dob", "012387");

//expected response
JsonObject response = new JsonObject().put("clientToken", "11oije311").put("clientID", "123ID");

//test setup
MyClient coreClient = new MyClient(createMockWebClient(new JsonObject()), "http://localhost:8080/core");

//test steps
coreClient.invokeCore(request, resp -> {
assertEquals(resp.getString("clientToken"), response.getString("clientToken"));
//end.finished();
});
}
}


Here is the error that appears in the console when i try to run the test:
enter image description here



And here is the pom.xml for the project:



<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>com.company.projectname</groupId>
<artifactId>myprofile</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>myprofile</name>
<description>Service to retrieve my profile</description>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<vertx.version>3.6.0</vertx.version>
<junit-jupiter.version>5.1.0</junit-jupiter.version>
<assertj-core.version>3.8.0</assertj-core.version>
<vertx.verticle>com.company.projectname.myProfileVerticle</vertx.verticle>
</properties>

<dependencies>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web</artifactId>
<version>${vertx.version}</version>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web-client</artifactId>
<version>${vertx.version}</version>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-junit5</artifactId>
<version>${vertx.version}</version>
<scope>test</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>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj-core.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.23.4</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-help-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<!-- Vert.x Maven Plugin -->
<!-- Auto re-deploy on code change: "mvn clean vertx:run" -->
<!-- Warning!!! Starts detached process, must manually kill when done. -->
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>vertx-maven-plugin</artifactId>
<version>1.0.13</version>
<executions>
<execution>
<goals>
<goal>initialize</goal>
<goal>package</goal>
</goals>
</execution>
</executions>
<configuration>
<redeploy>true</redeploy>
</configuration>
</plugin>
<!-- Fabric8 Maven Plugin -->
<!-- Deploy to openshift or kubernetes: "mvn clean fabric8:deploy" -->
<!-- Note: See src/main/fabric8/deployment.yml for deployment config -->
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>fabric8-maven-plugin</artifactId>
<version>3.5.41</version>
<executions>
<execution>
<id>fabric8</id>
<goals>
<goal>resource</goal>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

<!-- Deploy to Nexus -->
<distributionManagement>
<repository>
<id>projectnamereleases</id>
<url>http://artifactory.company.local/artifactory/projectnameReleases/</url>
</repository>
<snapshotRepository>
<id>projectnamesnapshots</id>
<url>http://artifactory.company.local/artifactory/projectnameSnapshots/</url>
</snapshotRepository>
</distributionManagement>
</project>









share|improve this question














I am new at testing with Junit and using mockito. I have created a class for my client which is supposed to send requests to an API. I have also created a class to write a simple test. When i run the test i get errors in the console and i do not know how to solve them. I have tested the API that i am sending requests to by using Postman.
Here is my code for the Client:



import io.netty.handler.codec.http.HttpResponse;
import io.vertx.core.AsyncResult;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.json.JsonObject;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;
import io.vertx.ext.web.client.HttpRequest;
import io.vertx.ext.web.client.WebClient;

public class MyClient {

private WebClient webclient;
private String requestURL;
private static Logger logger = LoggerFactory.getLogger(MyClient.class);

public MyClient(WebClient webclient, String requestURL) {
this.webclient = webclient;
this.requestURL = requestURL;
}

public void invokeCore(JsonObject request, java.util.function.Consumer<JsonObject> func){
webclient.post(requestURL)
.putHeader("content-type", "application/json")
.sendJson(request, ar -> {
if (ar.succeeded()) {
logger.info("succeeded: " + ar.succeeded());
logger.info("statusCode: " + ar.result().statusCode());
logger.info("body: " + ar.result().body());
logger.info("headers: " + ar.result().headers());
JsonObject response = new JsonObject();
// populate it
func.accept(response);
} else {
logger.info("Executed: " + ar.cause());
}
});
}
}


This is what i have written for the test class: I set up a mock WebClient and use it when creating a MyClient instance to send a request to the API.



import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.*;
import java.awt.List;
import javax.xml.ws.Response;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import io.netty.util.concurrent.Future;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.client.HttpRequest;
import io.vertx.ext.web.client.WebClient;
import io.vertx.junit5.VertxExtension;
import io.vertx.junit5.VertxTestContext;

@ExtendWith(VertxExtension.class)
public class MyClientTest {

private MyClient client;

//set up WebClient
private WebClient createMockWebClient(JsonObject mockResponse) {
WebClient mockWebClient = mock(WebClient.class);
HttpRequest<Buffer> mockRequest = mock(HttpRequest.class);

when(mockWebClient.post(any())).thenReturn(mockRequest);
when(mockRequest.putHeader(any(), any())).thenReturn(mockRequest);
doAnswer(new Answer() {

@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
// TODO Auto-generated method stub
java.util.function.Consumer func = invocation.getArgument(1);
func.accept(mockResponse);
return null;
}

}).when(mockRequest).sendJson(any(), any());
return mockWebClient;
}

@Test
@DisplayName("Test response from client")
public void test() {
//request being sent
JsonObject request = new JsonObject().put("SSN", "123456789").put("Address", "123 main st").put("zip", "08888").put("dob", "012387");

//expected response
JsonObject response = new JsonObject().put("clientToken", "11oije311").put("clientID", "123ID");

//test setup
MyClient coreClient = new MyClient(createMockWebClient(new JsonObject()), "http://localhost:8080/core");

//test steps
coreClient.invokeCore(request, resp -> {
assertEquals(resp.getString("clientToken"), response.getString("clientToken"));
//end.finished();
});
}
}


Here is the error that appears in the console when i try to run the test:
enter image description here



And here is the pom.xml for the project:



<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>com.company.projectname</groupId>
<artifactId>myprofile</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>myprofile</name>
<description>Service to retrieve my profile</description>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<vertx.version>3.6.0</vertx.version>
<junit-jupiter.version>5.1.0</junit-jupiter.version>
<assertj-core.version>3.8.0</assertj-core.version>
<vertx.verticle>com.company.projectname.myProfileVerticle</vertx.verticle>
</properties>

<dependencies>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web</artifactId>
<version>${vertx.version}</version>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web-client</artifactId>
<version>${vertx.version}</version>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-junit5</artifactId>
<version>${vertx.version}</version>
<scope>test</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>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj-core.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.23.4</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-help-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<!-- Vert.x Maven Plugin -->
<!-- Auto re-deploy on code change: "mvn clean vertx:run" -->
<!-- Warning!!! Starts detached process, must manually kill when done. -->
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>vertx-maven-plugin</artifactId>
<version>1.0.13</version>
<executions>
<execution>
<goals>
<goal>initialize</goal>
<goal>package</goal>
</goals>
</execution>
</executions>
<configuration>
<redeploy>true</redeploy>
</configuration>
</plugin>
<!-- Fabric8 Maven Plugin -->
<!-- Deploy to openshift or kubernetes: "mvn clean fabric8:deploy" -->
<!-- Note: See src/main/fabric8/deployment.yml for deployment config -->
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>fabric8-maven-plugin</artifactId>
<version>3.5.41</version>
<executions>
<execution>
<id>fabric8</id>
<goals>
<goal>resource</goal>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

<!-- Deploy to Nexus -->
<distributionManagement>
<repository>
<id>projectnamereleases</id>
<url>http://artifactory.company.local/artifactory/projectnameReleases/</url>
</repository>
<snapshotRepository>
<id>projectnamesnapshots</id>
<url>http://artifactory.company.local/artifactory/projectnameSnapshots/</url>
</snapshotRepository>
</distributionManagement>
</project>






java junit mockito web-api-testing vertx-httpclient






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 2 at 2:50









monkey123monkey123

263




263













  • I ran this in IntelJ IDEA and works fine. Maybe the issue is with how the test is executed in eclips

    – Dush
    Jan 2 at 6:04













  • It works? Well that is good news! Now, just have to get it to work for me, not sure any other way to execute it in eclipse though. Any ideas?

    – monkey123
    Jan 2 at 6:10











  • Yea it works(but test case fails as I dont have the mockResponse ). Actually I dont know how eclips executes junit test cases. But note that NoClassDefNoFoundError, the class ComparisionFailure.java is only available in Junit 4, and you are using Junit 5.

    – Dush
    Jan 2 at 6:25













  • hope this would help github.com/spring-projects/sts4/issues/71

    – Dush
    Jan 2 at 6:29











  • Thanks! That helped out. Now the test actually runs

    – monkey123
    Jan 2 at 7:08



















  • I ran this in IntelJ IDEA and works fine. Maybe the issue is with how the test is executed in eclips

    – Dush
    Jan 2 at 6:04













  • It works? Well that is good news! Now, just have to get it to work for me, not sure any other way to execute it in eclipse though. Any ideas?

    – monkey123
    Jan 2 at 6:10











  • Yea it works(but test case fails as I dont have the mockResponse ). Actually I dont know how eclips executes junit test cases. But note that NoClassDefNoFoundError, the class ComparisionFailure.java is only available in Junit 4, and you are using Junit 5.

    – Dush
    Jan 2 at 6:25













  • hope this would help github.com/spring-projects/sts4/issues/71

    – Dush
    Jan 2 at 6:29











  • Thanks! That helped out. Now the test actually runs

    – monkey123
    Jan 2 at 7:08

















I ran this in IntelJ IDEA and works fine. Maybe the issue is with how the test is executed in eclips

– Dush
Jan 2 at 6:04







I ran this in IntelJ IDEA and works fine. Maybe the issue is with how the test is executed in eclips

– Dush
Jan 2 at 6:04















It works? Well that is good news! Now, just have to get it to work for me, not sure any other way to execute it in eclipse though. Any ideas?

– monkey123
Jan 2 at 6:10





It works? Well that is good news! Now, just have to get it to work for me, not sure any other way to execute it in eclipse though. Any ideas?

– monkey123
Jan 2 at 6:10













Yea it works(but test case fails as I dont have the mockResponse ). Actually I dont know how eclips executes junit test cases. But note that NoClassDefNoFoundError, the class ComparisionFailure.java is only available in Junit 4, and you are using Junit 5.

– Dush
Jan 2 at 6:25







Yea it works(but test case fails as I dont have the mockResponse ). Actually I dont know how eclips executes junit test cases. But note that NoClassDefNoFoundError, the class ComparisionFailure.java is only available in Junit 4, and you are using Junit 5.

– Dush
Jan 2 at 6:25















hope this would help github.com/spring-projects/sts4/issues/71

– Dush
Jan 2 at 6:29





hope this would help github.com/spring-projects/sts4/issues/71

– Dush
Jan 2 at 6:29













Thanks! That helped out. Now the test actually runs

– monkey123
Jan 2 at 7:08





Thanks! That helped out. Now the test actually runs

– monkey123
Jan 2 at 7:08












2 Answers
2






active

oldest

votes


















1














Check if mockito supports junit5 or maybe one of mockito or junit5 versions are outdated. Today i've lost a lot of time with a newet version of mockito according to the junit4 ones. Junit5 5.3.2 is the most recent, you are using 5.1.0.
Other option is you must add another dependencie.






share|improve this answer































    0














    Could be an issue related to Eclips. Please see



    https://github.com/spring-projects/sts4/issues/71




    removed the Jupiter dependencies to use JUnit 4 and the test worked
    appropriately (gave correct failure/success)







    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54000695%2fjunit-test-for-testing-my-api-client-is-not-working-error-test-engine-with-id%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      Check if mockito supports junit5 or maybe one of mockito or junit5 versions are outdated. Today i've lost a lot of time with a newet version of mockito according to the junit4 ones. Junit5 5.3.2 is the most recent, you are using 5.1.0.
      Other option is you must add another dependencie.






      share|improve this answer




























        1














        Check if mockito supports junit5 or maybe one of mockito or junit5 versions are outdated. Today i've lost a lot of time with a newet version of mockito according to the junit4 ones. Junit5 5.3.2 is the most recent, you are using 5.1.0.
        Other option is you must add another dependencie.






        share|improve this answer


























          1












          1








          1







          Check if mockito supports junit5 or maybe one of mockito or junit5 versions are outdated. Today i've lost a lot of time with a newet version of mockito according to the junit4 ones. Junit5 5.3.2 is the most recent, you are using 5.1.0.
          Other option is you must add another dependencie.






          share|improve this answer













          Check if mockito supports junit5 or maybe one of mockito or junit5 versions are outdated. Today i've lost a lot of time with a newet version of mockito according to the junit4 ones. Junit5 5.3.2 is the most recent, you are using 5.1.0.
          Other option is you must add another dependencie.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 2 at 3:02









          lugolulugolu

          5615




          5615

























              0














              Could be an issue related to Eclips. Please see



              https://github.com/spring-projects/sts4/issues/71




              removed the Jupiter dependencies to use JUnit 4 and the test worked
              appropriately (gave correct failure/success)







              share|improve this answer




























                0














                Could be an issue related to Eclips. Please see



                https://github.com/spring-projects/sts4/issues/71




                removed the Jupiter dependencies to use JUnit 4 and the test worked
                appropriately (gave correct failure/success)







                share|improve this answer


























                  0












                  0








                  0







                  Could be an issue related to Eclips. Please see



                  https://github.com/spring-projects/sts4/issues/71




                  removed the Jupiter dependencies to use JUnit 4 and the test worked
                  appropriately (gave correct failure/success)







                  share|improve this answer













                  Could be an issue related to Eclips. Please see



                  https://github.com/spring-projects/sts4/issues/71




                  removed the Jupiter dependencies to use JUnit 4 and the test worked
                  appropriately (gave correct failure/success)








                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 2 at 7:17









                  DushDush

                  1,018613




                  1,018613






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54000695%2fjunit-test-for-testing-my-api-client-is-not-working-error-test-engine-with-id%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      'app-layout' is not a known element: how to share Component with different Modules

                      android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

                      WPF add header to Image with URL pettitions [duplicate]