wood burning stoves 2.0*
The moose likes Testing and the fly likes JUnit - Testing someone elses code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "JUnit - Testing someone elses code" Watch "JUnit - Testing someone elses code" New topic
Author

JUnit - Testing someone elses code

Eric Fancis
Greenhorn

Joined: Jun 14, 2011
Posts: 29
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!


http://www.h4t3r.com
Eric Fancis
Greenhorn

Joined: Jun 14, 2011
Posts: 29
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

Joined: Jun 14, 2011
Posts: 29
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 29233
    
138

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)


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Eric Fancis
Greenhorn

Joined: Jun 14, 2011
Posts: 29
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

Joined: Oct 15, 2010
Posts: 25
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

Joined: Jun 14, 2011
Posts: 29
Which one would you reccomend first? I think spring is a little to advanced for my current skill set
enric jaen
Greenhorn

Joined: Oct 15, 2010
Posts: 25
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 29233
    
138

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JUnit - Testing someone elses code
 
Similar Threads
What is the meaning of 'Pragmatic Unit Testing'?
JB: JUnit Testing-how far to go?
JUnit in Action and agile development
what I can do with junit ??
Who does the unit testing? Who SHOULD do it?