Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ jobs:
- name: Build and Deploy with Maven
env:
COVERALLS_TOKEN: ${{ secrets.COVERALLS_TOKEN }}
run: ./mvnw -B deploy coveralls:report
uses: GabrielBB/xvfb-action@v1
with:
run: ./mvnw -B deploy coveralls:report
- name: Clear Caches
run: curl -X PURGE https://camo.githubusercontent.com/8b5dee301fc3aee86900e1db08654773df3bd0938cd1d1009be98a34deb7478b/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f6c696e6b2d696e74657273797374656d732f6c69732d636f6d6d6f6e732f62616467652e7376673f6272616e63683d6d6173746572
23 changes: 23 additions & 0 deletions lis-commons-beans-mockito/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# lis-commons-beans-mockito

A mockito extension for bean related stuff.

## BeanMatchers

The BeanMatchers class provides support for java bean related equality matching.

> verify(personSetter, times(1)).setPerson(BeanMatchers.propertiesEqual(expectedPerson));
>
> // or exclude properties
>
> verify(personSetter, times(1)).setPerson(BeanMatchers.propertiesEqual(expectedPerson,"firstname"));

### Custom BeanMatcher

You can also create a custom BeanMatcher with another BeansFactory. E.g. one can create BeanMatcher that supports
Java records.

> BeansFactory recordBeansFactory = BeansFactory.getInstance("record");
> BeanMatcher recordBeanMatcher = new BeanMatcher(recordBeansFactory);
>
> verify(personSetter, times(1)).setPerson(recordBeanMatcher.propertiesEqual(expectedPersonRecord));
27 changes: 27 additions & 0 deletions lis-commons-beans-mockito/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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>
<parent>
<groupId>com.link-intersystems.commons</groupId>
<artifactId>lis-commons</artifactId>
<version>1.9.7-SNAPSHOT</version>
</parent>

<artifactId>lis-commons-beans-mockito</artifactId>
<description>A mockito extension for bean related stuff.</description>

<dependencies>
<dependency>
<groupId>com.link-intersystems.commons</groupId>
<artifactId>lis-commons-beans</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>compile</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.link_intersystems.beans.mockito;

import com.link_intersystems.beans.BeansFactory;
import org.mockito.ArgumentMatcher;

import static java.util.Objects.requireNonNull;
import static org.mockito.internal.progress.ThreadSafeMockingProgress.mockingProgress;

public class BeanMatcher {

public static final BeanMatcher DEFAULT = new BeanMatcher();

private BeansFactory beansFactory;

public BeanMatcher() {
this(BeansFactory.getDefault());
}

public BeanMatcher(BeansFactory beansFactory) {
this.beansFactory = requireNonNull(beansFactory);
}

public <T> T propertiesEqual(Object aBean, String... excludeProperties) {
reportMatcher(new PropertiesMatcher<>(aBean, beansFactory, excludeProperties));
return null;
}

private void reportMatcher(ArgumentMatcher<?> matcher) {
mockingProgress().getArgumentMatcherStorage().reportMatcher(matcher);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.link_intersystems.beans.mockito;

import com.link_intersystems.beans.BeansFactory;

public class BeanMatchers {

public static <T> T propertiesEqual(Object aBean, String... excludeProperties) {
return BeanMatcher.DEFAULT.propertiesEqual(aBean, excludeProperties);
}

public static <T> T propertiesEqual(BeansFactory beansFactory, Object aBean, String... excludeProperties) {
BeanMatcher beanMatcher = new BeanMatcher(beansFactory);
beanMatcher.propertiesEqual(aBean, excludeProperties);
return null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.link_intersystems.beans.mockito;

import com.link_intersystems.beans.Bean;
import com.link_intersystems.beans.BeansFactory;
import com.link_intersystems.beans.Property;
import com.link_intersystems.beans.PropertyList;
import org.mockito.ArgumentMatcher;

import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

import static java.util.Objects.requireNonNull;

public class PropertiesMatcher<T> implements ArgumentMatcher<T> {

private final Bean<T> expectedBean;
private final List<String> excludeProperties;
private final BeansFactory beansFactory;

public PropertiesMatcher(T expectedBean, String... excludeProperties) {
this(expectedBean, BeansFactory.getDefault(), excludeProperties);
}

public PropertiesMatcher(T expectedBean, BeansFactory beansFactory, String... excludeProperties) {
this.beansFactory = requireNonNull(beansFactory);
this.expectedBean = beansFactory.createBean(expectedBean, Object.class);
this.excludeProperties = Arrays.asList(excludeProperties);
}

@Override
public boolean matches(T argument) {
Bean<T> actualBean = beansFactory.createBean(argument, Object.class);

Predicate<Property> exclucedPropertiesPredicate = pd -> !excludeProperties.contains(pd.getPropertyDesc().getName());
PropertyList expectedProperties = expectedBean.getProperties();
PropertyList expectedFilteredProperties = expectedProperties.filter(exclucedPropertiesPredicate);

PropertyList actualProperties = actualBean.getProperties();
PropertyList actualFilteredProperties = actualProperties.filter(exclucedPropertiesPredicate);

return expectedFilteredProperties.equals(actualFilteredProperties);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.link_intersystems.beans.mockito;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static com.link_intersystems.beans.mockito.BeanMatchers.propertiesEqual;
import static org.mockito.Mockito.*;

class BeanMatchersTest {

interface PersonSetter {
public void setPerson(Person person);
}

private Person johnDoe;
private Person janeDoe;

@BeforeEach
void setUp() {
PersonFixture personFixture = new PersonFixture();
johnDoe = personFixture.getJohnDoe();
janeDoe = personFixture.getJaneDoe();
}

@Test
void testPropertiesEqual() {
PersonSetter personSetter = mock(PersonSetter.class);

personSetter.setPerson(johnDoe);

Person johnDoeCopy = new Person();
johnDoeCopy.setFirstname("John");
johnDoeCopy.setLastname("Doe");
johnDoeCopy.setAge(37);

verify(personSetter, times(1)).setPerson(propertiesEqual(johnDoeCopy));
}

@Test
void testPropertiesEqualExcludes() {
PersonSetter personSetter = mock(PersonSetter.class);

personSetter.setPerson(johnDoe);


verify(personSetter, times(1)).setPerson(propertiesEqual(janeDoe, "firstname", "age"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.link_intersystems.beans.mockito;

public class Person {

String firstname;
String lastname;
int age;

public String getFirstname() {
return firstname;
}

public void setFirstname(String firstname) {
this.firstname = firstname;
}

public String getLastname() {
return lastname;
}

public void setLastname(String lastname) {
this.lastname = lastname;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.link_intersystems.beans.mockito;

public class PersonFixture {

public Person getJohnDoe() {
Person johnDoe = new Person();

johnDoe.setFirstname("John");
johnDoe.setLastname("Doe");
johnDoe.setAge(37);

return johnDoe;
}

public Person getJaneDoe() {
Person janeDoe = new Person();

janeDoe.setFirstname("Jane");
janeDoe.setLastname("Doe");
janeDoe.setAge(35);

return janeDoe;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.link_intersystems.beans.mockito;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

class PropertiesMatcherTest {

private PersonFixture personFixture;
private PropertiesMatcher<Person> johnDoeMatcher;

@BeforeEach
void setUp() {
personFixture = new PersonFixture();
johnDoeMatcher = new PropertiesMatcher<>(personFixture.getJohnDoe());
}

@Test
void matches() {
assertTrue(johnDoeMatcher.matches(personFixture.getJohnDoe()));
}

@Test
void wontMatch() {
assertFalse(johnDoeMatcher.matches(personFixture.getJaneDoe()));
}

@Test
void matchExcludes() {
johnDoeMatcher = new PropertiesMatcher<>(personFixture.getJohnDoe(), "firstname", "age");

assertTrue(johnDoeMatcher.matches(personFixture.getJaneDoe()));
}
}
15 changes: 0 additions & 15 deletions lis-commons-beans/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,4 @@

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

This file was deleted.

This file was deleted.

Loading