How do I use Mockito to test that a Java 8 Stream had the expected values? - mockito

One of the interactions I want to test is that a class Foo is supposed to pass a Stream<Changes> to FooListener.someChangesHappened. Is there a Mockito idiom to verify that a stream contained the expected objects?

Assuming you are just verifying the argument to a mock implementation, and are not actually using it, here is a custom Hamcrest Matcher that will get the job done. It gets hairy when you need to read from the Stream more than once, because Streams are not built for that. You'll notice that this solution even needs to protect itself from JUnit calling matches more than once.
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.not;
import static org.mockito.Matchers.argThat;
import static org.mockito.Mockito.verify;
#RunWith(MockitoJUnitRunner.class)
public class Foo {
#Mock
FooListener fooListener;
#Before
public void happen() {
fooListener.someChangesHappened(Stream.of(Changes.ONE, Changes.TWO, Changes.THREE));
}
#Test
public void contains() {
verify(fooListener).someChangesHappened(argThat(streamThat(hasItem(Changes.TWO))));
}
#Test
public void doesNotContain() {
verify(fooListener).someChangesHappened(argThat(streamThat(not(hasItem(Changes.FOUR)))));
}
private static <T> Matcher<Stream<T>> streamThat(Matcher<Iterable<? super T>> toMatch) {
return new IterableStream<>(toMatch);
}
private interface FooListener {
void someChangesHappened(Stream<Changes> stream);
}
private enum Changes {
ONE, TWO, THREE, FOUR
}
private static class IterableStream<T> extends TypeSafeMatcher<Stream<T>> {
Matcher<Iterable<? super T>> toMatch;
List<T> input = null;
public IterableStream(Matcher<Iterable<? super T>> toMatch) {
this.toMatch = toMatch;
}
#Override
protected synchronized boolean matchesSafely(Stream<T> item) {
// This is to protect against JUnit calling this more than once
input = input == null ? item.collect(Collectors.toList()) : input;
return toMatch.matches(input);
}
#Override
public void describeTo(Description description) {
description.appendText("stream that represents ");
toMatch.describeTo(description);
}
}
}

Related

Archunit check method calls

I have a class that has three methods that shouldn't be called from certain classes.
How can I check this with Archunit?
So for example
public class Foo {
public void method1() {
}
public void method2() {
}
public void method3() {
}
}
method1 and method2 should only be called by classes Bar1 and Bar2.
I have a very similar requirement and came up with this:
#ArchTest
public static final ArchRule rule = noClasses()
.that(not(name(Bar1.class.getName()))
.and(not(name(Bar2.class.getName()))))
.should().callMethodWhere(target(nameMatching("method1"))
.and(target(owner(assignableTo(Foo.class)))))
.orShould().callMethodWhere(target(nameMatching("method2"))
.and(target(owner(assignableTo(Foo.class)))));
I have not tested it, but should be close I think.
EDIT: imports are:
import com.tngtech.archunit.junit.AnalyzeClasses;
import com.tngtech.archunit.junit.ArchTest;
import com.tngtech.archunit.lang.ArchRule;
import static com.tngtech.archunit.base.DescribedPredicate.not;
import static com.tngtech.archunit.core.domain.JavaCall.Predicates.target;
import static com.tngtech.archunit.core.domain.JavaClass.Predicates.assignableTo;
import static com.tngtech.archunit.core.domain.properties.HasName.Predicates.name;
import static com.tngtech.archunit.core.domain.properties.HasName.Predicates.nameMatching;
import static com.tngtech.archunit.core.domain.properties.HasOwner.Predicates.With.owner;
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.noClasses;
With
import com.tngtech.archunit.lang.ArchRule;
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.noClasses;
import static com.tngtech.archunit.lang.conditions.ArchConditions.callMethod;
you can use
ArchRule rule = noClasses()
.that().doNotHaveFullyQualifiedName(Bar1.class.getName())
.and().doNotHaveFullyQualifiedName(Bar2.class.getName())
// alternative 1:
.should().callMethod(Foo.class, "method1")
.orShould().callMethod(Foo.class, "method2");
// alternative 2:
// .should(callMethod(Foo.class, "method1").or(callMethod(Foo.class, "method2")));

How to convert a string (representing key-value pairs) to Map

How can I convert a String into a Map:
Map m = convert("A=4 H=X PO=87"); // What's convert?
System.err.println(m.getClass().getSimpleName()+m);
Expected output
HashMap{A=4, H=X, PO=87}
There is no need to reinvent the wheel. The Google Guava library provides the Splitter class.
Here's how you can use it along with some test code:
package com.sandbox;
import com.google.common.base.Splitter;
import org.junit.Test;
import java.util.Map;
import static org.junit.Assert.assertEquals;
public class SandboxTest {
#Test
public void testQuestionInput() {
Map<String, String> map = splitToMap("A=4 H=X PO=87");
assertEquals("4", map.get("A"));
assertEquals("X", map.get("H"));
assertEquals("87", map.get("PO"));
}
private Map<String, String> splitToMap(String in) {
return Splitter.on(" ").withKeyValueSeparator("=").split(in);
}
}
package com.sandbox;
import com.google.common.base.Splitter;
import org.junit.Test;
import java.util.Map;
import static org.junit.Assert.assertEquals;
public class SandboxTest {
#Test
public void testQuestionInput() {
Map<String, String> map = splitToMap("A=4 H=X PO=87");
assertEquals("4", map.get("A"));
assertEquals("X", map.get("H"));
assertEquals("87", map.get("PO"));
}
private Map<String, String> splitToMap(String in) {
return Splitter.on(" ").withKeyValueSeparator("=").split(in);
}
}

FacesContextListener implementation

In an attempt to implement the FacesContextListener, which seems to be the ideal place for the current challenge we are facing, I still struggle with its implementation. Attempts to declare it in the faces-config.xml or a construction similar to the ApplicationListener failed (as I probably reference things wrong (except for the class itself of course)).
Can someone provide directions / a short example regarding the implementation of the FacesContextListener?
Create a Java class which implements FacesContextListener interface.
package ch.hasselba.xpages;
import javax.faces.context.FacesContext;
import com.ibm.xsp.event.FacesContextListener;
public class MyFacesContextListener implements FacesContextListener {
public void beforeContextReleased(FacesContext fc) {
System.out.println("beforeContextReleased");
}
public void beforeRenderingPhase(FacesContext fc) {
System.out.println("beforeRenderingPhase");
}
}
Now, add an instance of the class to your XPage:
importPackage( ch.hasselba.xpages )
var fcl = new ch.hasselba.xpages.MyFacesContextListener();
facesContext.addRequestListener( fcl );
Hope this helps!
EDIT:
Here is a Java implementation with an anonymous Listener:
package ch.hasselba.xpages;
import javax.faces.context.FacesContext;
import com.ibm.xsp.context.FacesContextExImpl;
import com.ibm.xsp.event.FacesContextListener;
public class MyObject {
private transient FacesContextListener mFCListener;
public MyObject() {
mFCListener = new FacesContextListener() {
public void beforeContextReleased(FacesContext fc) {
System.out.println("Before Releasing.");
}
public void beforeRenderingPhase(FacesContext fc) {
System.out.println("Before Rendering.");
}
};
FacesContextExImpl fc = (FacesContextExImpl) FacesContext.getCurrentInstance();
fc.addRequestListener( this.mFCListener );
}
}

PowerMockito not obeying when.thenReturn

I am using PowerMockito to verify the number of calls on a private method: loadProperties().
I also have that method stubbed to return a pre-defined value.
Even though I have the method stubbed, the real implementation is being called, and throwing a NullPointerException, since its dependency (the "loader" variable, see snippet below) is not defined (nor should it be) for this test.
As an experiment, I changed the method to be public, and then it works fine!
I am thinking it is a bug in PowerMockito, but I have been wrong about more certain things than this!
Here is the code
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import java.util.HashMap;
import java.util.Map;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.times;
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.when;
#RunWith(PowerMockRunner.class)
#PrepareForTest({DArgumentsLoader.class})
public class TestConfig {
#Test
public void testGetPropertyMapCalledOnce() throws Exception {
Config configMock = mock(Config.class);
Map<String, String> testMap = new HashMap<String, String>();
testMap.put("xx", "xx");
when(configMock, "loadProperties").thenReturn(testMap);
when(configMock.getString(anyString(), anyString())).thenCallRealMethod();
// call it twice
configMock.getString("xx", "yy");
configMock.getString("xx", "yy");
// verify loadProperties was only called once
PowerMockito.verifyPrivate(configMock, times(1)).invoke("loadProperties");
}
}
Just to clarify, the Config class looks like this
private Map<String, String> loadProperties() throws IOException, HttpException {
return loader.loadProperties();
}
public String getString(String key, final String defaultValue) {
String value = getPropertyMap().get(key);
if(value != null) {
return value;
} else {
return defaultValue;
}
}
private Map<String, String> getPropertyMap() throws LoadException {
if(propertyMap == null) {
propertyMap = loadProperties();
}
return propertyMap;
}
The loadProperties() method should not be called at all, in view of the fact that the tester says
when(configMock, "loadProperties").thenReturn(testMap);
But it is being called, and it is throwing a NullPointerException. Is that a bug or a feature?
You are not preparing the Config class for mocking, so PowerMock cannot handle it. Just add it to the #PrepareForTest annotation, and you should be fine:
#PrepareForTest({Config.class, DArgumentsLoader.class})

CDI #Decorator, Is there a way to deactivate a decorator at runtime?

I use the following workaround in order to control the behaviour of a #Decorator since I couldn't find a way to deactivate it.
if (!FacesContext.getCurrentInstance().getViewRoot().getViewId()
.endsWith("decoratorDemo.xhtml")) {
return transInterBean.getTransactionalInsertRecords();
} else {
...
}
Is there no way to decide at runtime whether a decorator should be applied?
package com.cdi.decorators;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import javax.decorator.Decorator;
import javax.decorator.Delegate;
import javax.enterprise.inject.Any;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import com.cdi.cdibeans.TransactionalInterceptor;
import com.cdi.cdibeans.TransactionalInterceptorBean;
#Decorator
public abstract class TransactionalInterceptorDecorator implements
TransactionalInterceptor {
/**
*
*/
private static final long serialVersionUID = -1191671082441891759L;
#Inject
#Delegate
#Any
TransactionalInterceptorBean transInterBean;
#Override
public ArrayList<String> getTransactionalInsertRecords()
throws SQLException {
ArrayList<String> records = new ArrayList<String>();
if (!FacesContext.getCurrentInstance().getViewRoot().getViewId()
.endsWith("decoratorDemo.xhtml")) {
return transInterBean.getTransactionalInsertRecords();
} else {
Iterator<String> iter = transInterBean
.getTransactionalInsertRecords().iterator();
while (iter.hasNext()) {
String record = iter.next();
records.add(record);
records.add(">>>Decorator<<< Added record ... ");
}
if (records.isEmpty()) {
records.add(">>>Decorator<<< Currently there are no records yet!");
}
return records;
}
}
}
Deltaspike has an exclude feature ... may be this could help, I didn't try it with decorators.

Resources