Dienstag, 7. März 2017

Take care when using @Injectmocks

Well @Injectmocks is nice and evil in one tag. Assume we have this service 

public class SampleService {
    private final Logger LOG = Logger.getLogger(SampleService.class.getName());

    private SampleDependency1   dependency1;

    public Long sampleMethod() {
        LOG.info("Calling sampleMethod");
        Long l = dependency1.calculateValue();
        LOG.info("l = " + l);
        return  l;
and the corresponding test
public class InjectmocksApplicationTests {
    private SampleDependency1 dependency1;

    private SampleService service = new SampleService();

    public void contextLoads() {
        final Long l = service.sampleMethod();
        Assert.isNull(l, "well l should be null");

Ok, should work and will call our service with the injected mock for dependency1. Now lets add a second dependency like this:
public class SampleService {
    private final Logger LOG = Logger.getLogger(SampleService.class.getName());

    private SampleDependency1   dependency1;

    private SampleDependency2   dependency2;

    public Long sampleMethod() {
        LOG.info("Calling sampleMethod");
        Long l = dependency1.calculateValue();
        l= dependency2.calculateValue();
        LOG.info("l = " + l);
        return  l;
This will compile, but running your test will result in 
java.lang.NullPointerException at net.kambrium.example.SampleService.sampleMethod(SampleService.java:23)
because you forgot to add dependency2 to your test class. To avoid this do
1. Use constructor wiring for your dependencies
2. Use @InjectMocks wisely and go searching on your tests for usage of the service you change and adjust the test cases
I personally prefer 1. as it always starts complaining at compile time. If your are using checkstyle you will see this warning, when using field injection:
Spring Team recommends: "Always use constructor based dependency injection in your beans. Always use assertions for mandatory dependencies"

Freitag, 10. Februar 2017

Renew an Apple distribution certificate for dummies

Year after year you may need to refresh your distribution cert of your iOS app. These few steps are the short list, which should be done to renew an iOS Distribution Certificate for App Store distribution:

  1. Go to developer.apple.com -> Account -> Certificates
  2. Select Certificates->Production-> Add
  3. Choose App Store and Ad Hoc
  4. Open Keychain Access app and select Keychain Access > Certificate Assistant > Request a Certificate from a Certificate Authority as stated in the developer website
  5. Fill out all values and don't forget to select "let me specify key pair information" this will ensure that a private key is specified in your generated cert
  6. Save the cert request
  7. Select continue and choose the saved cert request
  8. This will generate you new distribution cert
  9. Go to provisioning profile and select the one you're using to provision your app
  10. Change the used distribution cert to the one you newly generated
  11. Go back to xcode and refresh your certs within prefs->account
  12. Download the newly generated provisioning profile if not already done

On any other machine you need to get access to the distribution profile including the private key to sign ios application. You need to export the distribution profile from the machine, which generates the profile as a p12 file. Just choose export within the key list and provide a password.
On the second machine you could then open the p12 file provide the password and that imports the dist cert including the private key into your local key store.

Montag, 21. November 2016

Docker cheat sheet

Start docker image and map a port from the docker image to a port on localhost
docker run -p 8538c205bee2
Starts image id 8538c205bee2 and maps internal port 3306 of that image to localhost port 8806.

Open a bash in a running docker image
docker exec -it 8538c205bee2 /bin/bash
Stop all running docker images
docker stop $(docker ps -a -q)
Remove/delete all running docker images
docker rm $(docker ps -a -q)

Mittwoch, 16. November 2016

How to browser a remote docker repository

to browse a remote docker repository for available versions of a docker image you could use the docker API like this

curl https://mydockerrepo.de/v2/[IMAGE-NAME]/tags/list

Mittwoch, 27. Juli 2016

Get rid of the google hangout window

I'm using google hangout and chat quite often. Sometimes it happens that the google hangout window opens in a new window instead inside of the google mail window. If that happens just deactivate the google hangout extension in your browser. I.e. chrome do the following

  1. Go to settings->Extensions
  2. Search for Google Hangouts extension
  3. Deactivate it

Mittwoch, 4. November 2015

GotoMeeting G2MUpdate hinders sleep mode in Mac OSX

Today I noticed that my iMac constantly wakes up even if there are no applications open. The reason in my case was GotoMeeting, which I installed a few days ago on that iMac. The default installation activates the automatic update preferences. This little checkbox creates a launch agent in your ~/Library/LaunchAgents called com.citrixonline.GoToMeeting.G2MUpdate.plist.
This agents starts each our and searches for updates (a release each hour, wow those guys might be magicians).
To get rid of it do the following

  1. Open GotoMeeting Software
  2. Select preferences within the GotoMeeting app toolbar
  3. Select updates
  4. Deselect the checkbox