how to disactivate #Before for a particular method? - testing

I am writing a test class in java using spring and junit (actually I have "#RunWith(SpringJUnit4ClassRunner.class)" at the bottom of my class).
there is a method with #Before annotation that I want it not to be run before a particular test method(but not the rest). how can I do this?
thanks

You have a few options:
Refactor to put that one test in a new test class which doesn't have #Before.
Refactor to put that one test in a new test class extending the original one that overrides #Before to do nothing.
Refactor to have all the other methods call the before method explicitly and remove the #Before annotation (I don't recommend you go with this one)

Related

How to make a method with #BeforeClass to run more than once?

I wish to run a method with #BeforeClass annotation twice (or more). Is there a way to do so with TestNG?
Like: #Test(invocationCount = 2)?
No there is no such provision available in TestNG.
#BeforeClass is a configuration method that is designed to run exactly once before any #Test methods within a particular class get executed.
So there's no way of altering its behavior.

Junit - Ignore on class level doesn't work

I'm using junit 4.12 and would like to ignore all test cases in class at once instead of adding ignore annotation before each method marked as test.
According to documentation it is possible to add #Ignore on class level.
#Ignore
public class BasicOperationsTest extends TestBaseClass{
I did it like above but it doesn't work - all test methods are executed.
When I add ignore before #Test method in this case annotation works and test is not executed.

How to tear down selenium webdriver when the #tests are in different classes

I have about 5 different classes with JUnit tests (selenium tests).
I need a way to teardown the webdriver at the end of the program.
So I need to know where to put the #AfterClass and how to pass the driver to it so it can be closed.
I thought of creating a test suit and implement the &AfterClass tearDownClass() there. But I'm not sure how to pass the driver to it to close.
You can use below code
[OneTimeTearDown]
public void SetupTestTeardown()
{
KillDriver();
Assert.AreEqual("", verificationErrors.ToString());
}
The OneTimeTearDown attribute is inherited from any base class. Therefore, if a base class has defined a OneTimeTearDown method, that method will be called after any test methods in the derived class.
You may define a OneTimeTearDown method in the base class and another in the derived class. NUnit will call base class OneTimeTearDown methods after those in the derived classes.
Add #BeforeClass to your superclass. Initiate the driver in that. Due to this, the test classes will also inherit the driver.
Add the driver teardown in your superclass in #AfterClass

#BeforeMethod implementaion issue

I want to implement BeforeMethod Annotation for specific test, it's getting implemented before every test can anybody know how resolve this issue!!! even tried with groups!!
Thanks
The solution is move out the test that doesn't need the #Before method into a separate test class. Also if you have some other code common to the tests you may move it into a helper class.
The easiest way is to call the method explicitly in your test.
#Test
public void yourTest() {
beforeMethod();
//your test
}

Why doesn't HttpClient tear down properly between unit tests in grails

I have two Controller unit tests and each one sets an HttpClient metaclass execute in setUp() like the following:
HttpClient.metaClass.execute = { HttpUriRequest request ->
<return expected data for my tests>
}
Then I attempt to tear down the metaClass in tearDown() with the following code:
protected void tearDown() {
super.tearDown()
GroovySystem.metaClassRegistry.removeMetaClass(HttpClient.class)
}
However only one of my unit tests passes because the return from the HttpClient is incorrect/unexpected. If I add the logic needed for both tests in the metaClass.execute of both tests I get no testing failures. However this is cumbersome and impractical, especially in an agile development environment.
What am I doing wrong with trying to tear down this HttpClient metaclass registration? How can I troubleshoot this further?
I'm currently using grails 1.3.7 on a CentOS 5 install.
Edit: I should clarify that my problem is that the metaClass override is causing issues between test classes, not test cases. We've been setting up the metaClass override so that it will return correct data for all of the test cases in a given class. So Test class A has the metaClass data for it's test cases and Test class B has the metaClass data for it's test cases. The issue is that since Test class A gets tested first, test class B ends up using the metaClass definition from test class A and fails because of this.
You shouldn't need to implement a tearDown method. Assuming you're extending ControllerUnitTestCase or GrailsUnitTestCase If you call registerMetaClass HttpClient before you metaClass the execute method it will replace the metaClass for HttpClient in between test classes for you. I've never used the removeMetaClass method so I don't know why that wouldn't be working so I can't answer the question of why that doesn't work, but this should get you past your issue.
...
registerMetaClass HttpClient
HttpClient.metaClass.execute = { HttpUriRequest request ->
<return expected data for my tests>
}
The solution I ended up using is a bit of a hack but it's easier to maintain. I just made a class in src/groovy that creates the HttpClient.metaClass and then just made each test class that needs it run the static method on the new class. That way all of the necessary code for the meta classing is in one place.