jerseytest | Writing lightweight REST integration tests with the Jersey | Code Editor library
kandi X-RAY | jerseytest Summary
kandi X-RAY | jerseytest Summary
Writing lightweight REST integration tests with the Jersey Test Framework. To compile the project, simply run "mvn package" inside the mjl-jersey-build directory. To view the project in eclipse, best use the maven eclipse plugin ("mvn eclipse:eclipse") and import as existing project. There is a blog post corresponding to this mini-tutorial:
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of jerseytest
jerseytest Key Features
jerseytest Examples and Code Snippets
Community Discussions
Trending Discussions on jerseytest
QUESTION
I have a self contained Jersey test using JerseyExtension (JerseyExtension) with JUnit5 (since JerseyTest does not work with JUnit5 unless you use the vintage engine) and subsequent calls to the container are getting different session. Is there a way to keep the session store same between the calls?
...ANSWER
Answered 2019-Dec-30 at 20:24The client used by Jersey test framework, does not behave like a browser when it comes to Set-Cookie/Cookie headers. The two requests are not connected and JSESSIONID set by first response is not propagated to next request. While the framework is aware of the JSESSIONID if present, it does not span requests and needs to be manually copied forward.
Changing the test method to following works:
QUESTION
Ran into a few errors as I am upgrading jdk11 and jersey but this one particularly stumped. So jersey 2.28 is supposedly compatible with OpenJDK11 and fixed some issues that was previously in 2.25.1, so I upgraded it
I also saw this already, not a working solution - Jersey Spring Maven java.lang.NoClassDefFoundError: org/glassfish/jersey/hk2/HK2InjectionManager
In my gradle build, failed on this task:
> Task :ods-impl:test FAILED
Under my task for this, here is the list of dependencies
...ANSWER
Answered 2019-May-15 at 19:42Turns out jersey-spring3 is outdated. If you want to use 2.27+ Jersey with Spring, but change dependency to use jersey-spring4
compile group: 'org.glassfish.jersey.ext', name: 'jersey-spring4', version: '2.28'
QUESTION
I'm trying to mock a controller/resource including the jax-rs layer. The class has dependencies that need to be injected.
It however also has some String
values that are injected using a qualifier interface.
Basically, I'm using JerseyTest to run a single controller and use HK2 for dependency injection. I created a ResourceConfig
and registered a AbstractBinder
to bind the injected classes.
This works fine for regular injected dependencies, but when the the additional @SomeQualifierInterface
annotation is added, it crashes with the following error:
ANSWER
Answered 2019-May-10 at 14:30Solved!
First, use the AbstractBinder
from org.glassfish.hk2.utilities.binding.AbstractBinder
instead of org.glassfish.jersey.internal.inject.AbstractBinder
.
Second, create a class that extends AnnotationLiteral
and implements the interface.
Last, bind the value to a TypeLiteral
and set the qualifiedBy to the AnnotationLiteral.
Full code:
QUESTION
When testing with JerseyTest, I am having trouble to bind to a:
...ANSWER
Answered 2019-Apr-11 at 20:15answered by @PaulSamsotha in his comment.
QUESTION
I'm building a Java REST API with Jersey 2.27 and in my test class (as shown below) I get the following compile error: Cannot resolve method 'bindFactory(java.lang.Class)'
.
I don't get why this is not compiling. The bindFactory
method has one parameter of type Class>
, and my factory class is implementing Supplier
as shown below. Can someone explain to me why this won't compile?
PS: Tthese are only snippets of the full code to make things clearer.
...ANSWER
Answered 2018-Jul-06 at 18:04Remove the generic type from your factory class. What you're supposed to be going is adding the concrete type to the Supplier
QUESTION
Trying to test a fairly simple JAX-RS endpoint
...ANSWER
Answered 2018-Jun-30 at 14:40The MockitoJUnitRunner
is for unit tests and JerseyTest
is for integration tests.
When using Mockito, your tests will call directly the declared myRestService
and Mockito dependency injection will take place.
When using JerseyTest, a new web container is created and your tests talk to MyRestService
via an HTTP call. Inside this container, the real dependency injection is happening, the classes are not even seeing you declared mocks.
You can use JerseyTest and Mockito together, exactly as you did. It just requires some extra configurations (as you already found) and the @RunWith
annotation is not necessary.
QUESTION
I configured jackson so that it gives me a smiple string representation if java.time.LocalDate
and java.time.LocalDateTime
. This works find in the serialization process, e.g when I get data on the REST api.
It doesn't work the other way round though. When I try to send data to the server and the JSON should be parsed to java objects this exception is thrown:
javax.ws.rs.client.ResponseProcessingException: com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of java.time.LocalDateTime: no String-argument constructor/factory method to deserialize from String value ('2018-04-19T14:10:30.903')
After a few hours of research I managed to get it to work, but only with the attributes I annotated with @JsonDeserialize(using = LocalDateDeserializer.class)
or @JsonDeserialize(using = LocalDateTimeDeserializer.class)
respectively.
In my opinion it would be ideal, if I could define these mappings in one central place.
ObjectMapper configuration:
...ANSWER
Answered 2018-Apr-22 at 07:03You registered the ContextResolver
with the server via the configure()
method of the JerseyTest
, but if you look at the exception, you'll see that is a client side exception (notice the client
in the package name). So the problem is on the client side. What you are missing is that the deserialization also needs to happen on the client side from JSON to Poc
, so you also need the ContextResolver
registered on the client. To do that, you can override configureClient()
on the JerseyTest
and register the ContextResolver
there.
QUESTION
I know that the error "MessageBodyProvider" not found for application type question has been asked a lot of time, but my case is a bit different.
The REST works fine when I manually invoke the REST API through Fiddler or in the browser. So there seems to something odd with the test. I can't get my head around this.
...ANSWER
Answered 2018-Apr-14 at 01:54You're only registering the provider on the server side in the configure()
method. You still need to register it on the client side. You can see in the stacktrace that the problem is occurring on the client side (see client
in the package names).
You can either just register the provider on the WebTarget
or you can override configureClient()
to register it with the Client
.
QUESTION
I am creating a application that should be able to create an account using a REST api. When I wrote the AccountController.java
class everything worked fine. I used postman to verify that I got the right response.
So my AccountController.java
works correctly but the unit test I wrote with it don't work. When the unit test is executed it returns a NullPointerException
which results in a InternalServerError within the REST API. While debugging i found out that if I remove the use of UriInfo in AccountController.java
and return Response.ok() instead of created the test is successfull.
So my question is. How do i properly test/mock a method that returns and URI?
Bellow are all relevant classes and dependencies. The Account.java
class is in another package. But both the main application as the project that contains Account.java
are part of the same parent pom. I am not sure if this is relevant to my question.
ANSWER
Answered 2017-Sep-22 at 10:04So I finally figured it out.
Somewhere on this line in AccountController.java
a NullPointerException
was thrown:
final URI uri = uriInfo.getAbsolutePathBuilder().path(newAccount.getId() + "").build();
This only happened when using my JerseyUnitTest so i guess Jersey must change some behavior of UriInfo
.
Eventually I solved the problem by overriding equals
in Account.java
.
QUESTION
I am trying to write some integration tests for an application that uses Jersey with some Spring DI. Tests always come back as a 500 with no real explaination. Grizzly appears to start ok and boots up my hibernate connections just fine. Here is the error trace along with all of relevant files sans the class under test. I think it may be an issue with Grizzly and the configuration but I cant find anything that seems to work including switching to a webappcontext.
...ANSWER
Answered 2017-Aug-04 at 02:23After several days of trial and error with almost no documentation and piecing together from other examples there were out of date, this is what finally worked for me. Hopefully it will help somebody else.
Test Class
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install jerseytest
You can use jerseytest like any standard Java library. Please include the the jar files in your classpath. You can also use any IDE and you can run and debug the jerseytest component as you would do with any other Java program. Best practice is to use a build tool that supports dependency management such as Maven or Gradle. For Maven installation, please refer maven.apache.org. For Gradle installation, please refer gradle.org .
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page