Friday 6 December 2013

Installing Ant on Mac OS Mavericks

The easiest way to install ant on mac osx (Mavericks) is by installing homebrew and to run the below commands...

To install homebrew, you'll need to open terminal and run below command:

ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go/install)"

Once homebrew is installed the following commands can be executed in your terminal window

macbook:~ fd$ brew update
~$ brew tap homebrew/dupes
~$ brew install ant

to check that it is installed properly
~$ ant -version
Apache Ant(TM) version 1.9.2 compiled on July 8 2013 


Reference: http://blog.xk72.com/post/53124504531/homebrew-dupes-installing-ant-on-mac-os-x

Thursday 8 August 2013

PowerMock: Sample usage of expectNew and expectStrictNew...

Depencencies


  • 'org.powermock:powermock-api-easymock:1.5.1'
  • 'org.powermock:powermock-module-junit4:1.5.1'
  • 'org.easymock:easymock:3.1'

PowerMockDemo

public class PowerMockDemo {
 
  public void someMethod(String someArgument){
  
  ClassA a = new ClassA();
  ClassB b = new ClassB();
  ClassC c = new ClassC();

  c.setFirst(a);
  c.setSecond(b);
  c.execute(someArgument);
 }
}

PowerMockDemoTest

Below is the unit test to test the PowerMockDemo.java.

import static org.powermock.api.easymock.PowerMock.*;
import org.easymock.EasyMock;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest(PowerMockDemo.class)
public class PowerMockDemoTest {
 
 private PowerMockDemo toTest;
 
 @Test
 public void testExcuteMethod() throws Exception {
  
  toTest = new PowerMockDemo();
 
  ClassA mockA = createMock(ClassA.class);
  expectNew(ClassA.class).andReturn(mockA);
  
  ClassB mockB = createMock(ClassB.class);
  expectNew(ClassB.class).andReturn(mockB);
  
  ClassC mockC = createStrictMock(ClassC.class);
  expectNew(ClassC.class).andReturn(mockC);
  
  mockC.setFirst(mockA);
  expectLastCall().once();
  
  mockC.setSecond(mockB);
  expectLastCall().once();
  
  mockC.execute(EasyMock.anyObject(String.class));
  expectLastCall().once();
  
  replayAll();
  
  // run the test code
  toTest.someMethod("Hello PowerMock");
  
  verifyAll();
 }
}

In the unit test above we asserted several things when the PowerMockDemo#someMethod(String) is invoked.

PowerMock.createMock() - is used to instantiate a new mock object.

PowerMock.expectNew() - is used to specify that a new a new instance is expected and when it is invoked the specified mock object is returned.

PowerMock.createStrictMock() - is also used to create a new mock object. The only difference is that, when compared to createMock(), the order of method invocations on the mock object is asserted.

References:

Wednesday 31 July 2013

Mockito: Using ArgumentCaptor to capture argument values for further assertions.

Sometimes there are situations where I need to assert the arguments that are passed into a service, but that arguments are instantiated within a method that you want to test and in which case it cannot be asserted using a plain JUnit test.

With Mockito, I can use the ArgumentCaptor to captures values and assert them.

To explain this better, consider the following simple scenario in the CarSelector.java class.
In this class, I want to verify that every time the method buyRedFerrari() is invoked, which subsequently invoked the service (CarBuilderService.buildCar) method to build a car, it passes the expected instance of BuyingCriteria.
In this case, when the CarBuilderService.buildCar is called, it is passed with the argument that the car has to be RED and the model is a FERRARI.

Usage examples:


References:
Mockito
ArgumentCaptor API

Tuesday 30 July 2013

Hibernate: Using Transformers.aliasToBean(Class)

"Creates a resulttransformer that will inject aliased values into instances of Class via property methods or fields."

Usage examples:


This is a nice technic to automatically set values from a ResultSet to the CustomerDTO attributes.

The bean properties must match the columns name specified in the SQL statement, otherwise org.hibernate.PropertyNotFoundException: Could not find setter for... is thrown.
To make sure that this does not happen we can use SQLQuery.addScalar() so that the column names matches the attributes name of our CustomerDTO attributes.

Also, by using addScalar() we save the overhead of using ResultSetMetaData to map the column names to class attributes.

References:
- Hibernate - NativeSQL

Monday 29 July 2013

SQL: How to Cast an Integer to String in DB2



A CAST function can be used to convert one data type to another in DB2.
For example:
SELECT 100, LPAD(CAST(100 as VARCHAR(5)), 5, 'X') AS CONVERTED FROM SYSIBM.SYSDUMMY1;
Output
1 CONVERTED
100 XX100


In the example the LPAD function is used to show that the converted value is of type varchar and not integer.



REFERENCES:
Casting between data types

Thursday 6 June 2013

An easy way to post a java code snippets on Blogger...

An easy way to post a java code snippets on Blogger is to use Gist.
For example below is a code snippet copied and pasted into Gist.