Can some one please help me with the usage of testing a JAVA class using annotations ( org.testng.annotations.Test) ... I am dealing with a program (written by some1 else) in which Junit is not used instead "org.testng.annotations.Test" is used of which i have no idea
You'll definitely want to read the documentation on TestNG here, as there are some differences from JUnit. Some of the annotations are similar, though.
Here are some highlights:
#BeforeSuite, #BeforeClass, #BeforeTest mark methods that will run before a test suite, before a test class, and before an individual test method, respectively (as you might expect).
#Test annotates a method as being a test method.
Those are the most similar to JUnit. The docs have a great explanation of the others.
Related
I've always used TestNG annotations like #BeforeSuite, #BeforeClass etc. in my tests.
But lately, I've been reading about TestNG listeners, and I can't help but feel most of the methods these listeners provide do exactly the same things as some of the annotations.
For example:
#BeforeSuite seems to be the same as ISuiteListener.onStart()
#BeforeMethod seems the same as IInvokedMethodListener.beforeInvocation().
So, why do we have these methods that pretty much do the same thing?
The methods are intended to be used by the environment running the test, not by the test itself. For example, an IDE can add an ISuiteListener and update the status of running the tests shown in the GUI.
They are used just to customize the configuration for all levels like BeforeMethod,Class etc.
For example if you want different configuration in
#BeforeMethod
for different classes then you will use #BeforeMethod in each class with some different configuration, that configuration will be limited to that class and its methods only and not for all methods or tests in the complete suite.
If you use it in listeners then it will be common to all classes and methods.
Generally listeners annotations are used to perform a common function that is required for all classes and methods.
For example , starting logging at the beginning of tests or at suite start, capturing screenshot at the end of test case etc.
Same is for , i.e different configuration for different class and not a common configuration.
#BeforeClass
Hope it might answer your question.
I'm writing some tests with JUnit and Mockito.
I've noticed that Mockito provides a JUnit Runner and a JUnit TestRule
Which are the pros and cons of each solution ?
In general, a rule provides more flexibility than a runner. There can be only one runner, whereas you can have multiple rules in one test class.
Since Mockitos runner and rule apparently do the same I don't see a reason to use the runner here.
For the sake of completeness, I'd like to mention that there is no need to use Mockito's rules (or runners) unless you want to use mock annotations or validateMockitoUsage().
For consistency I usually create all mocks with mock() as quite often tests have mocked fields as well as mocked local variables.
I have quite shallow understanding of JUnit and Javassist, i just want to use them to do some program analysis. For example given a library, I want to know during the runtime what methods in the library have been invoked. I can use bytecode manipulation to insert a system.out.println("method_name"); statement in the beginning of a method. So during the runtime, it will print out what methods have been invoked.
In standalone application i can intercept before the main() is called and use my own class loader(see below), however in JUnit there is no main(), could anyone show me how to intercept at this situation?
Many thanks.
...
Loader loader = new Loader( pool );
loader.addTranslator( pool, xlat );
loader.run( className, args );
...
Edit: I use JUnit 4.8 and Javassist 3.15.0.GA
Might I recommend an alternative approach instead? You can use an aspect-oriented approach instead, using AspectJ. With that, you can define pointcuts around a subset of or all methods that you want to monitor.
Another option is, if you're looking to monitor code coverage (the fact that you're using JUnit and just looking to do System.out.println(...) are good hints of this), maybe you're looking for a code coverage tool? If so, Cobertura would be your best bet - with no custom coding required.
Both of these options do their own byte-code manipulation, but without being something that needs to be maintained by the developer.
If you're using Eclipse as your IDE, both of these tie-in very nicely to Eclipse. AspectJ is actually an Eclipse project, but doesn't require Eclipse. The Eclipse plug-in for Cobertura is eCobertura.
Yet another option for this is to do it within JUnit itself - and this wouldn't require any bytecode manipulation. Take a look at its TestWatchman class. I don't yet have this documented online as I do with my other libraries, but you could take a look at my BaseTest class as part of my JUnit utilities library. Any JUnit test class that extends this will automatically log (to SLF4J) when each test starts, succeeds, or fails. However, this is all at a test-level only, and won't help monitor other methods that each test runs.
I have a JUnit 4 test suite with BeforeClass and AfterClass methods that make a setup/teardown for the following test classes.
What I need is to run the test classes also by them selves, but for that I need a setup/teardown scenario (BeforeClass and AfterClass or something like that) for each test class. The thing is that when I run the suite I do not want to execute the setup/teardown before and after each test class, I only want to execute the setup/teardown from the test suite (once).
Is it possible ? Thanks in advance.
I don't know of any standard way to do this with JUnit. The reason for it, as you probably already know, is that your test cases should run independently of each other. This concerns the "normal" setup/teardown methods which run before and after each test method. Class setup and teardown is a bit different though - although I would still prefer running my tests independently and staying out of the trouble zone.
However, if you really are convinced of what you are doing, you could use a global flag to signal whether or not the class setup/teardown is to run, and to check for its state in the class setup/teardown methods. In your test suite, you could include a special class as the very first one, which does nothing more than execute the setup and set the global flag to indicate to the real test cases that their class setup/teardown methods must not be run. Similarly, a special last class in the suite can execute the teardown code. The caveat is that I am afraid JUnit does not guarantee the order of execution of test classes inside a suite, although most probably it does execute them in the specified order - but this is just an implementation detail. Try it out, it may work for you - but there is no guarantee it will always do what you expect.
If you have jUnit 4.7+ I recommend looking into the new feature called Rules (which are explained in this blog post). They might not be exactly what you want, but they are probably the best you get with jUnit.
Supposedly TestNG has better test grouping possibilities, but I haven't really looked into it myself yet.
No, there's no standard way to do this in JUnit, though you could hack something up as Péter Török suggested.
Note however that you are more or less abusing JUnit in doing this. The whole point of unit tests it that they are independent of each other. This is because dependencies between tests create a total maintenance nightmare (tests failing because the run in the wrong order).
So I'd advise you to strongly consider if it's not better to just always run the setup...
We noticed that when testNG test cases extend TestCase (JUnit) those tests start executing as Junit tests. Also, I should probably mention, the tests are run through Maven.
Is this a bug or a feature? Is it possible to override this behavior and still run those types of tests as TestNG tests? Do you know a link where TestNG talks about this?
thanks.
I didn't think either TestNG or JUnit required any base classes now that both use annotations to specify test methods. Why do you think you need to extend a class? And why on earth would a TestNG class extend the JUnit base class TestCase? Is it any surprise that they run as JUnit tests?
It sounds like neither bug nor feature but user error on your part. I'm not sure what you're getting at here. Why would you do this?
UPDATE: Your question is confusing me. Did you have JUnit tests running successfully that you're not trying to convert to TestNG, or visa versa? I'm having a very hard time understanding what you're trying to achieve here. Leave Maven out of it. It's immaterial whether they're run by you, Ant, or Maven.
Looking at the maven surefire plugin info I can't see any way to select a test for TestNG processing only if it also extends a jUnit 3 class.
IFAIK your best bet is to just work on each class seperately, removing the jUnit references and then retesting. That way you never have the mixture in one class and you should avoid problems. To make the work manageable I would be inclined to do this only when I was changing a test case for some other reason.