Can I directly replace #RunWith(PowerMockRunner.class) with #RunWith(MockitoJunitRunner.class) - junit4

#RunWith(PowerMockRunner.class) is not working with Eclemma and it is showing 0% coverage for the classes annotated with #RunWith(PowerMockRunner.class)
If I replace #RunWith(PowerMockRunner.class) with #RunWith(MockitoJunitRunner.class) the code coverage is showing correctly.
If I replace #RunWith(PowerMockRunner.class) with #RunWith(MockitoJunitRunner.class) will it impact on existing test cases.
If this approach does not work then let me know an alternate option for #RunWith(PowerMockRunner.class)
Tried to get solution for #RunWith(PowerMockRunner.class) but no luck

Related

How to pass set of Systemproperties only to one particular testcase instead of all in gradle test

I have set of Junit test suites. All are working fine in eclipse.
In the test suites one test suite we will pass some System properties.
But those modified system properties should not propagate to other test suites So i just put those only in setup method like below,
#BeforeClass
public static void setUp() {
System.setProperty("public", "publicfolder");
System.setProperty("private", "privatefolder");
}
But this is working fine in eclipse only. While running it outside all other test suites are working fine except the above one.
I know to pass system properties in gradle in build file. but how could i pass those system properties to only one test suite instead of all thats my question here.
You could add another Test task so there's two in total. Each could have a different filter to run separate test suites and each could pass different system properties.
See here for a similar solution

Eclipse wraps NOPMD Tag after imports

I have a little problem with Eclipse. When i set a //NOPMD tag behind an import it wraps the tag to the end of the imports. How can I avoid that eclipse wraps any //NOPMD tags during beautifying the java program?
Example before beautifying:
import java.io.IOException;//NOPMD
Example after beautifying:
import java.io.IOException;
//NOPMD
How about using #SupressWarnings("PMD.NameOfRule"). This will suppress all instances of that rule in the class which may or may not be what you want. But it is immune to code reformatting and organize imports.
As a reference, here are the options for suppressing.
Found a better answer once I stopped looking at this as a PMD problem! The accepted answer to this question shows how to turn off formatting on sections of the code by writing:
// #formatter:off
...
// #formatter:on
You'd need to do this on each class around your imports. Or each class that has //NOPMD that is. And you'd need to turn on the setting in your workspace. And have your teammates to this same if you are on a team.

JUnit: How to bundle #Test methods to a suite?

I have several classes each containing several methods annotated with #Test. The classes are not extending TestCase. Now I want to
write a main that executes all these methods (for command line use)
create a class that can be "Run as -> JUnit Test" in Eclipse executing all these methods
Since the classes are no TestCases I can't just add them to a suite. Also extending TestCase is not an option because the test methods are just annotated and their names don't start with 'test'.
How can I do this?
Try this:
#RunWith(Suite.class)
#Suite.SuiteClasses({
Test1.class,
Test2.class,
Test3.class,
Test4.class
})
public class YourTestSuite {
}
You can set up run configurations for a project. Right click on your project. Then selecte 'Run as' -> 'Run configurations'. Within that you can select run all tests
What Eugene posted works if you have a small number of classes. If you want all the tests in a certain package pattern, Classpath Suite lets you specify this with patterns instead of listing them all out.

Selenium junit tests - how do I run tests within a test in sequential order?

I am using junit with eclipse to write function tests.
When running an individual test it runs in the order that I have them set within the class.
Eg.
testCreateUser
testJoinUserToRoom
testVerify
testDeleteUser
However when I run this test as part of a suite, (so in a package) the order is random.
It will for example do the verify, then delete user then joinuserToRoom then Createuser.
My tests within the suite are not dependent on each other. However each individual test within a test is dependent on them being run in the correct order.
Is there any way I can achieve this?
Thanks.
You can't guarantee the order of execution of test methods in JUnit.
The order of execution of test classes within a suite is guaranteed (if you're using Suite), but the order of execution if the test classes are found by reflection isn't (for instance, if you're running a package in Eclipse, or a set of tests from maven or ant). This may be definable by ant or maven, but it isn't defined by JUnit.
In general, JUnit executes the test methods in the order in which they are defined in the source file, but not all JVMs guarantee this (particulary with JVM 7). If some of the methods are inherited from an abstract base test class, then this may not hold either. (This sounds like your case, but I can't tell from your description).
For more information on this, see my answer to Has JUnit4 begun supporting ordering of test? Is it intentional?.
So what can you do to fix your problem? There are two solutions.
In your original example, you've actually only got one test (verify), but you've got 4 methods, two setup (createUser, joinUserToRoom) and one teardown (deleteUser). So your first option is to better define your test cases, using a TestRule, in particular ExternalResource. ExternalResource allows you to define before/after behaviour for a test, similar to #Before/#After. However, the advantage of ExternalResource is that you can factor this out of your test.
So, you would create/delete the user in your external resource:
public class UsesExternalResource {
#Rule
public ExternalResource resource= new ExternalResource() {
#Override
protected void before() throws Throwable {
// create user
};
#Override
protected void after() {
// destroy user
};
};
#Test
public void testJoinUserToRoom() {
// join user to room
// verify all ok
}
}
For me, this is simpler and easier to understand, and you get independent tests, which is a good thing. This is what I would do, but you will need to refactor your tests a bit. You can also stack these Rules using RuleChain.
Your second option, if you really want to introduce dependencies between your test methods, is to look at TestNG, in which you can define dependencies from one test to another.
If they have a 'correct' order, then they are not multiple tests, but a single test that you have incorrectly annotated as being multiple independent tests.
Best practise would to rewrite them in approved junit style (setup - act - verify), supported by #Before or #BeforeClass methods that did any required common setup.
Quick workaround would be to have a single #Test-annotated method that called the other test methods in sequence. That becomes something like the preferred alternative if you are using Junit not to do strict unit testing, but something more like scenario-driven systems testing. It's not necessarily the best tool for such use, but it does work perfectly well in some cases.
Then, what you would have so far is have a single test:
#Test public void testUserNominalLifeCycle(...
which could then, if you are feeling virtuous, be supplemented by extra new tests like
#Test public void testUserWhoNeverJoinsARoom(...

PHPunit, Getting function code coverage for a ZF controller action

I'm using PHPunit to test our Zend Framework project and it works allright but i'm not getting coverage om my action methods in my controllers.
Although I get coverage in number off lines of code but I want to have coverage on the functions/methods.
I see a lot of examples on the internet where they just do it like this:
class IndexTest extends Zend_Test_PHPUnit_ControllerTestCase
{
public function testIndexAction() {
$this->dispatch('/');
$this->assertController('index');
$this->assertAction('index');
$this->assertXpath("//form[#action = '/index']");
}
}
Which should work even if I look to this example from Jon:
http://www.zendcasts.com/unit-testing-with-the-zend-framework-with-zend_test-and-phpunit/2009/06/
http://code.google.com/p/zendcasts/source/browse/#svn/trunk/zc25-unit-testing
I'm doing it almost the exact way but it's not giving me any percentage of code coverage in functions, except for the init() function but I think that one is automaticly ignored by Zend Controller testcase.
I'm I doing something stupid or doesn't PHPUnit reconize it's calling this action? Using PHPUnit 3.5.14 and Zend Framework 1.11.x
To get code coverage for a specific function from PHPunit you need to make sure every line of your function is called by your tests, so that every possible situation is taken care of and tested.