InvalidUseOfMatchersException when setting expectations in Mockito and Groovy 2.0.4 - exception

I am getting an unexpected error when trying to perform a simple test with mocks.
#RunWith(MockitoJUnitRunner)
class AccessorTest {
#Mock
private DeviceBuilder deviceBuilder
#Test
void shouldCreateDeviceFromFilesystem() {
//given
URI uri = this.class.classLoader.getResource("sample-filesystem").toURI()
File deviceRoot = new File(uri)
Accessor accessor = new Accessor(deviceBuilder)
Device expectedDevice = new Device(deviceRoot)
when(deviceBuilder.build(eq(deviceRoot))).thenReturn(expectedDevice)
//when
Device device = accessor.readFrom(deviceRoot)
//then
assert device == expectedDevice
verify(deviceBuilder).build(deviceRoot)
}
}
The DeviceBuilder is a single method interface Device::DeviceBuilder#build(File root). Device has a well defined equals method as per Josh Bloch.
The exception is thrown on the when() line, and none of the variables in scope are null. The full exception is:
org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Invalid use of argument matchers!
0 matchers expected, 1 recorded.
This exception may occur if matchers are combined with raw values:
//incorrect:
someMethod(anyObject(), "raw String");
When using matchers, all arguments have to be provided by matchers.
For example:
//correct:
someMethod(anyObject(), eq("String by matcher"));
For what it's worth here's a snippet of my POM:
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<compilerId>groovy-eclipse-compiler</compilerId>
</configuration>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-compiler</artifactId>
<version>2.7.0-01</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.0.4</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.8.5</version>
<scope>test</scope>
</dependency>
</dependencies>
My suspicions are either some weirdness from the way Groovy manages classes, or some version incompatibility, but I hope it's something obvious I just can't see.

I know this is an old question, but this may be helpful to someone else.
The issue described in the question is detailed at http://code.google.com/p/mockito/issues/detail?id=303
I ran into the very same issue and I got it fixed by using the library provided at https://github.com/cyrusinnovation/mockito-groovy-support

Sometimes Mockito doesn't flag inappropriate uses at the time it occurrs but at the next invocation. You could have a look at the test before that one, maybe you have a problem there?
The message says that in an invocation you either have to provide only Matchers or only real objects, but not a combination thereof. You can usually fix that by using eq(obj) instead of the object itself (or sameInstance(obj)), to have all Matchers. However in this case I don't find any place in the code you posted that would fit that description, that's why I suspect a problem in an earlier place of the code.

I'm not answering the original question.
I've searched everywhere and the only question about the error I got points to this discussion.
In case if someone got this error: do not call mock methods to provide values for matchers. The call deviceBuilder.build should go BEFORE verify(.

Related

error: package com.google.appengine.labs.repackaged.org.json does not exist

I want to return JSON object back to the client side.I add dependencies and it is not working again.What is the purpose of 'repackaged'?I can not find any documentation of this pacakge to add in pom.xml.It allows me to create a JSONObject but in my console appear this error?!
pom.xml
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-json-plugin</artifactId>
<version>${struts.version}</version>
</dependency>
<dependency>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-api-labs</artifactId>
<version>${appengine.app.version}</version>
<scope>test</scope>
</dependency>
I used Gson and works for me.It is easy to implement and convert Java object to JSON and vice versa!Here is GSON dependency:
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.3.1</version>
</dependency>
Treat the 'repackaged' package as internal GAE implementation choices that may change. Such classes have their package path surgically altered to include repackaged so that they won't conflict with your choices.
I've had problems with Eclipse offering repackaged classes up as viable options. How to hide some Eclipse autocomplete results shows a way to fix that.
Just a follow up on this for IntelliJ users. You can tell the IDE to not offer repackaged classes in:
(for IntelliJ 2016.1)
Settings > Editor > General > Auto Import > Exclude from Import and
Completion
Enter there the following and you are done.
com.google.appengine.labs.repackaged
com.google.appengine.repackaged

Rest-assured JsonPath working in JUnit Test but not when deployed in application (Weblogic)

A little background first.
I have an application that is deployed in Weblogic. It receives a Json response from a Service. I'm trying to use JsonPath to navigate the tree and I'm having an unusual issue.
I'm using Maven to build/deploy the application.
Dependency:
<dependency>
<groupId>com.jayway.restassured</groupId>
<artifactId>json-path</artifactId>
<version>1.8.1</version>
</dependency>
After getting it running with the full response in Junit and realizing that it wasn't working in the application when deployed, I made it simpler and hard coded a very small subset of the data.
{
"ChangeStatus": {
"Code": {
"value": "1002"
},
"Description": {
"value": "Matched more then 10 records"
}
}
}
Here's what I'm looking at right now...
String miniJson = "{\"ChangeStatus\":{\"Code\":{\"value\":\"1002\"},\"Description\":{\"value\":\"Matched more then 10 records\"}}}";
JsonPath miniJsonPath = new JsonPath(miniJson);
String statusCode = miniJsonPath.getString("ChangeStatus.Code.value");
In JUnit, this code works and I can assert 1002 successfully.
In the application after pushing to weblogic, this exact code snippet does not work.
It throws a NoSuchMethodError.
Any ideas would be welcome.
FYI, we are on Weblogic 10.3.6
Thanks in advance!
I am not an expert in Weblogic, but as an alternative you can include
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.2.0-rc1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.2.0-rc1</version>
</dependency>
and create a class ChangeStatus with 2 members Code and Description and then you can deserialize the JSON using:
`new ObjectMapper().readValue(miniJson, ChangeStatus.class)`
Hope it hepls.
What I discovered is that jsonpath depends upon antlr.
Weblogic also includes this package, but I believe it is an older version.
I fixed the problem by telling Weblogic to use the classes included in the app.
weblogic.xml
<wls:container-descriptor>
<wls:prefer-web-inf-classes>true</wls:prefer-web-inf-classes>
</wls:container-descriptor>

Deserialize List with Jackson in JUnit (Jersey Client)

I recently moved from Jersey 1.X to Jersey 2.1 and using jersey-media-json-jackson for (de-)serializing to Json.
In my JUnit-Test I would like to consume a web service that return a List.
With Jerey 1.x I used to work with GenericType. However, folling code does not work with Jersey 2.1 / Jackson 2.1:
GenericType<Collection<String>> listType = new GenericType<Collection<String>>() {};
assertTrue(target("location").request().get(listType).contains("item"));
it crashes with
org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException:
MessageBodyReader not found for media type=application/json,
type=interface java.util.List, genericType=java.util.Collection<java.lang.String>.
Serializing is okay, since following code:
target("location").request().accept(MediaType.APPLICATION_JSON).get(String.class);
returns a valid Json String
Any ideas how to fix? Any working examples?
Versions:
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-common</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.1</version>
</dependency>
Probably too late and you found the solution, but here it is for some else who might run into the same problem. Make sure you register the JacksonFeature on a client config before creating your client, code example below.
ClientConfig cc = new ClientConfig().register(new JacksonFeature());
Client client = ClientBuilder.newClient(cc);
WebTarget target = client.target(url);

Jersey No WebApplication provider is present when jersey-* dependency added

I have a simple Spring & Jersey application, which works perfectly well for consuming requests through a simple Resource. However, I'd like to return a JSON response - containing a simple JSON serialization of an object. To achieve this, I've added a maven dependency for jersey-json. As soon as I add this dependency, however, I get this error at server startup:
com.sun.jersey.api.container.ContainerException: No WebApplication provider is present at
com.sun.jersey.spi.container.WebApplicationFactory.createWebApplication(WebApplicationFactory.java:69) at
com.sun.jersey.spi.container.servlet.ServletContainer.create(ServletContainer.java:391)
I'm not totally clear upon exactly what a provider is, but I'm pretty certain that there should be a default one found.
For completeness, here's my Resource:
#Path("/scan")
#Resource
#Component
public class ScanResource {
#GET
#Produces(MediaType.APPLICATION_JSON)
#Path("/{barcode}")
public List<Scan> getScansForBarcode(#PathParam("barcode") Long barcode){
..snip..
return results;
}
}
A Scan object is a simple Entity Bean object.
The mvn dependency is:
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.11</version>
</dependency>
Does anyone know why I might be getting the No WebApplication provider is present Exception? Any thoughts on how I might resolve it?
Thanks
You need to have jersey-server jar on your classpath as well. And you need to make sure that all your jars are from the same version, Jersey runtime won't be able to use provided classes otherwise.
Additionally (most likely not relevant here, but..) there is a recent change in module structure - servlet dependencies were separated to new modules. So if you are using servlets, you might want to depend on jersey-servlet (which depends on jersey-server).
I am also had this issue. The issue was resolved by having same version for "jersey-json" and "jersey-servlet"maven dependencies.
EX:
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.13</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>1.13</version>
</dependency>

RestEasy and JSON - how to avoid quotes around a number?

I am using RestEasy to marchal entities to JSON. That works okay but somehow every thing is represented as a String. e.g.
#XmlRootElement(name="TestObject")
public class TestObject {
private Long value;
public Long getValue(){
return value;
}
}
Instead of creating something like: {TestObject:{value:1234}}
It creates {TestObject:{value:"1234"}} (Please note the " " around the number)
So the long value is converted into a String. How can I avoid that?
I've asked on the Jackson forum which RestEasy is using for the JSON marchaling but they said it is probably caused by going Java->XML->JSON.
There doesn't seem to be a RestEasy forum and on the Seam forum no one could answer my question.
Does anyone else have the same problem?
Regards
Okay the problem is that RestEasy+Seam uses Jettison by default (and not Jackson).
Jettison does the marchaling via Java->XML->JSON.
The Jackson jars aren't actually included in the Seam distribution so you have to download RestEasy and copy all jars which mention jackson to your lib directory. When RestEasy finds the resteasy-jackson-provider.jar in the classpath, Jackson will be used instead of Jettison.
One problem I had when moving to Jackson from Jettison were cycling references. With Jettison you just annotate the method (e.g. a #ManyToOne relationship) with
#XmlTransient. For Jackson you have to annotate it with #JsonIgnore
use
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jettison-provider</artifactId>
<version>2.0.1.GA</version>
<exclusions>
<exclusion>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
</exclusion>
<exclusion>
<groupId>javax.xml.stream</groupId>
<artifactId>stax-api</artifactId>
</exclusion>
</exclusions>
</dependency>
I use only resteasy-jettison-provider, but server hangs when I try to get JSON output, but after I excludes the jaxb-api and jaxb-impl. webservice works perfectly fine with JSON output