Trying to build a hello world jersey test. https://jersey.java.net/documentation/2.5.1/test-framework.html makes it look so simple, but when overriding the configure method as documented doesn't work.
From the documentation
package api;
import static org.junit.Assert.assertEquals;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Application;
import org.junit.Test;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.core.ResourceConfig;
import com.sun.jersey.test.framework.JerseyTest;
import com.sun.jersey.test.framework.WebAppDescriptor;
public class JerseyTester extends JerseyTest {
#Path("hello")
public static class HelloResource {
#GET
public String getHello() {
return "Hello World!";
}
}
#Override
protected Application configure() {
return new ResourceConfig(HelloResource.class);
}
#Test
public void testHelloWorld() {
WebResource webResource = resource();
String responseMsg = webResource.path("helloworld").get(String.class);
assertEquals("Hello World", responseMsg);
}
}
The issue is the configure method override doesn't work - I get the error: "The return type is incompatible with JerseyTest.configure()". I also get the error: "Cannot instantiate the type ResourceConfig" - how can that be when the documentation explicitly says to instantiate it?!
This is so basic I don't know why it wouldn't work. I'm just trying to get a plain-jane jersey endpoint under test.
Here's my dependencies:
dependencies {
compile 'javax.ws.rs:jsr311-api:1.1.1'
compile 'com.sun.jersey:jersey-server:1.19'
compile 'com.sun.jersey:jersey-core:1.19'
compile 'com.sun.jersey:jersey-client:1.19'
compile 'com.sun.jersey:jersey-servlet:1.19'
compile 'com.sun.jersey:jersey-json:1.19'
compile 'com.yammer.metrics:metrics-core:2.2.0'
compile 'com.yammer.metrics:metrics-servlet:2.2.0'
compile 'com.yammer.metrics:metrics-jersey:2.2.0'
compile 'com.yammer.metrics:metrics-graphite:2.2.0'
compile 'log4j:log4j:1.2.16'
testCompile 'junit:junit-dep:4.10'
testCompile 'com.sun.jersey.jersey-test-framework:jersey-test-framework-grizzly2:1.19'
testCompile 'org.slf4j:slf4j-simple:1.6.1'
}
Yeah you're looking at the wrong documentation. The documentation you're looking at is for Jersey 2.x. But you are using Jersey 1.x. You could look at the documentation for 1.x, but there's not much going on there. Best thing to do is look at the source code tests for examples. You can also see another example at the bottom of this answer
Related
I'm trying to implement a custom endpoint in a Spring Boot application.
Goal is to use routes as: from("...").process("...").to("my:...");
Now, I have 3 classes: a DefaultConsumer, a DefaultEndpoint, a DefaultComponent:
package com.my.endpoint;
import org.apache.camel.Consumer;
import org.apache.camel.Processor;
import org.apache.camel.Producer;
import org.apache.camel.support.DefaultEndpoint;
public class MyEndpoint extends DefaultEndpoint {
public MyEndpoint(String uri, MyComponent myComponent) {
}
...
}
package com.my.endpoint;
import org.apache.camel.Endpoint;
import org.apache.camel.Processor;
import org.apache.camel.support.DefaultConsumer;
public class MyConsumer extends DefaultConsumer {
public MyConsumer(Endpoint endpoint, Processor processor) {
super(endpoint, processor);
}
}
package com.my.endpoint;
import org.apache.camel.CamelContext;
import org.apache.camel.Endpoint;
import org.apache.camel.spi.annotations.Component;
import org.apache.camel.support.DefaultComponent;
import java.util.Map;
#Component("my")
public class MyComponent extends DefaultComponent {
public MyComponent(CamelContext camelContext) {
super(camelContext);
}
...
}
Now: how can I register?
In a Spring configuration class, I have:
#Override
public void configure() throws Exception {
camelContext.addComponent("my", new MyComponent(camelContext));
But is not working:
Caused by: org.apache.camel.NoSuchEndpointException: No endpoint could be found for: my, please check your classpath contains the needed Camel component jar.
So, I added the META-INF file in services/org/apache/camel/component/my:
class=com.my.endpoint.MyComponent
But also this, is not working.
There is no complete tutorial on how to implement this.
Any help?
Note: I'm trying to implement an Endpoint because I need to integrate my systems using my data types. I tried using Transformer but failed because of this: Set a custom DataType in Apache Camel Processor
Before, I tried using data type converter, but failed because of this (marked duplicate because people are too lazy to really understand questions): Enforce type conversion on Rest consumer in Apache Camel
I've FULLY read "Apache Camel In Action, Second Edition" but, at the moment, I can't continue with my project because of?
This is because custom component must be annotated by #UriEndpoint annotation.
Another way to solve this problem: Set EndpointUri via Constructor or by implementing createEndpointUri() in MyEndpoint.
So easiest way might be changing your constructor to:
public MyEndpoint(String uri, MyComponent myComponent) {
super(uri, myComponent);
}
I followed the guide here to create a new Jersey HTTP server from Maven Archetype. Everything is fine, the resource returns the string as expected, here is the resource:
package com.example;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
#Path("myresource")
public class MyResource {
#GET
#Produces(MediaType.TEXT_PLAIN)
public String getIt() {
return "Hello from my Server";
}
}
I followed the guide here to use the downloaded ArcGIS Runtime SDK. I created the following simple Java class to determine whether a Point in inside a Geometry (Circle, Rectangle, Polygon, ...) or not:
package geoC;
import com.esri.arcgisruntime.geometry.Geometry;
import com.esri.arcgisruntime.geometry.GeometryEngine;
import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.geometry.SpatialReferences;
public class checkInside {
public static void main(String[] args) {
Point pt = new Point( -0.04473405456542423, 39.98776978688296, SpatialReferences.getWgs84());
String rectangleJson = "{\"xmin\":-0.05225854142896743,\"ymin\":39.98251082423102,\"xmax\":-0.02856927140946811,\"ymax\":39.993164240959295,\"spatialReference\":{\"wkid\":4326}}";
Geometry rectangle = (Geometry) Geometry.fromJson(rectangleJson);
//check if the point is inside the rectangle or not
boolean decision = GeometryEngine.contains( rectangle, pt );
System.out.println(decision);
}
}
This class works fine, producing "true" or "false" depending on the location input.
The problem arises when I tried to combine the first and the second piece of code, I put the content of the 2nd piece of code in the first piece of code:
package com.example;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import com.esri.arcgisruntime.geometry.Geometry;
import com.esri.arcgisruntime.geometry.GeometryEngine;
import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.geometry.SpatialReferences;
#Path("myresource")
public class MyResource {
#GET
#Produces(MediaType.TEXT_PLAIN)
public String getIt() {
Point pt = new Point( -0.04473405456542423, 39.98776978688296, SpatialReferences.getWgs84());
String rectangleJson = "{\"xmin\":-0.05225854142896743,\"ymin\":39.98251082423102,\"xmax\":-0.02856927140946811,\"ymax\":39.993164240959295,\"spatialReference\":{\"wkid\":4326}}";
Geometry rectangle = (Geometry) Geometry.fromJson(rectangleJson);
boolean decision = GeometryEngine.contains( rectangle, pt );
System.out.println(decision);
return "Hello from my Server";
}
}
mvn compile ---> BUILD SUCCESS
mvn exec:java ---> BUILD FAILURE
The error is: Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:java (default-cli) on project TestArcGISJersey: An exception occured while executing the Java class. null: InvocationTargetException: com/esri/arcgisruntime/geometry/Geometry: com.esri.arcgisruntime.geometry.Geometry -> [Help 1]
Furthermore, if I comment the 2 lines (boolean decision = ... + System.out.println(...) ) then mvn exec:java executes succesfully, but when I send a GET request to the resource, request failed instead of getting the expected String.
Anyone has any ideas here? Thank you very much.
Seems that you have not gotten any licence yet, remember that all ArcGIS products need to have a licence, does not matter if you pay or not.
I kindly suggest you to follow this steps from ArcGIS Developers (ArcGIS runtime for JAVA 100.0.0) "License your app"
There you can see that you need a line like this:
https://developers.arcgis.com/java/latest/guide/license-your-app.htm
// license with a license key
ArcGISRuntimeEnvironment.setLicense("runtimelite,1000,rud#########,day-month-year,####################");
I am not sure about using ArcGIS runtime on a Jersey HTTP server from Maven Archetype. you will need additional tests and always look to the server log.
ArcGIS Runtime SDK for Java is not a good candidate as an API to use in a server environment. While some APIs may work, this use case is not supported. The API was not designed for use in server containers.
However, for your use case you've described, you may want to look at the Java Geometry API open source project found here: https://github.com/Esri/geometry-api-java.
Still, having stated the reality of this, I do feel it is natural to assume it would be a viable solution for server development. So great question!
I got the message like this.
com.controller.Test1 > test FAILED
java.lang.NoSuchMethodError at Test1.java:18
The dependencies for test compiling is
testCompile "junit:junit:4.11",
'org.mockito:mockito-all:1.10.19',
'com.jayway.jsonpath:json-path:2.2.0',
'org.hamcrest:hamcrest-all:1.3',
'org.flywaydb.flyway-test-extensions:flyway-dbunit-spring4-test:4.0'
And this is my test code.
package com.controller;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;
import static org.hamcrest.Matchers.instanceOf;
import static org.junit.Assert.assertThat;
#RunWith(MockitoJUnitRunner.class)
public class Test1 {
#Test
public void test(){
assertThat(Long.valueOf(1), instanceOf(Integer.class));
}
}
I want the message like this.
Expected: an instance of java.lang.Integer
but: <1L> is a java.lang.Long
The java.lang.NoSuchMethodError is not related to JUnit specifically. Instead, it indicates that the version of a class that was on your compiler's classpath is different from the version of the class that is on your runtime classpath.
The reason for the clash is that JUnit comes with its own org.hamcrest.Matcher class that is being used instead of the one imported in your code. Use mockito-core in your imports instead of mockito-all to exclude the matcher.
This seems like it shouldn't compile and run as Object does not have a fail() method. At compile time is something funky happening? (I am using NetBeans):
import static org.junit.Assert.*;
import org.junit.Test;
public class Test {
#Test
public void hello() {
fail();
}
}
Regards,
Guido
Your import static line imports all static members of the Assert class into the static namespace of your compilation unit. The fail() call refers to Assert.fail().
The confusion you are experiencing regarding where fail() is defined is precisely why I don't usually recommend using import static. In my own code, I usually import the class and use it to invoke the static methods:
import org.junit.Assert;
import org.junit.Test;
public class Test {
#Test
public void hello() {
Assert.fail();
}
}
Much more readable.
However, as JB Nizet points out, it is fairly common practice to use import static for JUnit's assertions; when you write and read enough JUnit tests, knowing where the assertion methods come from will become second nature.
This is perfectly correct and it will run and compile - I already checked using eclipse.
The reason is the static import:
import static org.junit.Assert.*;
that adds all the static fields or methods from the org.junit.Assert class - hence including fail() method.
Nevertheless a problem that might occur is the fact that the name of your test class is the same as the name of the annotation
#Test
hence it will generate an error:
The import org.junit.Test conflicts with a type defined in the same file
This error is coming because your classname and annotation name are same(Test).Change your class name to 'Test1' or other than Test.
I'm new to java and I'm trying to compile and run a web service example from a book.
The example uses 3 files.
I can create an Eclipse Project and Run it. It works fine this way.
From the command line I tried
javac TimeServer.java TimeServerImpl.java TimeServerPublisher.java
And got no errors
This program does not run on the command line returns error:
"Could not find the main class"
java TimeServerPublisher
running using the -classpath option returns the same result.
Set classpath does not help either. ie
java -classpath . TimeServerPublisher
fails as well
Most of the online docs specify I need a classpath. I tried everything they suggested.
Please Help. Thanks in advance
Source:
TimeServer.java
package ch01.ts;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
#WebService
#SOAPBinding(style = Style.RPC) // more on this later
public interface TimeServer
{
#WebMethod String getTimeAsString();
#WebMethod long getTimeAsElapsed();
}
TimeServerImpl.java
package ch01.ts;
import java.util.Date;
import javax.jws.WebService;
#WebService(endpointInterface = "ch01.ts.TimeServer")
public class TimeServerImpl implements TimeServer
{
#Override
public String getTimeAsString()
{
return new Date().toString();
}
#Override
public long getTimeAsElapsed()
{
return new Date().getTime();
}
TimeServerPublisher.java
package ch01.ts;
import javax.xml.ws.Endpoint;
public class TimeServerPublisher
{
public static void main(String[ ] args)
{
Endpoint.publish("http://127.0.0.1:9876/ts", new TimeServerImpl());
}
}
Your class is not named TimeServerPublisher; it's named ch01.ts.TimeServerPublisher. Even if you manage to get the JVM to find your class file, it will reject it with a wrong-name error, as you must invoke the class with its full name.
Put all the class files into a directory ch01/ts if they're not there already, and from ch01's parent directory, type
java -cp . ch01.ts.TimeServerPublisher
I guarantee that done correctly this will work.
get rid of the package statements until you know how they work. to have that package, your sources and binaries should be under ./ch01/ts/ and you would compile and invoke as:
javac ch01/ts/*.java
java ch01.ts.TimeServerPublisher
Move all your class files to folder ch01/ts.
and then execute command
java ch01.ts.TimeServerPublisher
There you go. If you say javac -d ch01/ts *.java during compilation, it will be solved.