• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Interfaces in TDD

 
Ranch Hand
Posts: 1402
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi @ll,

having this Class:



I want to use TDD. I am finding out how to create TDD tests for classes with interfaces. I have searched on the web for a while but I find it confusing.

Could someone, give a resource where I could get clear examples or info, please?

Regards, ISaac
 
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What exactly do you mean when you say "I want to create TDD tests"?

TDD is an approach, a way of thinking about design and code. It's not an adjective as you have used it.
 
Marshal
Posts: 5986
414
IntelliJ IDE Python TypeScript Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You've jumped the gun already with TDD because you've written some implementation without writing a test first. TDD stands for Test Driven Development which discusses a method of programming where your tests drive your implementation which implies you write your tests first.

You'll have probably found already a whole bunch of information about TDD on the internet, some proposing it, some condemning it, and lots of conflicting information in-between. My recommendation to grasp the original essence of TDD without getting clouded by all the chaff that followed is to pick yourself up a copy of Kent Beck's "Test-Driven Development by Example". The book is from way back in 2003 but don't be put off, it's a great read.
 
Angus Ferguson
Ranch Hand
Posts: 1402
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I meant that I am following TDD for the desing of a small app, and I would like to have feed back, because I need to add Interfaces in order to implement the Comparator interface....
 
Angus Ferguson
Ranch Hand
Posts: 1402
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok I get the book!
 
Junilu Lacar
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No knock on Kent Beck's book but IMO the best book that will help you understand and get better at TDD is a one that's not explicitly about TDD: Corey Haines "Understanding the 4 Rules of Simple Design"

If you're a beginner, I doubt you'll find anything specifically about testing interfaces in either book though. I think that's a nuance that you'll only recognize when you understand how TDD is supposed to be done. Here's my take on testing interfaces:

1. Tests that are written against an interface will be "abstract" by nature because interfaces are not executable, unless your interface has default methods as introduced in Java 8.  Generally, however, the tests you define for an interface should pass when run against any and all current and future implementations.

2. Following from #1, you'd probably want to set up a unit test for an interface's API as an abstract class that will be extended by tests that provide a specific implementation.

I don't see the code that you provided as being specifically tied to a test around an interface.  I would probably write tests like this:


As I said, TDD is a way to think about design and drive that thinking with tests.  These tests would probably make me think about the need to implement equals() and hashcode() since it would be surprising to have a compareTo() that is based on # of likes but still have equals() be inherited from Object, which only checks for reference equality -- there's a lack of symmetry there that should be address to make the BioPic class more complete and avoid surprises later on.
 
reply
    Bookmark Topic Watch Topic
  • New Topic