#BeforeMethod implementaion issue - testing

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
}

Related

Best way to test enqueueUniqueWork and cancelUniqueWork WorkManager

What is the best way to test the methods enqueueUniqueWork and cancelUniqueWork in Kotlin in Android?
I only found answers for testing work with Id, but I have a unique name.
After a lot of searching, I found this tutorial:
https://medium.com/#thefallen.class/unit-testing-workmanager-with-dependencies-8ffff7ef5476
And also android documentation:
https://developer.android.com/topic/libraries/architecture/workmanager/how-to/integration-testing
It is not possible to directly tests the methods enqueueUniqueWork and cancelUniqueWork with Mocks only.
I needed libraries such as
androidx.test:core
androidx.work:work-testing
org.robolectric:robolectric
androidx.work:work-testing allowed me to initialize a Test WorkerManager thanks to the context provided by androidx.test:core
#RunWith(RobolectricTestRunner::class)
class ClassTest{
private lateinit classToTest: ClassToTest
private lateinit context: Context
#Before
fun setUp(){
context = ApplicationProvider.getApplicationContext()
val config = Configuration.Builder()
.setWorkerFactory((WorkerFactory.getDefaultWorkerFactory()))
.setExecutor(SynchronousExecutor())
.setMinimumLoggingLevel(Log.DEBUG)
.build()
WorkManagerTestInitHelper.initializeTestWorkManager(context, config)
}
}
Then, I managed to get the state of my unique enqueued work and cancelled work thanks to lines such as
WorkManager.getInstance(context).getWorkInfosForUniqueWork("name").get()[0].state
Which returned elements such as WorkInfo.State.CANCELLED or WorkInfo.State.ENQUEUED
I am not 100% if this is the best method to do so, but it allowed me to test what I wanted. Hoping it could help anyone else
Both methods came from WorkManager class. This class is an element of runtime environment, so testing of them is task of author of WorkManager.
What do you, probably, want to do is to test task executed/cancelled by those methods or test are they called.
For that second option please take a look at something called Mock. This is a special „class” that pretends your original class, but you can control how it behaves and check how it was used.

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.

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

how to disactivate #Before for a particular method?

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)

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.