Showing posts with label unit test. Show all posts
Showing posts with label unit test. Show all posts

15 June 2019

Mockito 2.0 and a thousand failing unit tests

Wow this killed me for a few days. I upgraded my long since out of date Mockito to the latest version 2.28.2 from version 1.something. Instantly 90% of my unit tests failed. Queue a long drawn out investigation to try and figure out what was happening. Numerous culprits were held under the spotlight and shaken down.

Ultimately, (as is usually the case) the answer was somewhat simple. My mock methods had all been declared irrelevant by this change to Mockito:

anyString() no longer accepts nulls.

So a mock method like this:

when(mockClass.mockMethod(anyString())).thenReturn("All your base are belong to me")

Simply stopped returning anything.

https://github.com/mockito/mockito/issues/185

The workaround is either to use any() or a deliberate null.

when(mockClass.mockMethod(any())).thenReturn("All your base are belong to me")


when(mockClass.mockMethod(isNull())).thenReturn("All your base are belong to me")


I hope this helps someone avoid my mistakes. Happy coding.

25 February 2015

Unit Tests, wonderful Unit tests


As I've mentioned before, I quite like unit tests. At the hands of Adam I've learnt to love and respect them. So when I had to write a simple function to ensure a password:

  • Is at least eight characters
  • Contains an upper-case character
  • Contains a lower-case character
  • Contains a digit
  • Contains a special character
I thought this would be a great reason to add some new unit tests in. Plus I'll be damned if I'm going to do all the typing on a phone to test and register all the various cases I can think of. As it happens this turned out to be a great idea because there was at least one case I'd missed.

Kids, unit tests work!

I thought I'd turn this into a challenge, how would you write the desired function? I'll provide the unit tests and you can impress me with your regex or your clever functions! Feel free to use any language you so desire. Winner gets the glory!

    @SmallTest
    public void testBlank(){
        String password = "";
        assertFalse(Utils.isPasswordValid(password));
    }

    @SmallTest
    public void testLower(){
        String password = "a";
        assertFalse(Utils.isPasswordValid(password));
    }

    @SmallTest
    public void testUpper(){
        String password = "A";
        assertFalse(Utils.isPasswordValid(password));
    }

    @SmallTest
    public void testNumber(){
        String password = "123";
        assertFalse(Utils.isPasswordValid(password));
    }

    @SmallTest
    public void testPadded(){
        String password = "     ";
        assertFalse(Utils.isPasswordValid(password));
    }

    @SmallTest
    public void testWhite(){
        String password = "\t\t";
        assertFalse(Utils.isPasswordValid(password));
    }

    @SmallTest
    public void testGood(){
        String password = "ab12CD*!";
        assertTrue(Utils.isPasswordValid(password));
    }

    @SmallTest
    public void testNoUpper(){
        String password = "ab12cd*!";
        assertFalse(Utils.isPasswordValid(password));
    }

    @SmallTest
    public void testNoLower(){
        String password = "AB12CD*!";
        assertFalse(Utils.isPasswordValid(password));
    }

    @SmallTest
    public void testNoNumber(){
        String password = "abcdCD*!";
        assertFalse(Utils.isPasswordValid(password));
    }

    @SmallTest
    public void testNoSymbol(){
        String password = "abcdCD12";
        assertFalse(Utils.isPasswordValid(password));
    }

    @SmallTest
    public void testGoodLong(){
        String password = "abcdCD12*&asdbbs167672HGSAHGAS!&^";
        assertTrue(Utils.isPasswordValid(password));
    }

    @SmallTest
    public void testGoodOne(){
        String password = "aaaaaA1\"";
        assertTrue(Utils.isPasswordValid(password));
    }

28 February 2014

Android Unit Tests


At my old gig I did unit tests....lots and lots of unit tests. At the mercy of one boisterous kiwi I got pretty good at it too, truth be told I began to really enjoy it. Unit testing (done well) gives you confidence in your code and allows you to make changes reassured that previous functionality won't break.
IMHO Any programmer worth their salt should unit test where possible.

Anyway, I've been in the Android world for some time and haven't really had the chance to start experimenting with unit testing. Thankfully Google has a really good explanation of how to set-up a unit testing environment:

http://developer.android.com/tools/testing/testing_android.html

However there's tonnes of information thrown at you there and their Eclipse example focuses on the UI. Now in my experience UI testing with automated tests is tricky. While it's fantastic if you can do it, I've found the UI changes too frequently too maintain automated tests. Business cases are naturally more concerned with the UI so it changes regularly and more often that not their are just too many factors involved. So I've written a very quick demo that focuses just on unit testing code.

The Android App To Be Tested

For the purposes of example I've created a very very simple Android app. Just run through the wizard and create a default app. I called mine myAwesomeAndroidApp. Then add the following method too the MainActivity:

public static boolean validateIsMonth(int month){
    
    if(month > 0 && month < 13){
        return true;
    }
    
    return false;
}

This is the method I want to test, simplest thing I could think of.

The Test Framework.

Eclipse ADT contains a framework for creating stand alone testing apps. This enables you to keep your tests and your deployable app separate. To start a new app goto
New -> Other -> Android Test Project


Follow the wizard through, of course ensuring you select your myAwesomeAndroidApp as the test target. You should now have two projects, the test project having an empty package.
In the empty package create a new class, call it TestValidation.java

Now paste the code in as such:

package com.example.myawesomeandroidapp.test;

import android.test.AndroidTestCase;

public class TestValidation extends AndroidTestCase {

    protected void setUp() throws Exception {
        super.setUp();
    }
    
}


Note the use of extends AndroidTestCase. This is very important and allows us to test Android projects.

Now add the unit tests, again this is a very basic example and you should make this more complicated as neccessary to suit your project and expectations.

    public void testValidation(){
        assertTrue(MainActivity.validateIsMonth(2));
    }
    
    public void testValidationFail(){
        assertFalse(MainActivity.validateIsMonth(13));
    }

You'll see we are testing our validateIsMonth method in the MainActivity, we're testing a pass case and a fail case. Again basic basic unit tests here but hopefully you get the idea.

If you right click on your project and click Run as -> Android JUnit Test your project should run and Eclipse should switch to JUnit view and show you a beautiful column of green lights indicating your unit tests have all passed :)



Hope this helps and happy testing.

Oh and jokes aside Adam Cameron has done some really awesome work on CFML unit testing, check out his blog for some great examples: http://cfmlblog.adamcameron.me/search/label/Unit%20Testing