Here’s how to capture arguments being passed to a method in order to make assertions on them.

Take the following production code. You want to make assertions on the User object that is being passed into the migrationDao.migrate() method from your class under test.

public class User {
    private String name;
    private int age;

    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() { return this.name; }

    public int getAge() { return this.age; }
}
public class MigrationService {
    private MigrationDao migrationDao;

    public MigrationService(MigrationDao migrationDao) {
        this.migrationDao = migrationDao;
    }

    public void migrate(User user) {
        migrationDao.migrate(user);
    }

}

Capture the argument using Mockito’s ArgumentCaptor in order to make specific assertions on the object passed.

public class MigrationServiceTest {

    private MigrationService MigrationService;
    private MigrationDao migrationDaoMock;

    @Before
    public void setup() {
        migrationDaoMock = mock(MigrationDao.class);
        migrationService = new MigrationService(migrationDaoMock);
    }

    @Test
    public void migrate_givenMigrateSingleUserNamedTimAged37_shouldReceiveUserNamedTimAged37() {
        User userToMigrate = new User("Tim", 37);
        migrationService.migrateUser(userToMigrate);
        
        ArgumentCaptor<User> userArgCaptor = ArgumentCaptor.forClass(User.class);
        verify(mockMigrationDao).migrate(userArgCaptor.capture());
        User userPassedToMigrationDao = userArgCaptor.getValue();
        
        assertThat(userPassedToMigrationDao.getName(), is("Tim"));
        assertThat(userPassedToMigrationDao.getAge(), is(37));
    }
}
Book Review: 'Your Code as a Crime Scene' by Adam Tornhill

Rating: 10/10

‘Your Code as a Crime Scene’ is aimed at intermediate to advanced Engineers who want to get more from their source repository history than simply who did what and when. As a developer who falls into that category I found it a fascinating read and left me itching to hunt out the criminals in my own applications.

Adam shows how to analyse your change history in new and interesting ways that allow you to uncover your code’s dirty little secrets. Files that always change together identify unnecessary coupling. Files with large indentations uncover complex branching. Files that consistently grow on each change point to possible code dumping grounds. Plus a number of scenarios that I hadn’t previously considered as problem areas.

The author has also written a powerful suite of tools called code-maat, available for free, which allow you to follow along and practice with the plentiful examples and get stuck in with analysing your own source history. This is a real strength of the book as it becomes of real practical use almost immediately. This ‘learn by doing’ approach is very valuable. Recommendations to improve each of the problem areas are presented and discussed in a pragmatic manner.

There are very few books that cover this topic and this is a welcome addition to my library. Having a good catalogue of scientific analysis techniques such as these for interrogating your source code takes a good deal of the ‘artwork’ out of finding problem hot spots. Highly recommended.


Disclosure: I received a copy of this title from the publisher in exchange for reviewing it on behalf of coderanch.com

Book Review: The Nature of Software Development

Rating: 7/10

‘The Nature of Software Development’ is aimed at anyone who wants to get the best out of their software development process. As an experienced developer I found it an interesting read but I expect software team managers will get the most value from it.

Ron Jeffries presents some simple, but not necessarily easy, ways to think about writing software. He discusses how to organise your work, and your teams. In doing so he cuts away a lot of the chaff and ceremony that has overshadowed some Agile methodologies in recent times to draw us back to the fundamental qualities that formed the Agile Manifesto nearly 15 years ago.

The book has a strong message, yet it is a light read. Ron uses his natural and conversational language along with plenty of pictures to illustrate his teachings, which makes for a pleasantly relaxed reading experience.

Overall an enlightening read that cuts right to the meaty parts of why we write software and presents recommendations for how we organise our work and ourselves to write valuable software in an effective manner. While at times feeling like a collation of blog posts, it’s a worthwhile collation that highlights the fundamentals of what Agile is intended to be. The abundance of illustration is a nice touch but does not present well in eBook format, being particularly uninspiring on the Kindle version. Nonetheless, a good read for software developers and team managers.


Disclosure: I received an eBook copy of this title from the publisher in exchange for reviewing it on behalf of coderanch.com

Book Review: Pragmatic Unit Testing in Java 8

Rating: 8/10

In ‘Pragmatic Unit Testing in Java 8’ Jeff demonstrates a very effective way to develop software, that just happens to involve writing a lot of Unit Tests. While the book is aimed as Java programmers who are new to Unit Testing, more experienced programmers are likely to learn a thing or two as well.

The book starts by guiding you through the development of a sample application discussing how and, quite importantly, how you might write useful Unit Tests to verify that the application works as intended. The ‘why’ topic is nicely supplemented with a sequence of hypothetical conversations between two colleagues, where one is not sold on the virtues of testing and the other puts forth a reasoned argument. Having a single example application to work with is a nice coherent way for the readers to practice the material being presented for themselves.

The author gives a selective exploration into the features of the jUnit testing framework that he finds most useful and highlights how to use them to write expressive test code. The later chapters reinforce an important concept that your test code is just as important as your production code, and equal care should be taken in keeping it clean. A valuable mindset.

Overall, Pragmatic Unit Testing in Java 8 is a solid introduction to unit testing and presents a wealth of tips, tricks, and good practices that will set any programmer in good stead. Recommended.


Disclosure: I received a copy of this book from the publisher in exchange for reviewing this book on behalf of coderanch.com

Book Review: Programming Elixir

Rating: 9/10

‘Programming Elixir’ provides practised programmers an introduction to the Elixir programming language. Particular attention is paid to the features of the language that the author, in his considerable experience, finds the most interesting. In addition to the expected introduction to the basics of the Elixir language, the author presents a deeper investigation into Immutability, Anonymous functions, and organising a project, among others. More advanced concurrent programming topics are covered with sections on working with multiple processes, and OTP (Open Telecom Platform).

A particular strength of the book is the collection of practice exercises that accompany each chapter. These transform the book into more of a personal training session where the author runs you through the learning material and then tests you on your knowledge. This ‘learn by doing’ approach greatly enhances the overall experience.

The author has deliberately targeted more experienced programmers and the prerequisite knowledge needed to get the most from the book is quite high. Therefore, lesser experienced programmers might find the pace quite hard going.

Overall, Programming Elixir is an inspiring introduction to the Elixir language, and a good choice of title for the curious programmer eager to explore a new language and a new way of programming. The book certainly does live up to its tag-line: “Functional |> Pragmatic |> Concurrent |> Fun”. Most enjoyable.


Disclosure: I received a copy of this book from the publisher in exchange for reviewing this book on behalf of coderanch.com