Separating JUnit standard output from test case output - junit

Is there a way to run JUnit programmatically in which I could pass custom PrintStream for all output of JUnit framework itself and leave standard output for test cases?
I see that JUnit internally is using JUnitSystem and TextListener to achieve this but I don't see intended entry point to use it without modifying or extending JUnitCore.
Does anyone have idea how to achieve this?

I don't think that JUnitCore and the classes used underneath print anything to standard out. The notable exception is JUnitCore.main, but this is just the main method for direct command-line execution. Instead you should use one of the run or runClasses methods. Your own RunListener can then output whatever/however it desires.

Related

JUnit equivalents for TestNG's #BeforeSuite, #BeforeTest

I'm refactoring some test classes from TestNG to JUnit 4. During the process, I've stumbled upon the following annotations:
#BeforeTest
#AfterTest
According to the manual:
The annotated method will be run before/after any test method belonging to the classes inside the tag is run.
What would be the equivalent annotations in JUnit?
This is the original answer, but I think it is wrong. See below for a better one
The equivalent would be the annotations
#Before
and
#After
see also http://junit.sourceforge.net/javadoc/org/junit/Before.html
This is a better answer, after I learned about the difference between Before/AfterMethod and Before/AfterTest in TestNG
If I got it right, with Before/AfterTest you can run a method before or after a list of tests, that you specify inside the annotation or a separate document.
There is no out of the box feature like this in JUnit.
Probably the best you can do, is put what ever you want to do in a JUnit Rule. See also http://schauderhaft.de/2011/07/24/rules-in-junit-4-9-beta-3/
Then you can use that Rule in any test that needs it.

Conditional skipping of unit tests

I'm currently working on a class, dealing with network issues. Using JUnit 3.8.1 and having a hardware device, that's not always around to test against, I'd like to conditionally suppress individual tests. Is there a way to achive this with a simple annotation like #if(!gatewayAvailable) -> test's suppressed?
Thanx for any pointers, marcus
There is no such feature in JUnit 3.8.1. You have to use JUnit4 and its Assume class.

How do I pass an argument to a JUnit Test?

I would like to pass arguments to my JUnit tests from run configurations. How do I reach to JUnits main method to access to those args? Anybody knows how to do this?
thanx
You can run you unit test with -D system properties and access them from you test case with System.getProperty instead of the way you are asking. Or check the class JUnitCore main method, that is used to run a test case from command line. http://junit.sourceforge.net/javadoc/org/junit/runner/JUnitCore.html. But never done this before.
I am not sure what kind of values you intend to pass, but in general it will be good to not have your tests depend on some data passed as arguments so that your tests are self contained and easy to run.

Which PMD rules to activate for JUnit tests?

I'm in the middle of setting up PMD as a tool in our team to support us writing better code. Basically I'm building Ant scripts and try to set up some rules for everyone to use.
But right now I hit this problem:
When I write JUnit tests I don't want to use the same rules I apply on our main source code. I don't care that much about String rules (like string dupliates or weird instantiations) in the junit tests.
My questions is:
Is that a fault on my side and should I start writing better JUnit tests?
Should I provide a 2nd set of rules that disables some of the string/design/finalizers rules?
The second option - I don't run PMD against my tests at all. I could and PMD provides some JUnit specific rules. I would definitely use a separate ruleset against the test code though. I expect more String literals and some thing specified instead of using conditionals/loops. After all, I don't want to duplicate the code I am trying to test.
Two things. Why are you trying to set up rules why not using the existing rules? (Special requirements?). And second yes of course Unit tests should have a good quality as well. Your Unit test test you production code so shouldn't they have at least the same quality as your production code?

How do I define a TestSuite without using #SuiteClasses in Junit 4.5?

I'm trying to migrate to JUnit 4 and I'm not clear about the correct way to set up test suites.
I know how to set up a test suite with fixed tests using the #SuitesClasses annotation.
However, I want to have a top-level suite class, where I can programatically decide which test classes or suites I want to load. I know that there are addTest and addTestSuite operations in the TestSuite class.
However, if I define a TestSuite subclass with a constructor that attempts to add these tests and try to run it, I get an error "Must have SuiteClasses annotation".
Any idea how to do this?
I would recommend creating a subclass of the BlockJUnit4ClassRunner and pull in the classes you want to test manually. The protected methods of the class do all the hard work for you, although you might want to tweak the Descriptions a bit to make sure the results are all unique in the output files.

Categories