Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I unit test code that uses the google guava libraries, especially stuff in the io package?

A lot of the functionality in guava is provided by static methods. I haven't figured out how to merge the use of guava libraries and good Dependency Injection practice.

For example, if I were to use

Files.readLines(File, Charset)

then I find I have a hard time writing a unit test which doesn't touch the filesystem, which I only like to do for integration testing.

I guess it's possible that I could write an adapter for all of the ones I'm interested in? But that could possibly end up being a lot of work...

I find it odd that the guava libraries come from the same set of people that provide guice and write blog posts like this

like image 276
thebamaman Avatar asked Jul 15 '11 22:07

thebamaman


People also ask

What is the use of guava library in Java?

Guava is an open-source “Collection Library” library for Java, developed by Google. It provides utilities for working with Java collections. As you dive deep into Guava you'll notice how it reduces coding errors, facilitates standard coding practices and boost productivity by making code concise and easy to read.

How do you unit test your application?

Unit tests can be performed manually or automated. Those employing a manual method may have an instinctual document made detailing each step in the process; however, automated testing is the more common method to unit tests. Automated approaches commonly use a testing framework to develop test cases.

What is Google guava JRE?

Guava is a set of core Java libraries from Google that includes new collection types (such as multimap and multiset), immutable collections, a graph library, and utilities for concurrency, I/O, hashing, caching, primitives, strings, and more!


1 Answers

Ugh, the dreaded static methods. I have heard that JMockit is capable of mocking out statics, but I've never tried it myself. The solution I typically use is an Adapter.

public class FilesAdapter {

    private final File file;

    public FilesAdapter( File file ) {
        this.file = file;
    }

    public List<String> readLines( Charset charset ) {
        return Files.readLines( file, charset );
    }
}

You can optionally have FilesAdapter implements an interface, although since this is a single purpose object, I typically wouldn't.

GUICE is capable of injecting concrete objects, and mocking frameworks such as JMock2 & Mockito are able to mock concretes as well. This is all a matter of academics and different people will have different opinions.

If you were using GUICE, you would wrap this guy in a factory for injection goodness.

public class FilesAdapter {

    private final File file;

    @Inject
    protected FilesAdapter( @Assisted File file ) {
        this.file = file;
    }

    public List<String> readLines( Charset charset ) {
        return Files.readLines( file, charset );
    }

    public interface Factory {
        FilesAdapter create( File file );
    }
}
like image 147
scubadev Avatar answered Sep 23 '22 15:09

scubadev