junit vs junit 4

hi,

i wonder how junit vs junit 4 are different.

i heard junit 4 if writing test case for exceptions and exception comes it will stop. I have not clearly understood this. also what are main difference in checking objects equality, toString() etc and also how to us mock object with respect to junit 3 and junit 4. please advise
LVL 7
gudii9Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dpearsonCommented:
The main shift between JUnit3 and JUnit4 was that 3 relied on names to identify a test and 4 uses an attribute:

/* JUnit 3 required this method name to start with 'test' for it to be a test */
  public void testMyFirstGo() {
       ... Assert some stuff
  }

/* In JUnit4 you put this @Test attribute before the method to show it's a test */
  @Test
  public void anyNameIWant() {
       ... Assert some stuff
  }

There are a series of other attributes for setup, teardown etc.  In 3 they required special names, in 4 you use an attribute.
So in this regard 4 is just better than 3.

For exceptions, in 4 in you can specify that a particular test should throw an exception like this:

@Test(expected = IndexOutOfBoundsException.class)
public void testOutOfBounds() {
  return "hello".charAt(20) ;      // This string only has 5 characters and we're asking for the 20th - so we should get an exception
}

Those are the big differences.

You can read about the other adjustments in more detail here:
http://www.asjava.com/junit/junit-3-vs-junit-4-comparison/

Doug
Margaret GreenCommented:
JUnit certainly changed significantly from version 3 to version 4.

As mentioned above, the way of identifying test methods has changed from method name based to annotation based where the method name has no special significance. @Test identifies a test method in JUnit 4.

Test classes in JUnit 3 also had to subclass a JUnit class. That is no longer necessary in JUnit 4. In JUnit 3, overridden inherited methods,  setUp() and tearDown()  were run before and after each test. These are replaced in JUnit 4 by annotations @Before and @After applied to method names of the developers choosing.

The annotations also bring in new features. @BeforeClass and @AfterClass identify methods to run before any execution of the set of tests in a class and after all the set of tests have completed.

A convenient annotation is @ignore which signals a test won't be run. This makes it easy to work with a set of tests broken by code changes elsewhere without commenting out the broken tests - a common problem over the life-cycle of a codebase.

Testing for results of tests benefits from new assertions in JUnit 4. But beyond this style are a whole new  rich set of alternatives called "matchers". These matchers can be used in what is called Behavior Driven Development. The BDD frameworks can be used with JUnit. The Hamcrest matchers are a popular library that let the developer write test specifications in the BDD style.

Expectations of thrown exceptions can be declared with the annotation @expect in JUnit 4.

In general a developer can think of JUnit as changing from a reflection based test execution framework in JUnit 3 to a declarative test execution framework in JUnit4. Reflection was used to identify the patterns in the test method names. The declarative style of annotations is more explicit. With the annotations also came new features.

Testrunners in JUnit 4 are a difference as well. JUnit 3 used the TestSuite class. In JUnit 4, the @RunWith annotation specifies the test runner.

There are several mock object libraries. Mockito has been used in both JUnit versions  3 and 4. I recently used JMockit with JUnit 4. It was simpler to set up for what I was testing. In Mockito there is a burden of declaring expectations. But, for a series of behaviors, Mockito lets you chain those expectations together.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.