rspec/rules/S2699/java/rule.adoc

57 lines
1.9 KiB
Plaintext
Raw Normal View History

2020-06-30 12:48:07 +02:00
A test case without assertions ensures only that no exceptions are thrown. Beyond basic runnability, it ensures nothing about the behavior of the code under test.
This rule raises an exception when no assertions from any of the following known frameworks are found in a test:
2020-06-30 12:48:07 +02:00
* AssertJ
* Awaitility
* EasyMock
* Eclipse Vert.x
* Fest 1.x and 2.x
* Hamcrest
* JMock
* JMockit
* JUnit
* Mockito
* Rest-assured 2.x, 3.x and 4.x
2020-06-30 12:48:07 +02:00
* RxJava 1.x and 2.x
* Selenide
2020-12-23 14:59:06 +01:00
* Spring's ``org.springframework.test.web.servlet.ResultActions.andExpect()``
2020-06-30 12:48:07 +02:00
* Truth Framework
* WireMock
2020-12-23 14:59:06 +01:00
Furthermore, as new or custom assertion frameworks may be used, the rule can be parametrized to define specific methods that will also be considered as assertions. No issue will be raised when such methods are found in test cases. The parameter value should have the following format ``<FullyQualifiedClassName>#<MethodName>``, where ``MethodName`` can end with the wildcard character. For constructors, the pattern should be ``<FullyQualifiedClassName>#<init>``.
2020-06-30 12:48:07 +02:00
2020-12-23 14:59:06 +01:00
Example: ``com.company.CompareToTester#compare*,com.company.CustomAssert#customAssertMethod,com.company.CheckVerifier#<init>``.
2020-06-30 12:48:07 +02:00
== Noncompliant Code Example
----
@Test
public void testDoSomething() { // Noncompliant
MyClass myClass = new MyClass();
myClass.doSomething();
}
----
== Compliant Solution
2020-12-23 14:59:06 +01:00
Example when ``com.company.CompareToTester#compare*`` is used as parameter to the rule.
2020-06-30 12:48:07 +02:00
----
import com.company.CompareToTester;
@Test
public void testDoSomething() {
MyClass myClass = new MyClass();
assertNull(myClass.doSomething()); // JUnit assertion
assertThat(myClass.doSomething()).isNull(); // Fest assertion
}
@Test
public void testDoSomethingElse() {
MyClass myClass = new MyClass();
new CompareToTester().compareWith(myClass); // Compliant - custom assertion method defined as rule parameter
CompareToTester.compareStatic(myClass); // Compliant
}
----