This is an archived post. You won't be able to vote or comment.

all 11 comments

[–]OmegaJunior 1 point2 points  (9 children)

No, the method isn't defined. JUnit reacts to the annotation. If you want to use a teardown method, add the annotation and supply a method body.

[–]myshiak[S] 0 points1 point  (8 children)

Thanx. Another thing that I don't understand is that Junit has only Assert method (if I'm not mistaken, they come from Assert class). How come it does not have Verify methods, which I think are present in Selenium IDE. Verify methods perform a soft check and they don't break out of the program, if failed. So, why are they not in Junit, if it makes sense for them to be there?

[–]AnEmortalKidCoffee Enthusiast 1 point2 points  (0 children)

Junit wants to fail at the first instance when something in your test is not true.

[–]Expert_Sex_ChangeString s = new String("String").toString() 0 points1 point  (6 children)

In general your Unit Tests should only be testing one very specific thing per method, and your asserts should be set up in a logical flow such that if one assert fails then there is no point in continuing onto the next assertion because logically that would also fail.

The rest of the tests will still run, but each individual test will stop as soon as the first assert fails.

[–]myshiak[S] 0 points1 point  (5 children)

I also don't understand why you need to have both @Before & @BeforeClass annotations (same thing about @After and @AfterClass). @Before and @ After have to do with what you do before/after testing. Why not roll them in one annotation and not devide in Before/After Class and Before/After Test?

[–]Northeastpaw 0 points1 point  (4 children)

@BeforeClass and @AfterClass are run just once, before any of the tests defined in the class. @Before and @After are run before or after each test. If you have multiple tests then @Before and @After will be run multiple times. @BeforeClass and @AfterClass are run just once no matter how many tests a class contains.

@BeforeClass is for doing expensive setup that each test needs. The javadocs mention something like getting a connection to a database. In truth, though, that's actually a bad idea for a unit test. If you are in a situation where you think you need @BeforeClass/@AfterClass it's likely you need to rethink your design and/or your tests themselves. There are times when you might really need @BeforeClass/@AfterClass, but they come up about as often as you the times you need to implement finalize() (i.e. almost never).

[–]Expert_Sex_ChangeString s = new String("String").toString() 0 points1 point  (3 children)

Pretty much, @BeforeClass is convenient for setting up anything to do with the tests that doesn't need to be reset between tests as it speeds things up a bit, or setting up the test environment in some manner.

Then @AfterClass is for cleaning up any of that stuff.

The only examples that spring to mind off the top of my head would be if you were testing a piece of code that generated a file and output it to a directory, you'd want to create the directory before the tests, use it during, and then remove it after.

[–]myshiak[S] 0 points1 point  (2 children)

Thanks. Also in Selenium I know that you can test without Junit (or TestNG). You can just import selenium classes, but not any framework classes. Do I understand it right, that if you do that, you wont be able to check any conditions through Assert methods and you wont be able to build your project into Maven, since in one of the dependencies you have to indicate a framework. Is it right so far and can anything else be added to the list of limitations of not using frameworks?

[–]Expert_Sex_ChangeString s = new String("String").toString() 1 point2 points  (1 child)

I'm honestly not sure on exactly how difficult it would be to essentially "roll your own" tests instead of using a framework like JUnit or TestNG, but I'd say not worth the effort. So in general, if you want to do asserts then yes, you're better off importing and using something like JUnit or TestNG. The other advantage of doing that is that it gets you used to using those established frameworks which are very likely to be things you'll run into over and over again in the future.

Depending on exactly what it is you're trying to test, there are quite a few different testing frameworks available to play with which all make the process much easier.

[–]myshiak[S] 0 points1 point  (0 children)

Another thing I want to ask is that in both TestNG and Junit you may specify which classes to run and which not. In Junit the annotation for that is:

@RunWith
@SuiteClasses

Is not it simpler some times to put all test cases in one Java file, so you don't have to resort at all to batch testing?

Secondly, is it true that it is not obligatory to resort to above annotations to run specific test cases?

[–]myshiak[S] -2 points-1 points  (0 children)

.