Using emma : Method is getting called but in Code coverage it shows 0% for that file - emma

I am using Emma for code-coverage for my project. In my JUnit test case, I am calling a method but in code coverage report, it shows no coverage for that method which is getting called from testCase.

Check these points:
Set a breakpoint in the method to make sure it is really called.
You can tell emma to omit some classes. Check the config.
Is the class properly instrumented?

Related

Maven JUnit 5: Using JUnit5 tags, run all unit tests by default or run only a specific test suite from the command line

I posted this: Maven JUnit 5: Run all unit tests by default or run only a specific test suite from the command line .
I'm simply posting it again with almost exactly the same title because nothing about my base problem has changed, but I will include what I tried to do with Tags, which are not working for me.
We are upgrading our services to a relatively recent SpringBoot version, along with JUnit 5. We normally run our builds with just "mvn package", which runs all of our unit tests by default.
We also have "component tests", which are run from a separate command line, and which are all specified in a single test suite with a specific name. Before JUnit 5, we would run this with something like:
mvn -Dtest=ComponentTestSuite test
This was working fine with JUnit 4.
The class looks like this:
#Suite
#SelectClasses(<testclassname>.class)
public class ComponentTestSuite {
}
With JUnit 5, this ends up saying "No tests found".
We are using v2.3.12 of Spring-Boot, which by default includes a somewhat older version of JUnit 5. I am overriding those defaults and including v1.8.2 of the junit-platform components and v5.8.2 of the jupiter components.
For the service that I'm testing this with, the test suite only has a single component test (which is unusual). I WAS able to simply replace "ComponentTestSuite" with the name of the component test class, and that would run that single component test.
I noticed this thread: Junit5 test suites with #Suite annotation doesn't execute tests with mvn test command .
Unfortunately, what I found was that changing "-Dtest" to "-Dinclude" simply ran all my default unit tests and ignored the test suite.
So, I was told that I'm "not supposed to do it this way", although it's not clear to me exactly which parts I'm not supposed to do, but I am supposed to use "Tags". Ok, so I tried to use tags.
I also noticed this article: https://www.baeldung.com/junit-filtering-tests , although that article appears to use some deprecated mechanisms, like "#RunWith(JUnitPlatform.class)", so I'm not sure how much I can believe from that article.
I've been trying several variations, and my last attempt is this:
#Suite
#Tag("ComponentTest")
#IncludeTags("ComponentTest")
public class ComponentTestSuite {
}
And I added #Tag("ComponentTest") to my one component test class.
This still doesn't work. I try to run mvn -Dtest=ComponentTestSuite test, and it says "No tests found". If I instead try mvn -Dtests=ComponentTest test that just runs all of my unit tests and ignores my component test suite. If I instead run mvn test -Dgroups=ComponentTest, that gives me another variation of "No tests found" in that it doesn't print that, it just literally executes no tests without giving an error.
Looking at that suite class now, it does seem nonsensical, as it doesn't really "contain" anything anymore, which is unfortunate. It was good to have that list of component test classes in one place. Now, it seems like the suite class is pointless, but I can't get anything to work anyway.
Note that I haven't yet added a "groups" element to my surefire config, and I haven't edited all of my unit tests to add a #Tag("UnitTest") annotation. I've only edited the suite class and the one component test.
Update:
To address a comment, I am specifying the following dependencies (among others):
Version 1.8.2 of
junit-platform-suite
junit-platform-suite-api
junit-platform-launcher
junit-platform-commons
junit-platform-engine
junit-platform-runner
junit-platform-suite-engine
Version 5.8.2 of
junit-jupiter
junit-bom (probably unnecessary)
The entire suite class is this:
import org.junit.jupiter.api.Tag;
import org.junit.platform.suite.api.IncludeTags;
import org.junit.platform.suite.api.SelectClasses;
import org.junit.platform.suite.api.Suite;
#Suite
#Tag("ComponentTest")
#IncludeTags("ComponentTest")
public class ComponentTestSuite {
}

Intellij not showing coverage

I wrote one test case for the controller. Inside that controller, calling one service class method.
When I Selected RunAsCoverage in the IntelliJ test case got passed. But it shows test coverage only for the controller class. For the service class, it's showing 0 percent.
When I run test coverage with the Jacoco plugin it's showing for both controller and service class
Please help with this. Thankyou

Using EvoSuite with JMockit for code coverage

Have created a unit test using EvoSuite and want to use maven build to do code coverage analysis on this unit test. When I run maven build I get ...
JMockit Coverage got reloaded through custom class loader org.evosuite.runtime.instrumentation.EvoClassLoader;
and
Class org.xyz.MyClass$1 was not instrumented by EvoSuite. This could happen if you are running JUnit tests in a way that is not handled by EvoSuite, in which some classes are loaded be reflection before the tests are run. Consult the EvoSuite documentation for possible workarounds for this issue.
So seems like code that is breaking is...
if (!InstrumentedClass.class.isAssignableFrom(clazz)) {
String msg = "Class " + clazz.getName() + " was not instrumented by EvoSuite. " +
"This could happen if you are running JUnit tests in a way that is not handled by EvoSuite, in " +
"which some classes are loaded be reflection before the tests are run. Consult the EvoSuite documentation " +
"for possible workarounds for this issue.";
logger.error(msg);
problem = true;
//throw new IllegalStateException(msg); // throwing an exception might be a bit too extreme }
Is it possible to run the EvoSuite unit test through JMockit? Any suggestions?
before reading this question, I had never heard of JMockit, so it is not something that was under the radar in EvoSuite... :(
anyway, I just added now some documentation about it at:
http://www.evosuite.org/documentation/measuring-code-coverage/
you can read there why you get 0% coverage, and possible workarounds for it.

how to use setCaptureScreenShotOnFailure(true) in selenium

I have a test class to which i have added a constructor containing a
method setCaptureScreenShotOnFailure(true)
There is an assert statement which gets failed in this test
But even though there is no screenshot being captured ( i checked the
selenium server directory)
Can anyone explain how to work with this method in
I understand i cannot use this in my setup method and i can only use
in the individual test classes
Is it correct?
Yes this is only for individual class. However, if you want more effective use then you can implement testng then create a screenshot class which extends to testlistener. So that you can capture screenshot during pass or faliure of tests. refer Selenium Testng Screenshot Listener

How to run this test in NUnit

Why in NUnit when i write :
Assert.AreEqual(ConfigurationManager.ConnectionStrings["FertigungRead"].ConnectionString , "Data Source=server511;Initial Catalog=FERTIGUNG;Persist Security Info=True");
it does not run the test and raises an error : Object reference not set to an instance of an object.
But ConfigurationManager is static class. So how can i run this test?
It is running the test - but the test is failing, because ConfigurationManager.ConnectionStrings["FertigungRead"] is returning null.
See this post about app.config files an NUnit, as that's where it'll be getting the configuration from.
However, I don't really see a test for a config file value as a valuable unit test... is this part of a more reasonable test?

Resources