DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

The Latest Coding Topics

article thumbnail
Mocking Method with Wildcard Generic Return Type
[Ivan Zerin provides a primer on mocking methods that contain generic return types.] There are several cases where construction when().thenReturn is not suitable during Unit tests. One of then generic return type. I have faced such issue, let's take a look. There is a method in interface declaration: public interface FooInterface { public Iterable getList(); ... } Implementation looks like this: public class Foo implements FooInterface { public List getList() { ... } ... } At first mocking of such method should not be something unusual: public class UnitTest { @Mock private FooInterface mockFoo; @Test public void someUnitTest() { ... List testList = generateTestList(); when(mockFoo.getList()) .thenReturn(testList); } } But this code won't compile with error: Cannot resolve method 'thenReturn(List). Seems to be some mistake, cause returned type are correct from the interface point of view. This error happening cause compiler can not gurantee that returned type of method getList() will be List. Actually return type of method getList() in this case is Iterable and this means "Return some Iterable object with any objects that extends SomeClass". Let's rename this type as 'X'. So when(mockFoo.getList()) will create object OngoingStubbing> and it has method thenReturn(Iterable). Compiler can not tell what type X before runtime and cannot perform safe cast from List to Iterable (we perfrom actuall call of method thenReturn(List)). Sounds a little tricky but let's assume that SomeClass is standard Java class Number. Then classes Integer and Double are both fulfill the criteria of List. Let's say that as return type of mock we will use List, in this case, compiler should be ready that actual work with code during runtime will be performed with List too, but it is clearly that cast from Double to Integer will be incorrect (try to cast double value 12.6 to int). You could argue that in the case of mockito compiler won't need to bother about the casting of returned type because call of original method would not produce anything, but it is known by mockito. From compiler point of view, it is only Java code, that should be checked for safety and correctness before compiling. Ok, so how we should deal with such cases in mockito? Use doReturn() method: doReturn(testList).when(mockFoo).getList(); Such expression is not type safe, so it were designed for exceprional cases, so use it only when you cannot use standart when().thenReturn(), which is the type-safe, elegant and more readable syntax.
April 27, 2016
by Ivan Zerin
· 44,511 Views · 4 Likes
article thumbnail
Enabling CORS in Node.js [Snippets]
This post shows how to enable CORS in Node. for your cross-domain requests.
April 27, 2016
by Madhuka Udantha
· 239,373 Views · 34 Likes
article thumbnail
Working With AVRO and Parquet Files
Installing and working with tools for AVRO and Parquet files with Scala and Spark
April 27, 2016
by Tim Spann DZone Core CORE
· 19,314 Views · 5 Likes
article thumbnail
Top 10 CSS Bad Practices
CSS is essential for web development; however, it's difficult to get right for your average developer, who may have interests elsewhere in the stack. Read this article to avoid needling inconsistencies and adopt best practices.
April 27, 2016
by Jonathan Danylko
· 10,226 Views · 11 Likes
article thumbnail
Logback Configuration: Using Groovy
Logback is one of the most popular logging frameworks for Java, but did you know you could configure it using Groovy? This guide walks you through the whole process.
April 27, 2016
by John Thompson
· 26,446 Views · 7 Likes
article thumbnail
Collaborators and Libraries: Java Design Patterns for Success
Should constructors throw exceptions? What can the 1815 Congress of Vienna teach us about good Java design?
April 26, 2016
by Alan Hohn
· 10,718 Views · 7 Likes
article thumbnail
Spring OAuth2 With JWT Sample
Spring Security is an extensible framework for authentication, including support for OAuth 2 and JSON Web Token, two popular choices.
April 26, 2016
by Anh Tuan Nguyen
· 79,133 Views · 11 Likes
article thumbnail
Sorted Pagination in Cassandra
We have a look at how to implement sorted pagination with the popular NoSQL solution Cassandra.
April 26, 2016
by Felipe Fernández
· 13,784 Views · 5 Likes
article thumbnail
Hadoop in Healthcare, Part 1
How Hadoop and open source big data analytics technology are positively helping the healthcare industry become more effective.
April 26, 2016
by Richard Proctor
· 11,092 Views · 4 Likes
article thumbnail
First Steps to Using Spring Boot and Cassandra
Get started using Spring Boot and the Apache Cassandra NoSQL database.
April 25, 2016
by Biju Kunjummen
· 64,355 Views · 9 Likes
article thumbnail
Build a REST API with XML Payload
Creating REST APIs with XML payloads.
April 23, 2016
by Neerav Aggarwal
· 49,667 Views · 3 Likes
article thumbnail
Java 8 New Date And Time Overview
A practical introduction to the new and improved Date & Time API in Java 8. If you've not had chance to review or use it yet then start here!
April 22, 2016
by Siva Prasad Rao Janapati
· 14,906 Views · 5 Likes
article thumbnail
The State of Jenkins — 2015 Community Survey
Results from the 2015 Jenkins Community Survey, suggesting that DevOps adoption has grown even more.
April 22, 2016
by Brian Dawson
· 5,916 Views · 3 Likes
article thumbnail
Authentication in Golang With JWTs
Go is an excellent choice for building fast and scalable API's. The net/http package provides most of what you need, but augmented with the Gorilla Toolkit, you'll have an API up and running in no time. Learn how to build and secure a Go API with JSON Web Tokens and consume it via a modern UI built with React.
April 21, 2016
by Adnan Kukic
· 23,341 Views · 8 Likes
article thumbnail
JavaScript MVVM — You’re (Probably) Doing it Wrong
If you are using one of the many frameworks that say they are using JavaScript MVVM, you might not be using it the way it should be used. In this article, Dave Bush defines MVVM, analyzes its advantages, and provides some MVVM best practices to follow.
April 21, 2016
by Dave Bush
· 22,951 Views · 10 Likes
article thumbnail
Building Blocks for Highly Available Systems on AWS
An upcoming article from DZone's Guide to Building and Deploying Applications on the Cloud, coming out next week!
April 21, 2016
by Andreas Wittig
· 8,411 Views · 5 Likes
article thumbnail
Deploy to WildFly and Docker From IntelliJ Using Management API
When you are new to both Docker and IntelliJ how do you deploy your Java EE applications? Steve Favez shows you how.
April 21, 2016
by Steve Favez
· 27,490 Views · 11 Likes
article thumbnail
Making Node.js Available to All Users With nvm
When trying to deploy a Node.js application to a remote server, how do we make it so that the user we've created (without sudo privilidges) to run/execute the application can see the necessary libraries needed to run our Node.js application? Read on and find out.
April 21, 2016
by Duncan Brown
· 29,127 Views · 2 Likes
article thumbnail
Using Spring Session for Concurrent Session Control in a Clustered Environment
How a new feature in Spring lets you manage sessions without relying on external repositories.
Updated April 20, 2016
by Joris Kuipers
· 30,359 Views · 17 Likes
article thumbnail
Logback Configuration: Using XML
Logback is a logging library from the creator of JUnit. In this series continuation, Spring guru John Thompson shows how to configure it using XML.
April 20, 2016
by John Thompson
· 92,394 Views · 16 Likes
  • Previous
  • ...
  • 760
  • 761
  • 762
  • 763
  • 764
  • 765
  • 766
  • 767
  • 768
  • 769
  • ...
  • Next
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook
×