Thursday, September 22, 2011

And to mock a static void...

Next up, mocking a static void to make it do nothing and then verifiying it was called. Here's how I did it using PowerMock:

@RunWith(PowerMockRunner.class)
@PrepareForTest(Thing.class)
public class MyTest {

   public void test() {

      // mock all static methods, rendering them useless/pointless
      PowerMockito.mockStatic(Thing.class)

      new Thing().save();

      // check save was called
      PowerMockito.verifyStatic();
      Thing.save();

   }

}

Wednesday, September 21, 2011

Mocking Roo Entities CRUD methods

When working with Roo it can be a pain to write true unit tests when your code interacts with CRUD/persistence methods on an Entity you've passed to it. The simple way around this is to use Mockito's 'spy' AKA partial mocking.

I tried this according to Mockito's documentation:
 Thing spy = spy(new Thing());
 when(spy.merge()).thenReturn(spy);
Unfortunately, as part of this code 'merge' is actually called, which will fail because Roo's logic attempts to merge the Mockito spy. The solution around this is to change how you setup the method stub:
 doReturn(spy).when(spy).merge();
For persist, i'd do
 doNothing().when(spy).persist();
I figured this out thanks to this page

Note that having to use a spy generally indicates poor design. Maybe that says something about Roo and its active record pattern...

Sunday, August 7, 2011

SMTP on Macbook with VirginMedia

After upgrading to Lion PostfixEnabler no longer worked, so I had to do this by hand. It was less than straight forward. Cudos to the following pages for pushing me in the right direction:

  • http://cloughnet.org/jeremy/linux/postfix/relaying-to-an-email-server-using-tls-wrapper-mode/
  • http://marc.info/?l=postfix-users&m=123799751204881

Virgin Media's SMTP server uses SSL on port 465. For reasons I don't care to understand, this is deprecated, and not supported by Postfix. Therefore, to use this we have to setup a secure tunnel using stunnel, and then get postfix to use the newly created stunnel as a relay.

Stunnel is already installed on Lion (as stunnel3). As I didn't realise this until after the event, I built stunnel from source my self:
  • Install XCode (required for 'make')
  • Go to the stunnel website
  • Get latest source
  • Extract to disk
  • Following stunnel build instructions
  • Put any old stuff in the generated certificate
Add the following to the stunnel.conf file in the root of the source

[smtp-tls-wrapper]
accept = 11125
client = yes
connect = smtp.ntlworld.com:465

Fire stunnel up with this config check there are no errors
sudo stunnel stunnel.conf
Before configuring postfix, we need to stop OS X from getting involved (for reasons why, see here). Edit the org.postfix.master.plist file in /System/Library/LaunchDaemons (as root or using sudo) and add the following two tags within the <dict> container:
<key>Disabled</key>
<true/>

Then Restart your Mac.

Finally, configure postfix. Add the following to the end of /etc/postfix/main.cf
myhostname = davidatkins.co.uk
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
mydestination = localhost.localdomain, localhost, 127.0.0.1
relayhost = [127.0.0.1]:11125
mynetworks_style = subnet

Note the relayhost to stunnel.

You should then be able to send mail:
echo "hello" | mail -s "subject" dave@davidatkins.co.uk
You can view the postfix logs in /var/log/mail.log
tail -f /var/log/mail.log

Note that once you've done this you'll need to manually start postfix and stunnel after every reboot to send mail.