how to mock a parametrized constructor? - java

I've the following class. It has the code to connect to SAP in its constructor. There is an abstract method(the subclasses define the implementation) which I want to mock.
public abstract class BapiExecutor {
...
public BapiExecutor(final SapConnectionInfo connectionInfo)
throws java.lang.Exception {
if (!validConnectorData()) {
throw new IllegalArgumentException(
"Does not have valid data to connect to SAP");
}
initializeState(connectionInfo);
}
public abstract Object execute() throws Exception ;
....
}
The unit I want to test is :
I want to mock the call to execute() method.
private String invokeBapiToAddAssociation(Map associationMap,
SapConnectionInfo connectionInfo) {
EidCcBapiExecutor executor = null;
String bapiExecutionResult = null;
try {
executor = new EidCcBapiExecutor(connectionInfo, associationMap);
bapiExecutionResult = (String) executor.execute();
} catch (Exception e) {
e.printStackTrace();
throw new CcGenericException(
"Exception occurred while invoking the EID-CC Association BAPI executor!",
e);
}
return bapiExecutionResult;
}
Any frameworks in Java that supports the mocking of parametrized constructors?
i just want to avoid connecting to SAP in the constructor.

JMock with the ClassImposteriser can do that, as can most good mocking frameworks.
The ClassImposteriser creates mock
instances without calling the
constructor of the mocked class. So
classes with constructors that have
arguments or call overideable methods
of the object can be safely mocked.

You can simply create the mock class, subclassing the abstract BapiExecutor class, and implementing the behavior you want in execute() (or any other method). You don't need to revert to a framework here.
Could you please elaborate about what the blocking point is ?

I expect that the EidCcBapiExecutor extends from BapiExecutor.
public class EidCcBapiExecutor extends BapiExecutor {
...
}
Than you could create the Mockup class for testing a specific method like:
public class EidCcBapiExecutorMockup extends EidCcBapiExecutor{
public EidCcBapiExecutorMockup (final SapConnectionInfo connectionInfo){
super(connectionInfo);
}
public Object execute() throws Exception {
// You mockup code
}
}
If you want to test the constructor you can create the class like:
public class EidCcBapiExecutorMockup extends EidCcBapiExecutor{
public EidCcBapiExecutorMockup (){
super(new SapConnectionInfo());
}
}
The object you place in the Constructor could be created in the setUp method of you JUnit test!

Related

Design Approach and Using Reflection to run methods in Java

I have a question. I have multiple classes in a package: Let's say package is
com.myPackage.first
And this package has the following classes:
firstGood
secondGood
thirdBad
fourthGood
Each of these classes have a method with the same name but different implementation. So say each have a one particular function called:
public void runMe(){
}
For now I want to come up with a way to given a class name, it'll go inside the class and run that particular method.
So conceptually, my method will look like those:
ArrayList<Class> classList ; // where classList is a list of classes I want to run
public void execute(){
for(Class c : classList){
// Go inside that class, (maybe create an intance of that class) and run the method called run me
}
}
or
public void execute(Class c, String methodToRun){
for(Class c : classList){
// Go inside that class, (maybe create an intance of that class) and run the method called run me
}
}
For now. what I have been able to do is get the name of the classes I want to run the
runMe()
method. So I have been able to come with a way to get the arraylist of classes I want to run. So what I need help with is coming up with a method such that it takes a class name and run the method I want it to. Any help is appreciated. Thanks
I suggest having a look at Class.forName ( ... ) to get the class object, Class.newInstance(); if your classes have a default constructor (or Class.getDeclaredConstructor(...) otherwise) to create a new instance and then Class.getDeclaredMethod( ... ) to find the method and invoke it.
All of this without any regard if your idea is really a good one, since I really didn't quite understand WHY you want to do what you want to do...
interface Me {
void runMe();
}
Then let all classes implement Me.
And have a list of Mes
List<Class<Me>> ...
Then
void test(Class<Me> cl) {
Me me = cl.newInstance();
me.runMe();
}
My adage is always use reflection to solve a problem - now you have two problems. In view of that have you considered a simple pattern like this:
interface Runner {
public void runMe();
}
static abstract class BaseRunner implements Runner {
public BaseRunner() {
// Automagically register all runners in the RunThem class.
RunThem.runners.add(this);
}
}
class FirstGood extends BaseRunner implements Runner {
#Override
public void runMe() {
System.out.println(this.getClass().getSimpleName() + ":runMe");
}
}
class SecondGood extends BaseRunner implements Runner {
#Override
public void runMe() {
System.out.println(this.getClass().getSimpleName() + ":runMe");
}
}
static class RunThem {
static final Set<Runner> runners = new HashSet<>();
static void runThem() {
for (Runner r : runners) {
r.runMe();
}
}
}
public void test() {
Runner f = new FirstGood();
Runner s = new SecondGood();
RunThem.runThem();
}
Here all of your runMe objects extend a base class whose constructor installs the object in a Set held by the class that calls their runMe methods.
inline
void execute() throws Exception{
for (Class<?> c : classesList)
{
//If you don't already have an instance then you need one
//note if the method is static no need for any existing instance.
Object obj = Class.forName(c.getName());
// name of the method and list of arguments to pass
Method m = c.getDeclaredMethod(methodName,null);
//method accessibility check
if(!m.isAccessible())
m.setAccessible(true);
//invoke method if method with arguements then pass them as new Object[]{arg0...} instead of null
//if method is static then m.innvoke(null,null)
m.invoke(obj, null);
}
}
I would recommend using an Interface that defines the runMe() method and then have all your classes implement that interface. Then you would have a list of this Interface:
List<MyInterface> classes = new ArrayList<MyInterface>();
Then you could easily iterate over it and invoke "runMe()" on all of them or if you only want to invoke it for instances of a certain class you could do it like this:
public void execute(Class classForWhichToExecute) {
for (MyInterface myInterface : classes) {
if (classForWhichToExecute.isAssignableForm(myInterface)) {
myInterface.runMe();
}
}
}
Of course this wouldn't work if your method is a static method - so adding more information from your side would help.
I would suggest to use an interface with a common method to override in each class. So that any class can be casted to interface and use its method to execute the method.
interface GoodAndBad{
public void runMe();
}
Implemented class
class FirstGood implements GoodAndBad{
#override
public void runMe(){
// Code to be executed
}
}
You can use execute() method as follows
public void execute(List<GoodAndBad> classList){
for(GoodAndBad c : classList){
c.runMe();
// Go inside that class, (maybe create an intance of that class) and
// run the method called run me
}
}
Change the Class to GoodAndBad interface to change the other method too.
This is loosely coupling objects to support favor over composition in Java Object Oriented Design Patterns.
Never use Strings of method names to execute a method at anytime. There are plenty of other cool solutions for that using design patterns.

super class constructor in custom exception in java

I am learning features in java including exceptions. I am writing a custom exceptions. Here is what i am doing :custom exception class:
public class ServiceException extends Exception {
private String customMessage;
public ServiceException(String customMessage) {
super(customMessage);
this.customMessage = customMessage;
}
}
Main class:
public class Main {
public static void main(String[] args) {
try {
new Main().test();
} catch (Exception e) {
System.out.println("the exception message is " + e.getMessage());
}
}
public void test() throws ServiceException {
try {
int i = 1 / 0;
} catch (Exception e) {
throw new ServiceException(e.getMessage());
}
}
}
This much i know:
if super class constructor is not invoked in the custom exception class, the message set in the custom exception is not passed to the Exception class. But if i have a method public String getMessage in my custom exception class, even if the super is not invoked, that message is printed . Sorry if this is a naive question. But i am failing to understand he concept. Could come one help clear the concept ?
In main where you are catching the error, you are basically assigning a ServiceException object to a Exception reference, i.e. assigning derived class object to base class reference, so if the derived class has overridden the method, it will get called.
the e.message() being called is from ServiceException not Exception, you are right, no data is being passed when you are not calling super, data is inside ServiceException class only and the function invoked is also from ServiceException class.
That is because you are supplying it. You are passing e.getMessage() to your constructor as the only argument, customMessage. You then pass customMessage to its parent's constructor that takes a String, Exception(String). In doing so, you are giving it the message to use for serviceExceptionInstance.getMessage(). Instead, do not pass the customMessage to its parent (use super();, which is implied if no call to a parent constructor is given and a no-arg, parent constructor exists). Then the message will be null as it is not supplied.
In other words:
new ServiceException(e.getMessage());
Creates a new ServiceException with the message from e. You pass that message to Exception, ServiceException's parent.
super(customMessage);
In doing so, you use the single argument, String-based constructor of Exception. Passing a value to that constructor implies that you want it used when callers invoke getMessage. To avoid doing this, call a different parent constructor, or none at all (calling none is technically not possible, and it will implicitly do super(); for you):
public ServiceException(String customMessage)
{
this.customMessage = customMessage;
}
This will call super(); for you, which means that the parent class has no message to send, and by not overriding getMessage() yourself, then it will return its default value (null). To be clear, Exception itself extends from Throwable, which is really the class providing this functionality, but it all stems from how you work with Exception as it serves as a pass-thru to Throwable.

How to override abstract base class behavior with Mockito

I have code like this:
class Outer {
private External external;
class MyCallback extends ExternalAbstractCallback {
void somethingHappened() { if (super.someCondition()) { ... }; }
}
public Outer() {
external = ...;
}
public setExternal(External e) { external = e; } // test support
public void doIt() {
external.setCallback(new MyCallback());
external.doSomething();
}
}
I want to test the behavior of MyCallback when doIt() is called. I use Mockito to create a mock External and can then get external.doSomething() to execute the MyCallback.somethingHappened. Can I control the return value of the call to super.someCondtion at line 4? Can I rearrange the implementation to improve testability? The implementations of External and ExternalAbstractCallback cannot be changed.
Your problem is that you can't mock new with Mockito. In a nutshell, code that you want to test should never call new.
Workarounds:
Add a getter/setter for the callback.
Move new MyCallback() into a new protected helper method which you override in your test

Mockito bypass static method for testing

I need to test handleIn() method using Mockito.
However the code need to call this legacy code Util.getContextPDO which is a static method.
Note that in testing environment this Util.getContextPDO is always returns Exception, and I intend to bypass this Util.getContextPDO() by always return a dummy IPDO.
public class MyClass {
public IPDO getIPDO()
{
return Util.getContextPDO(); // note that Util.getContextPDO() is a static, not mockable.
}
public String handleIn(Object input) throws Throwable
{
String result = "";
IPDO pdo = getIPDO();
// some important business logic.
return result;
}
}
Initially I thought this achieveable by using spy() of the class "MyClass", so I can mock the return value of getIPDO(). Below is my initial effort using spy ()
#Test
public void testHandleIn() throws Exception
{
IPDO pdo = new PDODummy();
MyClass handler = new MyClass ();
MyClass handler2 = spy(handler);
when(handler2.getIPDO()).thenReturn(pdo);
PDOUtil.setPDO(pdo, LogicalFieldEnum.P_TX_CTGY, "test123");
IPDO pdoNew = handler2.getIPDO();
Assert.assertEquals("test123,(PDOUtil.getValueAsString(pdoNew, LogicalFieldEnum.P_TX_CTGY)));
}
However the when(handler2.getIPDO()).thenReturn(pdo); is throwing the Exception that I want to avoid ( because handler2.getIPDO() ) seems to call the real method.
Any idea on how to test this part of code?
A good technique for getting rid of static calls on 3rd party API is hiding the static call behind an interface.
Let's say you make this interface :
interface IPDOFacade {
IPDO getContextPDO();
}
and have a default implementation that simply calls the static method on the 3rd party API :
class IPDOFacadeImpl implements IPDOFacade {
#Override
public IPDO getContextPDO() {
return Util.getContextPDO();
}
}
Then it is simply a matter of injecting a dependency on the interface into MyClass and using the interface, rather than the 3rd party API directly :
public class MyClass {
private final IPDOFacade ipdoFacade;
public MyClass(IPDOFacade ipdoFacade) {
this.ipdoFacade = ipdoFacade;
}
private IPDO getIPDO() {
return ipdoFacade.getContextPDO();
}
public String handleIn(Object input) throws Throwable
{
String result = "";
IPDO pdo = getIPDO();
someImportantBusinessLogic(pdo);
return result;
}
...
}
In your unit test, you can then easily mock your own interface, stub it any way you like and inject it into the unit under test.
This
avoids the need to make private methods package private.
makes your tests more readable by avoiding partial mocking.
applies inversion of control.
decouples your application from a specific 3rd party library.
Changed my testing to :
#Test
public void testHandleIn() throws Exception
{
IPDO pdo = new PDODummy();
MyClass handler = new MyClass ();
MyClass handler2 = spy(handler);
doReturn(pdo ).when( handler2 ).getIPDO();
PDOUtil.setPDO(pdo, LogicalFieldEnum.P_TX_CTGY, "test123");
IPDO pdoNew = handler2.getIPDO();
Assert.assertEquals("test123,(PDOUtil.getValueAsString(pdoNew, LogicalFieldEnum.P_TX_CTGY)));
}
Solved after reading Effective Mockito.
when(handler2.getIPDO()).thenReturn(pdo);
Will actually call the method and then return pdo regardless.
Whereas:
doReturn(pdo).when(handler2).getIPDO();
Will return pdo without calling the getIPDO() method.

Delay EasyMock verification

I'm using EasyMock to create mock objects for JUnit testing in Java. I create a mock object and pass it to another thread where it expects methods to be called. In the other thread, the calls are enclosed in a try/catch(Throwable) block, so when an unexpected call occurs on the mock and it thus throws AssertionError, that error is caught by the catch block and treated. So, even though an unexpected call occurred, the test passes.
In order to have the test fail as expected, I would like to delay all verification of calls to the EasyMock.verify(mock) call made in the test-runner thread at the end. Is this possible and how?
I'm not sure about how to do this with EasyMock, but this behavior is possible with Mockito because verification assertions can be specified at the end of the test.
The correct solution I'd guess is to stop catching Throwable. Doing so catches all Errors as you're finding, which can be quite dangerous... are you absolutely positively 100% sure you need to catch Throwable? Why?
(If it turns out you do, you could catch AssertionError specifically and rethrow it. But that's ugly!)
Try using nice mocks:
http://easymock.org/EasyMock2_5_2_Documentation.html
"Nice Mocks
On a Mock Object returned by createMock() the default behavior for all methods is to throw an AssertionError for all unexpected method calls. If you would like a "nice" Mock Object that by default allows all method calls and returns appropriate empty values (0, null or false), use createNiceMock() instead. "
Default values will be returned for unexpected calls instead of throwing AssertionError, but you can still verify them with the verify() method (in which case the AssertionErrors will be thrown)
As #deterb suggested, it's possible with Mockito but you have to know the method name or you have to set expectations for every method. Here is an example:
The mocked interface:
public interface MyInterface {
void allowedMethod();
void disallowedMethod();
}
The user class which catches AssertionError:
public class UserClass {
public UserClass() {
}
public static void throwableCatcher(final MyInterface myInterface) {
try {
myInterface.allowedMethod();
myInterface.disallowedMethod();
} catch (final Throwable t) {
System.out.println("Catched throwable: " + t.getMessage());
}
}
}
And the Mockito test:
#Test
public void testMockito() throws Exception {
final MyInterface myInterface = mock(MyInterface.class);
UserClass.throwableCatcher(myInterface);
verify(myInterface, never()).disallowedMethod(); // fails here
}
The same is possible with EasyMock but it needs some work:
#Test
public void testEasyMock() throws Exception {
final AtomicBoolean called = new AtomicBoolean();
final MyInterface myInterface = createMock(MyInterface.class);
myInterface.allowedMethod();
myInterface.disallowedMethod();
final IAnswer<? extends Object> answer = new IAnswer<Object>() {
#Override
public Object answer() throws Throwable {
System.out.println("answer");
called.set(true);
throw new AssertionError("should not call");
}
};
expectLastCall().andAnswer(answer).anyTimes();
replay(myInterface);
UserClass.throwableCatcher(myInterface);
verify(myInterface);
assertFalse("called", called.get()); // fails here
}
Unfortunately you also have to know the method names here and you have to define expectations like myInterface.disallowedMethod() and expectLastCall().andAnswer(answer).anyTimes().
Another possibility is creating a proxy with the Proxy class (with a custom InvocationHandler) and using it as a mock object. It definitely needs more work but it could be the most customizable solution.
Finally don't forget that it's also possible to create a custom implementation with or without delegation to the EasyMock mock object. Here is one with delegation:
public class MockedMyInterface implements MyInterface {
private final MyInterface delegate;
private final AtomicBoolean called = new AtomicBoolean();
public MockedMyInterface(final MyInterface delegate) {
this.delegate = delegate;
}
#Override
public void allowedMethod() {
delegate.allowedMethod();
}
#Override
public void disallowedMethod() {
called.set(true);
throw new AssertionError("should not call");
}
public boolean isCalled() {
return called.get();
}
}
And the test for it:
#Test
public void testEasyMockWithCustomClass() throws Exception {
final MyInterface myInterface = createMock(MyInterface.class);
myInterface.allowedMethod();
final MockedMyInterface mockedMyInterface =
new MockedMyInterface(myInterface);
replay(myInterface);
UserClass.throwableCatcher(mockedMyInterface);
verify(myInterface);
assertFalse("called", mockedMyInterface.isCalled()); // fails here
}

Categories

Resources