• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JUnit testing Game code -beginner

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys

I'm new to unit testing and I'm completely stuck on testing these 2 methods in a game class. I've been looking on-line for simple JUnit instructions for beginners and I haven't really found anything usefull, I would appreciate some recommendations as well. I was reading somewhere about mockito and I've downloaded it but still have no clear idea of how to implement. I'm just getting really frustrated. Thanks in advance.






Method. 2

 
author & internet detective
Posts: 41878
909
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can’t tell what method 2 does. It looks like inHand is a collection? If so, you would test by calling it. Then you’d call another method that gets the inHand collection so you can assert the size is one larger or that it contains the bone object.

Method 1 could use some refactoring. It is really complicated which makes it hard to read and maintain. It has a complexity of at least 19. (# of conditionals and loops). 10 is considered high and many people (myself included) consider 10 to be too high. Consider extracting some of that logic to helper methods.

That said, when writing tests for a complex method that you don’t want to refactor (or don’t yet understand), you start simply. Can you write a test for the case where the input number (pick) is 0? You need to find the right input and have your test check an exception is thrown.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic