Essential Java Libraries You Should Not Miss in 2017
This article compiles a curated list of indispensable Java libraries—including Guice, OkHttp, Retrofit, JDeferred, RxJava, MBassador, Lombok, SLF4J, JUnitParams, Mockito, Jukito, Awaitility, Spock, and WireMock—providing concise descriptions and practical code examples to help developers choose the right tools for modern backend development.
The author collected a set of noteworthy Java libraries after reviewing Andres Almiray's presentation, presenting each library with a brief feature overview and example code.
Guice
Guice is a lightweight dependency‑injection framework from Google, suitable for Java 6+.
public class DatabaseTransactionLogProvider implements Provider<TransactionLog> { @Inject Connection connection; public TransactionLog get() { return new DatabaseTransactionLog(connection); } } public interface PaymentFactory { Payment create(Date startDate, Money amount); }
OkHttp
OkHttp is a high‑performance HTTP client that supports HTTP/2, connection pooling, transparent GZIP, and response caching.
OkHttpClient client = new OkHttpClient(); String run(String url) throws IOException { Request request = new Request.Builder() .url(url) .build(); Response response = client.newCall(request).execute(); return response.body().string(); }
Retrofit
Retrofit, from Square, is a type‑safe HTTP client that turns REST APIs into Java interfaces.
public interface GitHubService { @GET("users/{user}/repos") Call<List<Repo>> listRepos(@Path("user") String user); } Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://api.github.com/") .build(); GitHubService service = retrofit.create(GitHubService.class); Call<List<Repo>> repos = service.listRepos("octocat");
JDeferred
JDeferred brings JQuery‑style Deferred/Promise functionality to Java, supporting callbacks, chaining, multiple promises, executors, generics, Android, and Java 8 lambdas.
RxJava
RxJava adds reactive‑programming capabilities to the JVM, allowing asynchronous, event‑driven code with observable sequences and a rich set of operators.
Flowable.fromCallable(() -> { Thread.sleep(1000); // imitate expensive computation return "Done"; }) .subscribeOn(Schedulers.io()) .observeOn(Schedulers.single()) .subscribe(System.out::println, Throwable::printStackTrace); Thread.sleep(2000); // wait for the flow to finish
MBassador
MBassador is a lightweight, high‑performance event bus implementing the publish‑subscribe pattern with annotation‑driven listeners, synchronous/asynchronous delivery, filtering, prioritization, and extensibility.
// Define your listener class SimpleFileListener { @Handler public void handle(File msg) { // do something with the file } } // somewhere else in your code MBassador bus = new MBassador(); Object listener = new SimpleFileListener(); bus.subscribe(listener); bus.post(new File("/tmp/smallfile.csv")).now(); bus.post(new File("/tmp/bigfile.csv")).asynchronously();
Lombok
Lombok reduces boilerplate in Java by generating getters, setters, constructors, builders, toString, equals/hashCode, and more through annotations such as @Getter, @Setter, @Data, @Builder, @Value, @NonNull, @Cleanup, etc.
SLF4J
SLF4J provides a simple façade for various logging frameworks (java.util.logging, Logback, Log4j), allowing libraries to remain agnostic about the concrete logging implementation chosen by the application.
JUnitParams
JUnitParams enables parameterized tests in JUnit with clear, concise syntax, allowing parameters to be supplied via CSV strings or provider classes without extra boilerplate.
@Test @Parameters({"17, false", "22, true"}) public void personIsAdult(int age, boolean valid) throws Exception { assertThat(new Person(age).isAdult(), is(valid)); }
Mockito
Mockito is a popular mocking framework for Java unit tests, allowing creation of mock objects, stubbing of method calls, and verification of interactions.
// You can mock concrete classes as well as interfaces LinkedList mockedList = mock(LinkedList.class); // Stubbing when(mockedList.get(0)).thenReturn("first"); when(mockedList.get(1)).thenThrow(new RuntimeException()); System.out.println(mockedList.get(0)); // prints "first" System.out.println(mockedList.get(1)); // throws RuntimeException System.out.println(mockedList.get(999)); // prints "null" verify(mockedList).get(0);
Jukito
Jukito combines JUnit, Guice, and Mockito, automatically injecting @Inject fields, reducing boilerplate, and allowing unit tests to evolve with the code under test.
@RunWith(JukitoRunner.class) public class EmailSystemTest { @Inject EmailSystemImpl emailSystem; Email dummyEmail; @Before public void setupMocks(IncomingEmails incomingEmails, EmailFactory factory) { dummyEmail = factory.createDummy(); when(incomingEmails.count()).thenReturn(1); when(incomingEmails.get(0)).thenReturn(dummyEmail); } @Test public void shouldFetchEmailWhenStarting(EmailView emailView) { // WHEN emailSystem.start(); // THEN verify(emailView).addEmail(dummyEmail); } }
Awaitility
Awaitility offers a DSL for synchronizing asynchronous operations in tests, allowing concise waiting for conditions to become true.
@Test public void updatesCustomerStatus() throws Exception { // Publish an asynchronous event: publishEvent(updateCustomerStatusEvent); // Awaitility lets you wait until the asynchronous operation completes: await().atMost(5, SECONDS).until(customerStatusIsUpdated()); }
Spock
Spock is a powerful testing and specification framework for the JVM, using a readable Groovy‑based DSL.
class HelloSpockSpec extends spock.lang.Specification { def "length of Spock's and his friends' names"() { expect: name.size() == length where: name | length "Spock" | 5 "Kirk" | 4 "Scotty"| 6 } }
WireMock
WireMock is a tool for mocking HTTP services, supporting request stubbing, verification, proxying, stateful behavior, and configurable response delays.
{ "request": { "method": "GET", "url": "/some/thing" }, "response": { "status": 200, "statusMessage": "Everything was just fine!" } }
Thank you for reading!
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Architecture Digest
Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
