fasterxml jackson: there is no necessary methods to use #JsonView functionality - json

I try to organize dynamic #JsonIgnore property when I convert java-object to json-string. In example
it works in next way
for (codehause jackson)
ObjectMapper oMapper = new ObjectMapper();
oMapper.setSerializationConfig(...
or in example for fasterxml
objectMapper.getSerializationConfig().setSerializationView(
PROBLEM: bolded method are absent in my jackson:
pom.xml
<jackson.version>2.1.1</jackson.version>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
</dependency>

Have a look at ObjectWriter and ObjectReader which do include functionality for setting active view to use. This is different from Jackson 1.x, which exposed them through SerializationConfig and DeserializationConfig.
You can construct these objects from ObjectMapper (mapper.readerXxx() and mapper.writerXxx()); they are reusable, and offer more thread-safe configurability than ObjectMapper.

Related

How to mock a static method in JUnit 5

I am upgrading my JUnit to version 5 and I get this error when I run the JUnit 5
I am using in my pom
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
</dependency>
org.powermock.api.mockito.ClassNotPreparedException:
[Ljava.lang.Object;#723ca036 The class com.xxxxxx.MyClass not prepared
for test.
I am using #RunWith(JUnitPlatform.class) for my class test notation
My code is
PowerMockito.mockStatic(MyClass.class);
when(MyClass.get(anyString()))
.thenReturn(mock);
You have to use ExtendWith. In junit 5 the annotation gets #RunWith changed to
#ExtendWith(JUnitPlatform.class)
Further details on how to use Extend with

GenericObjectPool<PoolableConnection>

i think i found a pretty good answer for my question here:
log4j2-jdbc-manager-cannot-connect-to-the-database
I tried to implement the code in my project and everything worked fine beside of this little error:
GenericObjectPool<PoolableConnection>
Type GenericObjectPool does not take parameters.
I don't know how I can do a workaround, or what as to be changed.
thanks for your help!
Try this
https://git-wip-us.apache.org/repos/asf?p=commons-dbcp.git;a=blob_plain;f=doc/PoolingDataSourceExample.java;hb=HEAD
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.3</version>
</dependency>
Then in your code add:
import org.apache.commons.pool2.impl.GenericObjectPool;

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

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);

InvalidUseOfMatchersException when setting expectations in Mockito and Groovy 2.0.4

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(.