Run code only on test - java

I would like to execute a method only on test running.
This is a completion of integration test. When I run integration test, I want to generate a json file with the state of an Object.
I think of doing something like this in my code :
if(environnement == TEST) {
// save object as json
}
Do you know other method to do this ? less crapy ?
Regards

I generally provide test 'hooks' using this pattern
public class MyObject {
public void doStuff() {
// stuff to do
onCompleteStuff();
}
protected void onCompleteStuff() {
}
}
Then in the test code you can create a subclass of MyObject that does your test-only actions:
private MyObject newMyObject() {
return new MyObject() {
#Override
protected void onCompleteStuff() {
saveObjectAsJson();
}
};
}
This has the advantage of keeping test code out of your main build. It will also work if you must use a mock in your tests. For example, using the excellent Mockito library:
MyObject foo=Mockito.spy(new MyObject());
doAnswer(new Answer<Object>() {
#Override
public Object answer(InvocationOnMock invocation) throws Throwable {
saveObjectToJson();
return null;
}
}).when(foo).onCompleteStuff();

How about using the Polymorphism OOP feature?
abstract class MyObject {
void doSomething();
}
class MyObjectImpl {
void doSomething() {
// real implementation
}
}
class MyObjectTest {
void doSomething() {
// create your json or other test functional
}
}
class MyObjectFactory {
static getMyObject(Environment env) {
if (env == Environment.TEST) {
return new MyObjectTest();
} else {
return new MyObjectImpl();
}
}
}

Related

Is there a pattern to execute a chain of methods based on the result of the previous in Java?

The use case is there is a set of methods which need to be executed based on whether the previous one has returned true or not.
For example:
class Test {
boolean method1() {...}
boolean method2() {...}
boolean method3() {...}
...
void callAll() {
if(method1()) {
if(method2() {
if(method3() {
...
}
}
} else {
error();
}
}
}
There has to be an else for all the ifs.
Is there a better way of handling this scenario?
I would just do it like this:
void callAll(){
if(method1() && method2() && method3()){
// all passed
} else {
error();
}
}
Java short-circuits the && logical operation so failure in a previous method here will prevent running the next one.
If in error() you need to know which of the methods failed, you could declare an error message field for storing the information within the class and set its value corresponding the failure:
private String errorMessage;
//...
boolean method2() {
// something went wrong
errorMessage = "Failed to do method2 stuff";
}
Are more elegant way to achieve the same would be to use the Chain of responsibility design pattern and encapsulate the boolean methods in their own handler objects. Doing this would however require more refactoring to the code you currently have and more information about your specific use case.
It's easy enough to write your own varargs method to do this:
public static void run(Supplier<Boolean>... methods) {
for (Supplier<Boolean> method : methods) {
if (!method.get()) return;
}
}
Sample usage:
run(this::method1, this::method2, this::method3);
You can use some form of Observable pattern for these kind of thins too. In most normal cases it seems a bit silly to implement it but otherwise a great way to decouple code from control structures if you have a lot of these. Note that ObservableBoolean is an Android class, but just showing the logic here:
ObservableBoolean a = new ObservableBoolean();
ObservableBoolean b = new ObservableBoolean();
public void call() {
a.addOnPropertyChangedCallback(new OnPropertyChangedCallback() {
#Override
public void onPropertyChanged(android.databinding.Observable sender, int propertyId) {
method2();
}
});
b.addOnPropertyChangedCallback(new OnPropertyChangedCallback() {
#Override
public void onPropertyChanged(android.databinding.Observable sender, int propertyId) {
//..you end the "chain" here
}
});
method1();
}
void method1() {
if(true) {
a.set(true);
}
else {
b.set(false);
}
}
void method2() {
if(true) {
b.set(true);
}
else {
b.set(false);
}
}
I use this technique - although some would find it odd.
boolean method1() {
System.out.println("method1");
return true;
}
boolean method2() {
System.out.println("method2");
return false;
}
boolean method3() {
System.out.println("method3");
return true;
}
void callAll() {
boolean success = method1();
success = success ? method2() : success;
success = success ? method3() : success;
if (success) {
System.out.println("Success");
} else {
System.out.println("Failed");
}
}
I could suggest you to use RX approach, with rxjava it should look like
public boolean test1() {
Log.d("TESTIT", "test1 called");
return true;
}
public boolean test2() {
Log.d("TESTIT", "test2 called");
return true;
}
public boolean test3() {
Log.d("TESTIT", "test3 called");
return false;
}
public boolean test4() {
Log.d("TESTIT", "test4 called");
return true;
}
public boolean elseMethod(boolean result) {
if (result) return true;
else {
Log.d("TESTIT", "ELSE");
}
return false;
}
public void chainedCallback() {
Observable.just(test1())
.filter(this::elseMethod)
.flatMap(aBoolean -> Observable.just(test2()))
.filter(this::elseMethod)
.flatMap(aBoolean -> Observable.just(test3()))
.filter(this::elseMethod)
.flatMap(aBoolean -> Observable.just(test4()))
.filter(this::elseMethod)
.subscribe();
}
call for chainedCallback() will print
test1 called
test2 called
test3 called
ELSE
You define a class that holds an action (calling one of the methods) and with a corresponding failure handler (the else block of an if call)
public static class ActionWithFailureHandler {
private Supplier<Boolean> action;
private Runnable failureHandler;
public ActionWithFailureHandler(Supplier<Boolean> action, Runnable failureHandler) {
this.action = action;
this.failureHandler = failureHandler;
}
//Getters for the instance variables
}
You make a list of the above and call each of the actions till one of the following happens
One of the actions fails (i.,e one of the method returns false). In that case, you need to execute the failureHandler corresponding to that action.
All actions pass. In this case, execute the successHandler (the logic that you execute when all methods return true).
private static void callAll(List<ActionWithFailureHandler> actionWithFailureHandlers, Runnable successHandler) {
actionWithFailureHandlers.stream()
.filter(actionWithFailureHandler -> !actionWithFailureHandler.getAction().get())
.findFirst() //Find first failing action
.map(ActionWithFailureHandler::getFailureHandler)
.orElse(successHandler)
.run(); //You might be running either the successHandler or the failureHandler for the first failed action
}
Driver code:
public static void main(String[] args) {
Test test = new Test();
List<ActionWithFailureHandler> actionWithFailureHandlers = com.google.common.collect.ImmutableList.of(
new ActionWithFailureHandler(test::method1, () -> System.out.println("Method 1 returned false")),
new ActionWithFailureHandler(test::method2, () -> System.out.println("Method 2 returned false")),
new ActionWithFailureHandler(test::method3, () -> System.out.println("Method 3 returned false"))
);
callAll(actionWithFailureHandlers, () -> System.out.println("All returned true"));
}
Exception firstly comes to my mind, but see the link below to learn more about its performance hit.
Original answer. I would do..
public class MyException extends Exception
{
}
public void doAll()
{
try
{
method1();
method2();
method3();
}catch (MyException e)
{
error();
}
}
And let's assume that method1, method2, and method3 throws MyException when it fails.
Though it does not fit your question, it is a good pattern to use Exceptions.
public class Helper
{
public Helper(Method m)
{
this.method=m;
}
public void Do() throws MyException
{
if(method.invoke()==false)
throw new MyException ();
}
}
Using this class,
public void doAll()
{
Helper [] helpers={new Helper(this::method1), new Helper(this::method2), new Helper (this::method3)};
try
{
for(Helper helper:helpers)
{
helper.Do();
}
}catch (MyException e)
{
error();
}
}
But
according to the comment of #dilix and the link, it can be a performance-expensive strategy.
So let's use them only for their purpose.

mockito verifying polymorphic methods

I'm trying to verify call to a polymorphic method using mockito, and am confused about what the best way forward is.
Example Class
public class Library {
public boolean foo() {
return true;
}
public boolean foo(String s) {
return true;
}
public boolean foo(Integer s) {
return true;
}
}
The test class using mockito(ignore the fact that in LibraryTest class, Library is not the class-under-test, but rather I'm mocking it)
import org.junit.Test;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
public class LibraryTest {
#Test public void testFoo1() {
Library mockLibrary = mock(Library.class);
mockLibrary.foo("Hi");
verify(mockLibrary).foo(any());
}
#Test public void testFoo2() {
Library mockLibrary = mock(Library.class);
verify(mockLibrary, never()).foo(any());
}
}
So both the verify statements don't compile, with the error "The method foo(String) is ambiguous for the type Library".
The error kind of makes sense, any() tries to return a captor based on the type of the argument but the argument could be Integer or String or void.
What I want to achieve is that in both the tests, a call to any of the foo methods is counted by the verify. In other words the first verify call should succeed if I called any of the foo methods and the second verify should fail if I call any of the foo methods.
Is there a way to make this happen?
You can use isA matcher
verify(mockLibrary).foo(isA(Integer.class));
verify(mockLibrary).foo(isA(String.class));
and btw use Mockito.spy instead of Mockito.mock when you only want to see if some methods have been called on the class under test
EDIT with example (written in few minutes, don't mind the code:)) based on the new op details.
public static class Library {
public boolean foo() {
return true;
}
public boolean foo(String s) {
return true;
}
public boolean foo(Integer s) {
return true;
}
public String x(){
return "";
}
public void y(){
return;
}
}
public static class ResponseProvider {
public boolean result;
}
#Test
public void testFoo1() {
final ResponseProvider provider = new ResponseProvider();
provider.result = false;
Library lib = Mockito.mock(Library.class, new Answer<Object>() {
#Override
public Object answer(InvocationOnMock invocation) throws Throwable {
if ((invocation.getMethod().getName().equals("foo"))) {
provider.result = true;
return true;
}
return invocation.callRealMethod();
}
});
//lib.foo();
//lib.x();
//lib.y();
assertTrue(provider.result);
}
First of all, You are mocking the same class which you are testing. Thats not advisable but still
Its giving this exception because mockito doesn't know which method you are verifying among the foo methods. If you want to make sure the method you invoked is called with the right value, you can either use isA(ClassName) matcher or use can use ArgumentCaptor.
Example of ArgumentCaptor
#Test public void testFoo1() {
Library mockLibrary = mock(Library.class);
mockLibrary.foo("Hi");
ArgumentCaptor<String> stringCaptor = ArgumentCaptor.forClass(String.class);
verify(mockLibrary).foo(stringCaptor.capture());
String actualArgument = stringCaptor.getValue();
assertEquals(actualArgument, "Hi");
}

How to modify/decorator an object returned from 3rd party API using javassist/CGLib

I have an 3rd party API call which returns the following object:
public class A {
protected void common() {
System.out.println("common is called in A");
}
public void test1() {
common();
System.out.println("test1 is called in A");
}
public void test2() {
common();
System.out.println("test2 is called in A");
}
}
But I'd like to modify its behavior like the following ModifiedA shows:
public class ModifiedA extends A {
#Override
protected void common() {
super.common();
System.out.println("common is called in ModifiedA");
}
}
So what I am trying to do is:
A a = 3rdPartyAPI_call();
//
// Now I'd like to get a ModifiedA which has changed common() behavior.
//
How to use javassist/CGLIB to accomplish this ?
One easy way may be to like this:
public class ModifiedA extends A {
private A a;
public ModifiedA(final A a) {
this.a = a;
}
//
// Override every public method in A
//
#Override
protected void common() {
super.common();
System.out.println("common is called in ModifiedA");
}
}
But since A's definition comes from 3rd party and is very complex and may change, so I'd like to use a proxy to do this?
Thanks for your comments in adavance.
You can use CGLib to implement a delegator pattern without having to override all the methods. There are a few different approaches to implement this depending on style but here is one similar to your example.
You can wrap the instance using a cglib Enhancer:
public static <T> T wrapInstance(final T original) {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(original.getClass());
enhancer.setCallback(new MethodInterceptor() {
#Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
Object returnValue = proxy.invoke(original, args);
if (method.getName().equals("common")) {
System.out.println("common is called");
}
return returnValue;
}
});
return (T) enhancer.create();
}
eclps post will fullfill your requirement and it works.I want to add some more code to eclps code.
Adding filter which give index zero for common method and rest all method to One. MethodInterceptor callback will intercept only common method and rest all method use NoOp intercetor(which will call super class apis).This way filtering is not happening for every method call.
public static <T> T wrapInstance(final T original) {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(original.getClass());
enhancer.setCallbackFilter(new CallbackFilter() {
#Override
public int accept(Method method) {
if (method.getName().equals("common")) {
return 0;
}
return 1;
}
});
enhancer.setCallbacks(new Callback[]{new MethodInterceptor() {
#Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
// only common method will intercept this call back.
return proxy.invoke(this, args);
}
}, NoOp.INSTANCE});
return (T) enhancer.create();
}

Mockito Allow different argument types to mock overloaded method

For JUnit testing I want to mock an overloaded method. There is no need to implement several methods in the mockbuilder though.
I want to do something like this:
Mockito.when(mock.getSomeInfo(Mockito.any(ArgumentType1.class) OR Mockito.any(ArgumentType2.class), Mockito.any(ArgumentType3.class))).then(new Answer<AnswerType>() {..}
I know it doesn't work with the OR statement, but is there another way to do this in Mockito?
As mentioned on the mockito github:
It looks like this has to do with calling when the second time. I couldn't find what was going on, but the second when doesn't attempt to add the mock logic, it just calls the method. Replacing with doReturn().when() works.
doReturn(expected1).when(overloadedMethods).getString(any());
doReturn(expected2).when(overloadedMethods).getString(any(), any());
https://github.com/mockito/mockito/issues/1496#issuecomment-423310950 DavidTanner
You can do this with a custom matcher.
Warning: Be reasonable with using complicated argument matching, especially custom argument matchers, as it can make the test less readable. Sometimes it's better to implement equals() for arguments that are passed to mocks (Mockito naturally uses equals() for argument matching). This can make the test cleaner.
public class TypeOrMatcher extends ArgumentMatcher<Object> {
private final List<Class<?>> clazzes;
public TypeOrMatcher(Class<?>...clazzes) {
this.clazzes = new ArrayList<Class<?>>(clazzes);
}
public boolean matches(Object actual) {
if (actual == null) {
return false;
}
Class<?> actualClass = actual.getClass();
for (Class<?> clazz : clazzes) {
if (clazz.isAssignableFrom(actualClass) {
return true;
}
}
return false;
}
}
TypeOrMatcher isTypeOneOrTwo = new TypeOrMatcher(
ArgumentType1.class, ArgumentType2.class);
Some mockObj = mock(Some.class);
when(mockObj.someMethod(argThat(isTypeOneOrTwo), any(ArgumentType3.class))
.thenReturn(true);
You can pre-create the answer object you want to return and then return this answer.
class A {
public int test(final String s) {
return 0;
}
public int test(final int i) {
return 0;
}
}
And in the test method:
public void testAtest() {
final A a = Mockito.mock(A.class);
final Answer<Integer> answer = new Answer<Integer>() {
#Override
public Integer answer(final InvocationOnMock invocation) throws Throwable {
return 0;
}
};
Mockito.when(a.test(Matchers.anyInt())).then(answer);
Mockito.when(a.test(Matchers.anyString())).then(answer);
}
For an example I have a service class that will be called from testing method:
public interface AService{
public ReturnObject addNewItem(String param1, String param2);
public ReturnObject addNewItem(String param1, String param2, boolean isOk);
}
Have a method in MainServiceImpl class will call an overloaded method like below:
#Service("mainService")
public class MainServiceImpl implements MainService {
#Autowired
private AService aService;
public ReturnObject saveItem(String itemName, String itemCode){
return aService.addNewItem(itemName, itemCode);
}
}
So when we have to write unit test for saveItem the method which already calls to overloaded method as addNewItem, if you had used normal way to create a mock then you answer will not return what you want in answer object return.
#RunWith(PowerMockRunner.class)
#PrepareForTest({ })
public class ItemTest{
#Test
public void testSaveItem() throws Exception {
//create a real object of MainServiceImpl
MainServiceImpl mainService = new MainServiceImpl();
//create a normal way for a mocking object
AService aService = Mockito.mock(AService.class);
// Add mock object to MainServiceImpl instance
ReflectionTestUtils.setField(mainService, "aService", aService);
//Mock when aService call to addNewItem() method
PowerMockito.when(aService , "addNewItem", Mockito.anyString(), Mockito.anyString()).then(new Answer<ReturnObject>() {
#Override
public ReturnObject answer(InvocationOnMock invocation) throws Throwable {
return new ReturnObject("saveOK");
}
});
ReturnObject returnObj = mainService.saveItem("Book", "Code123");
Assert.assertNotNull(returnObj);
}
}
Try to replace testSaveItem above by with testSaveItem below then success:
#Test
public void testSaveItem() throws Exception {
//create a real object of MainServiceImpl
MainServiceImpl mainService = new MainServiceImpl();
//create a special way for a mocking object by add
//the answer at create the mock object
final Answer<ReturnObject> answer = new Answer<ReturnObject>() {
#Override
public ReturnObjectanswer(final InvocationOnMock invocation) throws Throwable {
return new ReturnObject("saveOK");
}
};
AService aService = Mockito.mock(AService.class, answer);
// Add mock object to MainServiceImpl instance
ReflectionTestUtils.setField(mainService, "aService", aService);
//Mock when aService call to addNewItem() method
PowerMockito.when(aService , "addNewItem", Mockito.anyString(), Mockito.anyString()).then(new Answer<ReturnObject>() {
#Override
public ReturnObject answer(InvocationOnMock invocation) throws Throwable {
return new ReturnObject("saveOK");
}
});
ReturnObject returnObj = mainService.saveItem("Book", "Code123");
Assert.assertNotNull(returnObj);
}

Conditional matching in Google Guice

I have a MethodInterceptor bound to methods in a class in order to do some simple logic before on the data before the class gets to touch it.
However, teh class itself makes calls to some of its own intercepted methods, but at that point I don't need to re-run that logic anymore.
public class MyModule extends AbstractModule {
#Override
public void configure() {
bindInterceptor(Matchers.any(), Matchers.annotatedWith(MyAnnotation.class), new MyInterceptor());
}
}
public class MyInterceptor implements MethodInterceptor {
#Override
public Object invoke(MethodInvocation invocation) throws Throwable {
// logic
}
}
public MyClass {
#MyAnnotation
void foo() {
bar();
}
#MyAnnotation
void bar() {
}
}
Is there a way for the call for bar within foo to not be itnercepted?
To be honest, the easiest solution is to simply avoid the problem by never calling another public/annotated method of the same class from within the class:
public class MyClass {
#MyAnnotation
public void foo() {
doBar();
}
#MyAnnotation
public void bar() {
doBar();
}
private void doBar() {
//doesn't go through interceptor
}
}
If for some reason that's not an option, then you might look at this approach. More expressive AOP libraries like AspectJ give you a greater level of flexibility for defining a pointcut.
In Guice, the pointcut is simply a method with an annotation belonging to an instance instantiated by Guice. So this logic has to be moved to the interceptor itself.
One approach for doing so might be to use a ThreadLocal to track entries into the interceptor. Extending something like this might be a start:
public abstract class NonReentrantMethodInterceptor implements MethodInterceptor {
private final ThreadLocal<Deque<Object>> callStack = new ThreadLocal<>();
#Override
public final Object invoke(MethodInvocation invocation) throws Throwable {
Deque<Object> callStack = this.callStack.get();
if (callStack == null) {
callStack = new LinkedList<>();
this.callStack.set(callStack);
}
try {
return invokeIfNotReentrant(callStack, invocation);
} finally {
if (callStack.isEmpty()) {
this.callStack.remove();
}
}
}
private final Object invokeIfNotReentrant(Deque<Object> callStack, MethodInvocation invocation) throws Throwable {
Object target = invocation.getThis();
if (callStack.isEmpty() || callStack.peek() != target) {
//not being called on the same object as the last call
callStack.push(target);
try {
return doInvoke(invocation);
} finally {
callStack.pop();
}
} else {
return invocation.proceed();
}
}
protected abstract Object doInvoke(MethodInvocation invocation) throws Throwable;
}
This uses a thread local stack to track the stack of calls into the interceptor. When the last call into this interceptor targeted the same object, it calls proceed() and bypasses the interceptor. When this is the first call into the interceptor, or if the last call was not targeting the same object, it applies the interceptor.
Then the actual logic you would want to apply when the interceptor is active would go into doInvoke().
Example usage:
public class NonReentrantTester {
public static void main(String[] args) {
Injector injector = Guice.createInjector(new Module());
MyClass instance = injector.getInstance(MyClass.class);
instance.foo();
}
static class Module extends AbstractModule {
#Override
protected void configure() {
bindInterceptor(Matchers.any(), Matchers.annotatedWith(PrintsFirstInvocation.class),
new PrintsFirstInvocationInterceptor());
}
}
public static class MyClass {
#PrintsFirstInvocation
void foo() {
bar();
}
#PrintsFirstInvocation
void bar() {
}
}
public static class PrintsFirstInvocationInterceptor extends NonReentrantMethodInterceptor {
#Override
protected Object doInvoke(MethodInvocation invocation) throws Throwable {
System.out.println(invocation.getMethod());
return invocation.proceed();
}
}
#BindingAnnotation
#Target({FIELD, PARAMETER, METHOD})
#Retention(RUNTIME)
public #interface PrintsFirstInvocation {
}
}

Categories

Resources