• 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 someone elses code

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've been tasked with writing unit tests. I've done a few JUnit tutorials, but they are really basic. I have no idea how to start writing tests for this code that another developer is responsible for developing.

I'm just completely lost. Does anyone have any tips for finding starting points? The project I'm testing uses Maven, Web Services, Spring, etc.... I am very new to to Java EE development.

So I'm looking for guidance or advice on how to go about writing unit tests with JUnit for existing code that is also being added to by another developer.

Let me know if you need more info. I'm so inexperienced that I find it difficult to even phrase my questions.

Thanks!
 
Eric Fancis
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm going to start with trying to test a single class that I understand the code.

I am going to try and test the public methods first.

Good plan?
 
Eric Fancis
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do I test a method with parameters?
How would I write a test to make sure that detailsTO is not null?
Can someone reccommend a good intro tutorial to JUnit 3.8 (using it at work) and testing in general?

JUnit setup code:



Method I'm testing:

 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that everyone here is a volunteer and many people like me only check once a day.

Having one person write code and another write JUnit tests is a horrible decision. You need to be able to refactor the code to make it testable which you can't do if someone is still working on it. You lose the benefits of codifying the author's thoughts. I'm sure you didn't make this decision, but sharing anyway.

A good starting point is small methods. The one you picked looks good.

Do you have to use JUNit 3.8? JUnit 4.0 is a bit easier to use if you get a choice.

As far as how to test it, you need two tests for full coverage. Can you think of what the scenarios are? What objects do you need to pass in (should be one). What do you want to assert? (not null, type is correct)

 
Eric Fancis
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:Note that everyone here is a volunteer and many people like me only check once a day.

Having one person write code and another write JUnit tests is a horrible decision. You need to be able to refactor the code to make it testable which you can't do if someone is still working on it. You lose the benefits of codifying the author's thoughts. I'm sure you didn't make this decision, but sharing anyway.

A good starting point is small methods. The one you picked looks good.

Do you have to use JUNit 3.8? JUnit 4.0 is a bit easier to use if you get a choice.

As far as how to test it, you need two tests for full coverage. Can you think of what the scenarios are? What objects do you need to pass in (should be one). What do you want to assert? (not null, type is correct)



Thanks, I've got some simple tests going. Now I'm moving on to jMock.
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some books I found useful:

Spring in action, 2nd edition (appendix B)

JUnit in Action, 2nd Edition (2010)

Junit recipes (2004)

Test Driven: Practical TDD and Acceptance TDD for Java Developers (2007)



 
Eric Fancis
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which one would you reccomend first? I think spring is a little to advanced for my current skill set
 
enric jaen
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's hard to say.

Junit In Action 1st ed introduces junit and covers junit 3.8.1 and maven,
Junit In Action 2nd ed introduces junit and covers junit 4.5 and maven2
Junit recipes is based on junit 3.8.1 but contains good testing tips

Keep in mind that junit now goes in version 4.10

Maybe "Pragmatic Unit Testing in Java with JUnit (2004)" can be a good choice, but I have not read it
http://media.pragprog.com/titles/utj/contents.pdf

If you want to test with spring then:
"Spring recipes" covers junit 3 and 4 spring tests
Spring in action 2d covers also spring tests

If you want to learn TDD, read Part 1 of "Test Driven: Practical TDD.."


HTH..
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
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'd start with learning about unit testing in general. Testing the Spring specific parts require understanding regular junit first. And much Spring code can be tested without knowing Spring. It's when you get to in container testing that you need to understand Spring and read about the Spring context testing.

I think it is best to start with a JUNit 4 book. Test Driven or Junit in Action 2nd edition would be my recommendations. I liked the former better, but both are fine.
 
reply
    Bookmark Topic Watch Topic
  • New Topic