This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JUnit - Testing someone elses code

 
Eric Fancis
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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:

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34095
337
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
enric jaen
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
Marshal
Posts: 34095
337
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic