Using 'Run with code coverage' with Intellj idea gets ClassFormatError - intellij-idea

Recently I use JMockit and Junit4 for unit tests in intellj idea. When I run my test using the 'Run' button or the 'debug' button, it works fine. When I try to get my code coverage result using the 'Run with coverage' button, it gets ClassFormatError.
java.lang.ClassFormatError at
sun.instrument.InstrumentationImpl.redefineClasses0(Native Method)
It happens only when I try to mock a method in the class which I want to test, like
new Expectations(BaseValidator.class) {
{
BaseValidator.isExistAirLineByTwoCode(anyString);
returns(false, true);
}
};
I test another method in class 'BaseValidator' which calls the method 'isExistAirLineByTwoCode'.
I don't know if the cause of this problem is from idea or jmokit, even junit. By the way, I use jacoco for code coverage reports.
How can I fix this problem?

I found a workaround by switching to jacoco runner:
In the menu, click Run --> Edit Configuration --> choose your test under JUNIT category
change coverage runner to Jacoco .

Related

Espresso: How do I kill the app each time Before test starts

I am writing down tests in kotlin using expresso and when I run it, espresso seem to leave the app state as is if the test fails or when it goes to the next test. I want to make these tests independent and not run them in order or implement an if condition in the acceptance tests. So I was looking out on how to kill it the similar way as how the app is fullReset with capability in appium.
I tried using the ORCHESTRATOR test runner implementation but as soon as I put that in the test options gradle sync and it displays there are no tests to run, even though I do have #Test annotation tag, but once I remove it I am able to run the tests.
I had even tried putting out .edit() .clear() and .commit() in the sharedpreference before each test but again that wont help. The app resumes rather than relaunch when the next test starts.
And then I again tried Espresso.pressback() but though it does, the app still only resumes
Clearing preferences at #Before may be too late in your case because it is called after the activity is started. You can try to create your own test rule before the activity is started:
class MyActivityTestRule : ActivityTestRule<MyActivity>(MyActivity::class.java, false, true) {
val preferences = ApplicationProvider.getApplicationContext<Application>()
.getSharedPreferences(NAME, MODE)
// or getDefaultSharedPreferences(), depends on your app!
override fun beforeActivityLaunched() {
preferences.edit().clear().commit()
}
}
Then set the rule in your test:
class MyTest {
#get:Rule val myTestRule = MyActivityTestRule()
// ...
}
testInstrumentationRunnerArguments clearPackageData: 'true'
under defaultConfig in project grade file, works for me.
However it does require the orchestrator, I'm suggesting clean and rebuild before running the test.
Here is how I added the orchestrator :
androidTestUtil 'androidx.test:orchestrator:1.1.0'
as dependencies
execution 'ANDROIDX_TEST_ORCHESTRATOR'
under test options.

Selenium: Check i the testcase pass or fail

Guys First of all I am totally new for Selenium.
I am having a automation project. In my project, I am creating a screenshot function to take screenshots of my event which I have created for my testcases. Now if my test cases passes then all screenshot should move to Pass folder, else fail folder.
I would like to know how to detect that my test case pass?
I know Nunit detects but I wanted to program it so that I cam place my screenshot as well as log file to pass or fail folder.
Program in C#
Selenium
Nunit to run my test case.
I think you meant was this. But there is work around for this. You need to add your code accordingly.
if (TestContext.CurrentContext.Result.Outcome.Equals(ResultState.Failure))
{
IntegrationTest.WriteInLog("FAILS");
}
else if (TestContext.CurrentContext.Result.Outcome.Equals(ResultState.Success))
{
IntegrationTest.WriteInLog("SUCESS");
}
Check status property and compare it with TestStatus enum at teardown method.
NUnit2:
TestContext.CurrentContext.Result.Status
NUnit3:
TestContext.CurrentContext.Result.Outcome.Status

MoreUnit Plugin for Intellij

I am looking for a moreUnit like plugin for Intellij
This are the features I mostly use and miss in Intellij:
create a new test method from the method under test
Marker on method under test indicating that a corresponding test method exist
jump to the test method (not only the class) from method under test.
execute corresponding test method from method under test.
I saw there is plugin called MoreUnit but the only thing it does is jump to test/to class under test. This is function does intellij out of the box with CMD+SHIFT+T.
After a few years there is now a port of moreunit to intellij
https://github.com/MoreUnit/org.moreunit.intellij.plugin

PHPUnit does not report on Not Successful Test

I've install PHPUnit3.6.2 and tried to run simple test.
If test Successful, there are no problems: phpunit reporting that test is OK.
But if there any error in test, PHPUnit reporting NOTHING! No failure and error text, no line in test.php, just empty line.
If it can helps: in browser code ends with getLocation() method. But I didnt use such method in my test.
why is this happening?
Also I should add: if in test use some element, wich not present on web page (for example button with wrong x-path). And there is action with this button in test:
$this->clickAt("wrong x-path");
phpunit doesn't show error report too. It continues to run. Looks like it found this button and clicked it. But there is NO such button on page.
No errors, no failures, test doesn't stop...
You are alking about Selenium testing. getLocation() is called by PHPUnit when formatting the failure report (to display the URL on which the failure occurred). You should try some simple (non-Selenium) PHPUnit tests to see if failure/error reporting works there, so you can narrow it down to Selenium or PHPUnit itself.

Running single test with TestNG in Intellij IDEA

How do you guys run single tests with IDEA's TestNG plugin? I tried creating a focus group, but it seems the TestNG plugin does not resolve dependencies to other tests automatically. Even when I comment out dependsOnMethods, it seems #BeforeMethod is not run.
Advice is greatly appreciated,
Robin
If you mean a single test method inside a test class there is a right-click option called Run "testMethod" where testMethod is the actual method name.
The short cut is ctrl+shift+F10.
If you put the cursor between methods (even outside the javadoc) or outside the entire class it will test all methods in that file.
Bring the class into focus in the editor, and then simply right-click, select run?