Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3432394
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:26:38+00:00 2026-05-18T07:26:38+00:00

After comming from Ruby world, I’m having little problems doing TDD in Java. The

  • 0

After comming from Ruby world, I’m having little problems doing TDD in Java. The biggest issue is when I have application that is just communicating with external API.

Say I want to just fetch some data from Google Calendar, or 5 tweets from some Twitter user and display it.

In Ruby, I don’t have any problems, because I can monkey-patch the API library in tests directly, but I have no such option in Java.

If I think about this in terms of MVC, my model objects are directly accessing the API through some library. The question is, is this bad design? Should I always wrap any API library in some interface, so I can mock/stub it in Java?

Because when I think about this, the only purpose of that interface would be to simulate (please don’t kill me for saying this) the monkey-patch. Meaning that any time I use any external resource, I have to wrap each layer in interface that can be stubbed out.

# do I have to abstract everything just to do this in Java?
Twitter.stub!(:search)

Now you might say that I should always abstract away the interface, so I can change the underlying layer to anything else. But if I’m writing twitter app, I’m not going to change it to RSS reader.

Yes, I can add for example Facebook and then it would make sense to have interface. But when there is no other resource that can be substituted for the one I’m using, than I still have to wrap everything in interfaces to make it testable.

Am I missing something, or is this just a way to test in the Java world?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T07:26:39+00:00Added an answer on May 18, 2026 at 7:26 am

    Using interfaces is just generally good practice in Java. Some languages have multiple inheritance, others have duck typing, Java has interfaces. It’s a key feature of the language, it lets me use

    1. different aspects of a class in different contexts and
    2. different implementations of the same contract without changing client code.

    So interfaces are a concept you should embrace in general, and then you would reap the benefits in situations like this where you could substitute your services by mock objects.

    One of the most important books about Java best practices is Effective Java by Joshua Bloch. I would highly suggest you to read it. In this context the most important part is Item 52: Refer to objects by their interfaces. Quote:

    More generally, you should favor the use of interfaces rather than
    classes to refer to objects. If appropriate interface types exist, then parameters, return values, variables, and fields should all be declared using interface
    types.
    The only time you really need to refer to an object’s class is when you’re
    creating it with a constructor.

    And if you take things even further (e.g. when using dependency injection), you aren’t even calling the constructor.

    One of the key problems of switching languages is that you have to switch the way of thinking too. You can’t program language x effectively while thinking in language y. You can’t program C effectively without using pointers, Ruby not without duck typing and Java not without Interfaces.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a lot of trouble with Ruby after coming back to it from
After doing some stuff in Ruby on Rails with Cucumber, RSpec and Ruby BDD
I'm new to Ruby coming from Java. I'm trying to make a http get
Possible Duplicate: Python try-else Comming from a Java background, I don't quite get what
I have to do read an Large InputStream comming from a URL. I loaded
I'm kind of new to lisp, and after coming from languages like C, Java,
It's been a while since I programmed in C++, and after coming from python,
After reading this question , I was reminded of when I was taught Java
After lots of attempts and search I have never found a satisfactory way to
I am relatively new to C++, coming from the happy-go-lucky world of Python. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.