• Post Reply Bookmark Topic Watch Topic
  • New Topic

Testing your Java code - JUnit  RSS feed

 
Ali Gordon
Ranch Hand
Posts: 182
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yakshemash Javaranch friends ! I saw it is important to know how to use a Java testing framework such as JUnit to test your code. I have the following questions regarding JUnit -

1 - As a developer (and NOT dedicated QA tester), how deep do I need to go into JUnit ? (A small 100 page crash course OR a big 500 page book with loads of problems ?)
2 - How do I learn the fundamentals of testing itself, regardless of language, framework etc. ?
3 - What are the better alternatives to JUnit ?

Thanks.
Chenqui.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would say its the developers job to Unit Test their code, not the QA department. This is especially true if you are following the Test Driven Development or Behavioural Driven Development paradigms as your unit tests would be written before your actual code.

That being said, my unit tests rarely use anything beyond the assertThat(...) method from org.unit.Assert, and the is(...), not(...) and equalTo(...) methods from org.hamcrest.Matchers.

To learn how to produce effective unit tests it is good to look into:

1) How to write testable code. Dependency injection is particularly useful here, as is defining a well documented interface to your classes.

2) Mocking (which is particularly useful if you use dependency testing). This allows you to control the input to your tests easily and decouples your tests from your implementations. I use Mockito, but other mocking frameworks are available.
 
Janeice DelVecchio
Bartender
Posts: 1812
12
Eclipse IDE Java Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Developers who write good tests sleep better at night than developers who rely on the QA department. When someone claims there's a defect in my code, I can see if there's a missing test and add it... exercising the defect immediately, and it will pass when the defect is fixed. I also find that, in some cases, the tests become a better source of documentation than comments that may go stale.

I think the book, Effective Unit Testing is pretty good. It focuses on Java, but the concepts should be the same regardless of language.

I use JUnit and JMock... mockito is fine.... In my opinion, I think the most important part is consistency.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I agree with that, consistency is very important. Readability is also very important. Tests should be short, focussed and well named.

They should also be run regularly. At a minimum they should be run manually by you before every check-in (I assume you use a configuration management tool OP), and then also by your continuous integration environment if you have one.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And about your third question, JUnit is the de-facto standard - it works well and it is what almost everybody uses, so why not use it yourself?

One alternative is TestNG.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!