Merge remote-tracking branch 'origin/master'

Conflicts:
	pom.xml
This commit is contained in:
Slavisa Baeldung
2016-07-25 14:33:17 +02:00
95 changed files with 1770 additions and 1088 deletions
+1
View File
@@ -0,0 +1 @@
target/
+41 -29
View File
@@ -8,18 +8,42 @@
<artifactId>apache-cxf</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<properties>
<cxf.version>3.1.6</cxf.version>
<spring.version>4.3.1.RELEASE</spring.version>
<surefire.version>2.19.1</surefire.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<webXml>src/main/webapp/WEB-INF/web.xml</webXml>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
@@ -33,6 +57,7 @@
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>integration</id>
@@ -50,7 +75,7 @@
<configuration>
<properties>
<cargo.hostname>localhost</cargo.hostname>
<cargo.servlet.port>8080</cargo.servlet.port>
<cargo.servlet.port>8081</cargo.servlet.port>
</properties>
</configuration>
</configuration>
@@ -71,6 +96,7 @@
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.version}</version>
@@ -91,27 +117,13 @@
</plugins>
</build>
</profile>
</profiles>
<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
<properties>
<cxf.version>3.1.6</cxf.version>
<spring.version>4.3.1.RELEASE</spring.version>
<surefire.version>2.19.1</surefire.version>
</properties>
</project>
@@ -0,0 +1,21 @@
package com.baeldung.cxf.spring;
import javax.servlet.ServletContext;
import javax.servlet.ServletRegistration;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
public class AppInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext container) {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(ServiceConfiguration.class);
container.addListener(new ContextLoaderListener(context));
ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new CXFServlet());
dispatcher.addMapping("/services");
}
}
@@ -5,5 +5,6 @@ import javax.jws.WebService;
@WebService
public interface Baeldung {
String hello(String name);
String register(Student student);
}
@@ -0,0 +1,21 @@
package com.baeldung.cxf.spring;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ClientConfiguration {
@Bean(name = "client")
public Object generateProxy() {
return proxyFactoryBean().create();
}
@Bean
public JaxWsProxyFactoryBean proxyFactoryBean() {
JaxWsProxyFactoryBean proxyFactory = new JaxWsProxyFactoryBean();
proxyFactory.setServiceClass(Baeldung.class);
proxyFactory.setAddress("http://localhost:8081/services/baeldung");
return proxyFactory;
}
}
@@ -0,0 +1,24 @@
package com.baeldung.cxf.spring;
import javax.xml.ws.Endpoint;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ServiceConfiguration {
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
}
@Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(springBus(), new BaeldungImpl());
endpoint.publish("http://localhost:8081/services/baeldung");
return endpoint;
}
}
@@ -1,10 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd">
<bean id="client" factory-bean="clientFactory" factory-method="create" />
<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass" value="com.baeldung.cxf.spring.Baeldung" />
<property name="address" value="http://localhost:8080/services/baeldung" />
</bean>
</beans>
@@ -1,7 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<jaxws:endpoint id="baeldung"
implementor="com.baeldung.cxf.spring.BaeldungImpl" address="http://localhost:8080/services/baeldung" />
</beans>
@@ -1,14 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="3.0"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
</web-app>
@@ -3,15 +3,12 @@ package com.baeldung.cxf.spring;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class StudentTest {
private ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "client-beans.xml" });
private Baeldung baeldungProxy;
{
baeldungProxy = (Baeldung) context.getBean("client");
}
private ApplicationContext context = new AnnotationConfigApplicationContext(ClientConfiguration.class);
private Baeldung baeldungProxy = (Baeldung) context.getBean("client");
@Test
public void whenUsingHelloMethod_thenCorrect() {
@@ -25,7 +22,7 @@ public class StudentTest {
Student student2 = new Student("Eve");
String student1Response = baeldungProxy.register(student1);
String student2Response = baeldungProxy.register(student2);
assertEquals("Adam is registered student number 1", student1Response);
assertEquals("Eve is registered student number 2", student2Response);
}
+20 -4
View File
@@ -1,6 +1,5 @@
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
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>
@@ -9,7 +8,7 @@
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>Resource vs Inject vs Autowired</name>
<name>dependency-injection</name>
<description>Accompanying the demonstration of the use of the annotations related to injection mechanisms, namely
Resource, Inject, and Autowired
</description>
@@ -54,6 +53,7 @@
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
@@ -71,13 +71,29 @@
</includes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>${maven-war-plugin.version}</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<maven-war-plugin.version>2.6</maven-war-plugin.version>
</properties>
<repositories>
<repository>
<id>java.net</id>
<url>https://maven.java.net/content/repositories/releases/</url>
</repository>
</repositories>
</project>
@@ -7,9 +7,19 @@ import java.util.List;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
public class CustomListDeserializer extends JsonDeserializer<List<ItemWithSerializer>> {
public class CustomListDeserializer extends StdDeserializer<List<ItemWithSerializer>> {
private static final long serialVersionUID = 1095767961632979804L;
public CustomListDeserializer() {
this(null);
}
public CustomListDeserializer(final Class<?> vc) {
super(vc);
}
@Override
public List<ItemWithSerializer> deserialize(final JsonParser jsonparser, final DeserializationContext context) throws IOException, JsonProcessingException {
@@ -6,11 +6,20 @@ import java.util.List;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
public class CustomListSerializer extends JsonSerializer<List<ItemWithSerializer>> {
public class CustomListSerializer extends StdSerializer<List<ItemWithSerializer>> {
private static final long serialVersionUID = 3698763098000900856L;
public CustomListSerializer() {
this(null);
}
public CustomListSerializer(final Class<List<ItemWithSerializer>> t) {
super(t);
}
@Override
public void serialize(final List<ItemWithSerializer> items, final JsonGenerator generator, final SerializerProvider provider) throws IOException, JsonProcessingException {
final List<Integer> ids = new ArrayList<Integer>();
@@ -8,12 +8,21 @@ import java.util.Date;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
public class CustomDateDeserializer extends JsonDeserializer<Date> {
public class CustomDateDeserializer extends StdDeserializer<Date> {
private static final long serialVersionUID = -5451717385630622729L;
private SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
public CustomDateDeserializer() {
this(null);
}
public CustomDateDeserializer(final Class<?> vc) {
super(vc);
}
@Override
public Date deserialize(final JsonParser jsonparser, final DeserializationContext context) throws IOException, JsonProcessingException {
final String date = jsonparser.getText();
@@ -6,13 +6,22 @@ import java.util.Date;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
public class CustomDateSerializer extends JsonSerializer<Date> {
public class CustomDateSerializer extends StdSerializer<Date> {
private static final long serialVersionUID = -2894356342227378312L;
private SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
public CustomDateSerializer() {
this(null);
}
public CustomDateSerializer(final Class<Date> t) {
super(t);
}
@Override
public void serialize(final Date value, final JsonGenerator gen, final SerializerProvider arg2) throws IOException, JsonProcessingException {
gen.writeString(formatter.format(value));
@@ -8,10 +8,20 @@ import org.joda.time.format.DateTimeFormatter;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
public class CustomDateTimeSerializer extends JsonSerializer<DateTime> {
public class CustomDateTimeSerializer extends StdSerializer<DateTime> {
private static final long serialVersionUID = -3927232057990121460L;
public CustomDateTimeSerializer() {
this(null);
}
public CustomDateTimeSerializer(final Class<DateTime> t) {
super(t);
}
private static DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm");
@@ -6,13 +6,23 @@ import java.time.format.DateTimeFormatter;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
public class CustomLocalDateTimeSerializer extends JsonSerializer<LocalDateTime> {
public class CustomLocalDateTimeSerializer extends StdSerializer<LocalDateTime> {
private static final long serialVersionUID = -7449444168934819290L;
private static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
public CustomLocalDateTimeSerializer() {
this(null);
}
public CustomLocalDateTimeSerializer(final Class<LocalDateTime> t) {
super(t);
}
@Override
public void serialize(final LocalDateTime value, final JsonGenerator gen, final SerializerProvider arg2) throws IOException, JsonProcessingException {
gen.writeString(formatter.format(value));
@@ -2,17 +2,26 @@ package com.baeldung.jackson.deserialization;
import java.io.IOException;
import com.baeldung.jackson.dtos.User;
import com.baeldung.jackson.dtos.Item;
import com.baeldung.jackson.dtos.User;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.node.IntNode;
public class ItemDeserializer extends JsonDeserializer<Item> {
public class ItemDeserializer extends StdDeserializer<Item> {
private static final long serialVersionUID = 1883547683050039861L;
public ItemDeserializer() {
this(null);
}
public ItemDeserializer(final Class<?> vc) {
super(vc);
}
/**
* {"id":1,"itemNr":"theItem","owner":2}
@@ -4,15 +4,24 @@ import java.io.IOException;
import com.baeldung.jackson.dtos.ItemWithSerializer;
import com.baeldung.jackson.dtos.User;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.node.IntNode;
public class ItemDeserializerOnClass extends JsonDeserializer<ItemWithSerializer> {
public class ItemDeserializerOnClass extends StdDeserializer<ItemWithSerializer> {
private static final long serialVersionUID = 5579141241817332594L;
public ItemDeserializerOnClass() {
this(null);
}
public ItemDeserializerOnClass(final Class<?> vc) {
super(vc);
}
/**
* {"id":1,"itemNr":"theItem","owner":2}
@@ -4,10 +4,20 @@ import java.io.IOException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
public class TypeSerializer extends JsonSerializer<TypeEnumWithCustomSerializer> {
public class TypeSerializer extends StdSerializer<TypeEnumWithCustomSerializer> {
private static final long serialVersionUID = -7650668914169390772L;
public TypeSerializer() {
this(null);
}
public TypeSerializer(final Class<TypeEnumWithCustomSerializer> t) {
super(t);
}
@Override
public void serialize(final TypeEnumWithCustomSerializer value, final JsonGenerator generator, final SerializerProvider provider) throws IOException, JsonProcessingException {
@@ -3,13 +3,22 @@ package com.baeldung.jackson.serialization;
import java.io.IOException;
import com.baeldung.jackson.dtos.Item;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
public class ItemSerializer extends JsonSerializer<Item> {
public class ItemSerializer extends StdSerializer<Item> {
private static final long serialVersionUID = 6739170890621978901L;
public ItemSerializer() {
this(null);
}
public ItemSerializer(final Class<Item> t) {
super(t);
}
@Override
public final void serialize(final Item value, final JsonGenerator jgen, final SerializerProvider provider) throws IOException, JsonProcessingException {
@@ -3,13 +3,22 @@ package com.baeldung.jackson.serialization;
import java.io.IOException;
import com.baeldung.jackson.dtos.ItemWithSerializer;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
public class ItemSerializerOnClass extends JsonSerializer<ItemWithSerializer> {
public class ItemSerializerOnClass extends StdSerializer<ItemWithSerializer> {
private static final long serialVersionUID = -1760959597313610409L;
public ItemSerializerOnClass() {
this(null);
}
public ItemSerializerOnClass(final Class<ItemWithSerializer> t) {
super(t);
}
@Override
public final void serialize(final ItemWithSerializer value, final JsonGenerator jgen, final SerializerProvider provider) throws IOException, JsonProcessingException {
@@ -4,10 +4,20 @@ import java.io.IOException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
public class MyDtoNullKeySerializer extends JsonSerializer<Object> {
public class MyDtoNullKeySerializer extends StdSerializer<Object> {
private static final long serialVersionUID = -4478531309177369056L;
public MyDtoNullKeySerializer() {
this(null);
}
public MyDtoNullKeySerializer(final Class<Object> t) {
super(t);
}
@Override
public void serialize(final Object value, final JsonGenerator jgen, final SerializerProvider provider) throws IOException, JsonProcessingException {
@@ -6,10 +6,19 @@ import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
public class RestLoaderRequestDeserializer extends JsonDeserializer<RestLoaderRequest<IEntity>> {
public class RestLoaderRequestDeserializer extends StdDeserializer<RestLoaderRequest<IEntity>> {
private static final long serialVersionUID = -4245207329377196889L;
public RestLoaderRequestDeserializer() {
this(null);
}
public RestLoaderRequestDeserializer(final Class<?> vc) {
super(vc);
}
@Override
public RestLoaderRequest<IEntity> deserialize(final JsonParser jp, final DeserializationContext ctxt) throws IOException, JsonProcessingException {
+1
View File
@@ -5,3 +5,4 @@
### Relevant Articles:
- [JMockit 101](http://www.baeldung.com/jmockit-101)
- [A Guide to JMockit Expectations](http://www.baeldung.com/jmockit-expectations)
@@ -1,9 +1,11 @@
package org.baeldung.mocks.jmockit;
public class Collaborator {
public boolean collaborate(String string){
return false;
}
public void receive(boolean bool){
//NOOP
}
@@ -1,7 +1,7 @@
package org.baeldung.mocks.jmockit;
public class Model {
public String getInfo(){
return "info";
public String getInfo() {
return "info";
}
}
@@ -1,21 +1,20 @@
package org.baeldung.mocks.jmockit;
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.List;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.junit.Test;
import org.junit.runner.RunWith;
import mockit.Delegate;
import mockit.Expectations;
import mockit.Mocked;
import mockit.StrictExpectations;
import mockit.Verifications;
import mockit.integration.junit4.JMockit;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.assertEquals;
@RunWith(JMockit.class)
@SuppressWarnings("unchecked")
@@ -23,130 +22,103 @@ public class ExpectationsTest {
@Test
public void testForAny(@Mocked ExpectationsCollaborator mock) throws Exception {
new Expectations() {
{
mock.methodForAny1(anyString, anyInt, anyBoolean);
result = "any";
}
};
new Expectations() {{
mock.methodForAny1(anyString, anyInt, anyBoolean);
result = "any";
}};
assertEquals("any", mock.methodForAny1("barfooxyz", 0, Boolean.FALSE));
mock.methodForAny2(2L, new ArrayList<>());
new Verifications() {
{
mock.methodForAny2(anyLong, (List<String>) any);
}
};
new Verifications() {{
mock.methodForAny2(anyLong, (List<String>) any);
}};
}
@Test
public void testForWith(@Mocked ExpectationsCollaborator mock) throws Exception {
new Expectations() {
{
mock.methodForWith1(withSubstring("foo"), withNotEqual(1));
result = "with";
}
};
new Expectations() {{
mock.methodForWith1(withSubstring("foo"), withNotEqual(1));
result = "with";
}};
assertEquals("with", mock.methodForWith1("barfooxyz", 2));
mock.methodForWith2(Boolean.TRUE, new ArrayList<>());
new Verifications() {
{
mock.methodForWith2(withNotNull(), withInstanceOf(List.class));
}
};
new Verifications() {{
mock.methodForWith2(withNotNull(), withInstanceOf(List.class));
}};
}
@Test
public void testWithNulls(@Mocked ExpectationsCollaborator mock) {
new Expectations() {
{
mock.methodForNulls1(anyString, null);
result = "null";
}
};
new Expectations() {{
mock.methodForNulls1(anyString, null);
result = "null";
}};
assertEquals("null", mock.methodForNulls1("blablabla", new ArrayList<String>()));
mock.methodForNulls2("blablabla", null);
new Verifications() {
{
mock.methodForNulls2(anyString, (List<String>) withNull());
}
};
new Verifications() {{
mock.methodForNulls2(anyString, (List<String>) withNull());
}};
}
@Test
public void testWithTimes(@Mocked ExpectationsCollaborator mock) {
new Expectations() {
{
// exactly 2 invocations are expected
mock.methodForTimes1();
times = 2;
mock.methodForTimes2(); // "minTimes = 1" is implied
}
};
new Expectations() {{
mock.methodForTimes1();
times = 2;
mock.methodForTimes2();
}};
mock.methodForTimes1();
mock.methodForTimes1();
mock.methodForTimes2();
mock.methodForTimes3();
mock.methodForTimes3();
mock.methodForTimes3();
new Verifications() {
{
// we expect from 1 to 3 invocations
mock.methodForTimes3();
minTimes = 1;
maxTimes = 3;
}
};
new Verifications() {{
mock.methodForTimes3();
minTimes = 1;
maxTimes = 3;
}};
}
@Test
public void testCustomArgumentMatching(@Mocked ExpectationsCollaborator mock) {
new Expectations() {
{
mock.methodForArgThat(withArgThat(new BaseMatcher<Object>() {
@Override
public boolean matches(Object item) {
return item instanceof Model && "info".equals(((Model) item).getInfo());
}
new Expectations() {{
mock.methodForArgThat(withArgThat(new BaseMatcher<Object>() {
@Override
public boolean matches(Object item) {
return item instanceof Model && "info".equals(((Model) item).getInfo());
}
@Override
public void describeTo(Description description) {
// NOOP
}
}));
}
};
@Override
public void describeTo(Description description) {
}
}));
}};
mock.methodForArgThat(new Model());
}
@Test
public void testResultAndReturns(@Mocked ExpectationsCollaborator mock) {
new StrictExpectations() {
{
// return "foo", an exception and lastly "bar"
mock.methodReturnsString();
result = "foo";
result = new Exception();
result = "bar";
// return 1, 2, 3
mock.methodReturnsInt();
result = new int[] { 1, 2, 3 };
// return "foo" and "bar"
mock.methodReturnsString();
returns("foo", "bar");
// return only 1
mock.methodReturnsInt();
result = 1;
}
};
new StrictExpectations() {{
mock.methodReturnsString();
result = "foo";
result = new Exception();
result = "bar";
mock.methodReturnsInt();
result = new int[]{1, 2, 3};
mock.methodReturnsString();
returns("foo", "bar");
mock.methodReturnsInt();
result = 1;
}};
assertEquals("Should return foo", "foo", mock.methodReturnsString());
try {
mock.methodReturnsString();
@@ -164,27 +136,23 @@ public class ExpectationsTest {
@Test
public void testDelegate(@Mocked ExpectationsCollaborator mock) {
new StrictExpectations() {
{
// return "foo", an exception and lastly "bar"
mock.methodForDelegate(anyInt);
times = 2;
result = new Delegate() {
public int delegate(int i) throws Exception {
if (i < 3) {
return 5;
} else {
throw new Exception();
}
new Expectations() {{
mock.methodForDelegate(anyInt);
result = new Delegate() {
public int delegate(int i) throws Exception {
if (i < 3) {
return 5;
} else {
throw new Exception();
}
};
}
};
}
};
}};
assertEquals("Should return 5", 5, mock.methodForDelegate(1));
try {
mock.methodForDelegate(3);
} catch (Exception e) {
// NOOP
}
}
}
@@ -21,7 +21,9 @@ public class PerformerTest {
model.getInfo();result = "bar";
collaborator.collaborate("bar"); result = true;
}};
performer.perform(model);
new Verifications() {{
collaborator.receive(true);
}};
+23 -3
View File
@@ -15,10 +15,17 @@
<modules>
<module>assertj</module>
<module>apache-cxf</module>
<module>apache-fop</module>
<module>core-java</module>
<module>core-java-8</module>
<module>couchbase-sdk-intro</module>
<module>couchbase-sdk-spring-service</module>
<module>dependency-injection</module>
<module>gatling</module>
<module>gson</module>
<!-- <module>gson-jackson-performance</module> -->
<module>gson-jackson-performance</module>
<module>guava</module>
<module>guava18</module>
<module>guava19</module>
@@ -28,27 +35,35 @@
<module>javaxval</module>
<module>jjwt</module>
<module>jooq-spring</module>
<module>json-path</module>
<module>jpa-storedprocedure</module>
<module>json</module>
<module>json-path</module>
<module>junit5</module>
<module>mockito</module>
<module>mocks</module>
<module>jee7schedule</module>
<!-- <module>jpa-storedprocedure</module> -->
<module>querydsl</module>
<!-- <module>raml</module> -->
<module>rest-assured</module>
<module>rest-testing</module>
<module>resteasy</module>
<module>log4j</module>
<module>spring-all</module>
<module>spring-apache-camel</module>
<module>spring-autowire</module>
<module>spring-batch</module>
<module>spring-boot</module>
<module>spring-controller</module>
<module>spring-data-cassandra</module>
<module>spring-data-couchbase-2</module>
<module>spring-data-couchbase-2b</module>
<module>spring-data-elasticsearch</module>
<module>spring-data-neo4j</module>
<module>spring-data-mongodb</module>
<module>spring-data-redis</module>
<module>spring-data-rest</module>
<module>spring-exceptions</module>
<module>spring-freemarker</module>
<module>spring-hibernate3</module>
@@ -62,9 +77,12 @@
<module>spring-openid</module>
<module>spring-protobuf</module>
<module>spring-quartz</module>
<module>spring-spel</module>
<module>spring-rest</module>
<module>spring-rest-docs</module>
<module>spring-security-basic-auth</module>
<module>spring-security-custom-permission</module>
<module>spring-security-mvc-custom</module>
<module>spring-security-mvc-digest-auth</module>
<module>spring-security-mvc-ldap</module>
@@ -82,9 +100,11 @@
<module>xml</module>
<module>lombok</module>
<module>redis</module>
<module>webjars</module>
<module>mutation-testing</module>
<module>spring-mvc-velocity</module>
<module>xstream</module>
</modules>
</project>

Some files were not shown because too many files have changed in this diff Show More