SONAR coding rule for junit assertion - junit

It is great that SONAR can detect junit test without an assertion, but in practice mocking technology may be used during junit test, f.e. EasyMock or Mockito. Then question raises from here: some of the test cases only need verification of mocking object method call, and no assertion is needed. Is there a way to tell SONAR to accept mocking utility verification method as a valid assertion besides junit assertion?
B.R.

Related

Execute Junit Test methods programmatically in queue consumer

Folks,
Is there any api or approach which we can use to fetch each Junit5 TestCase method defined with #Test annotation in code and execute it in SpringBoot/Java programmatically. Could you also please suggest some test report framework where we can upload the results of the test suite execution?
The basic idea is to scale up the automation test case execution by using a message broker. For that, I need to send individual test cases in a queue and get it executed by the consumers. Could you please share your valuable thoughts on how this can be achieved?

What's the difference between the JUnit and the TestNG

Both Testng and Junit are Testing framework used for Unit Testing. So what are the diffrences and advantages of JUnit and the TestNG?
Unit Testing Frameworks
Both JUnit and TestNG are Testing framework used for Unit Testing. TestNG is similar to JUnit. Few more functionalities are added to it that makes TestNG more powerful than JUnit. TestNG is a testing framework inspired by JUnit and NUnit.
Differences
Advantages of TestNG
TestNG Annotations are easy to understand over JUnit.
No constraints like declaring #BeforeClass and #AfterClass in
TestNG, which are present in JUnit.
As method name constraint is present in JUnit, such method name
constraint is not present in TestNG and you can specify any test
method names.
TestNG enables you to group test cases easily which is not possible
in JUnit. TestNG supports following three 3 additional
setUp/tearDown level: #Before/AfterSuite, #Before/AfterTest and
#Before/AfterGroup.
TestNG do not require extend any class.
TestNG allows to execute test cases based on group which isn't
possible in JUnit.
Parallel execution of Selenium test cases is possible in TestNG.

JMockit and parameterized tests (JUnit parameterized or JUnitParams)

I was wondering if JMockit is compatible with JUnit parameterized tests or JUnitParams, because I haven't found a way to make them work together so far, since you can only specify one JUnit runner, and both JMockit, JUnitParams and Parameterized require you to use their own runner.
JMockit does not require you to use an own runner. Using an own runner is just one of the possible ways to make sure JMockit got initialized properly before your tests run. You can also add JMockit as Java agent via commandline parameters, depend on classpath ordering (having JMockit in the classpath before JUnit) or call the JMockit initialization method manually before the actual tests start if you have such a place where you can call it, e. g. if you use an own JUnit runner.

What makes up the "standard jmock libraries"?

I'm following this guide http://javaeenotes.blogspot.com/2011/06/short-introduction-to-jmock.html
I've received the error
java.lang.SecurityException: class "org.hamcrest.TypeSafeMatcher"'s signer information does not match signer information of other classes in the same package.
In the guide the author says:
The solution is make sure the jMock libraries are included before the
standard jUnit libraries in the build path.
What makes up the "standard jmock libraries" and the "junit libraries"?
Junit only has one jar so that's easy, but jmock comes with over 10 different jars.
I've been using: j-unit4.10, jmock-2.5, hamrest-core and hamcrest-library
What are the hamcrest core and library classes for?
i'm a committer on both libraries. JMock depends on hamcrest to help it decide whether an call to an object is expected. I suggest just using the hamcrest-all jar. The split between hamcrest core and library was to separate the fundamental behaviour of matching and reporting differences from a convenient implementations of the most common cases.
Finally, if you're using hamcrest, I suggest you use the junit-dep jar to avoid clashes with some features of hamcrest that are included in the junit.jar
JUnit is used to do Unit test in order to test your methods. JMock is used to test your program inside a context, You will have to know what you are expecting to send to the context (ENV) and what will answer the context.
JMock use JUnit, that is why, in order to avoid dependency conflicts, you need to include it before JUnit.
The 10 libraries of JMock are kind of add-ons if you need to use JMock script or any other functionnality not available in the JMock core.
You don't need to know about Hamcrest-core library to use JMock. Just follows the guide on the web site (don't use version 1 of JMock) and Organize your libraries in the correct order (JUnit should be last in order to avoid your error)
mock frameworks licke jmock do some black magic behind the scenes
( including, but not limited to runtime byte code manipulation )
to provide mock methods classes and whatever. To be able to do this,
some tweaks in basic junit classes are necessary, and the only way to do this is to
register itself as java agent before JU classes are loaded.
Also, put your mock framework before junit in classpath

How can I write a JUnit test for a JNI call that crashes?

I'm working on fixing a JNI call that's crashing at the moment. Striving to be a good TDD practitioner, I've created a JUnit test to replicate the problem, which it does admirably.
However, by crashing, the JNI call never returns. Is it possible to write a JUnit test that reports a failure on a JNI crash?
Ouch, if your JNI is crashing the JVM, then that's certainly going to be difficult to test from junit. You can't, for example, hook up a signal handler for SIGSEGV, the JVM's just going to die.
If it were me, I would change this around a little bit and create a simple Java class with a Main that calls the crashing native code, and in my junit test I would execute a new instance of a Java runtime that runs that wrapper class using Runtime#exec.
You can then wait for the resultant process to finish and check its return code - if it's non-zero, there was a problem in execution.
try using the timeout parameter: http://junit.sourceforge.net/javadoc/org/junit/Test.html if you are using junit 4, otherwise something like: http://netbeans.org/kb/docs/java/junit-intro.html#Exercise_243