rspec/rules/S5970/java/rule.adoc

59 lines
1.7 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2021-04-28 16:49:39 +02:00
The Spring framework comes with dedicated classes to help writing better and simpler unit tests. In particular, when testing applications built on top of Spring MVC, it is recommended to use Spring's ``++ModelAndViewAssert++`` assertions class, instead of manually testing MVC's properties.
 
This rule raises an issue when Spring's ``++ModelAndViewAssert++`` assertions should be used instead of manual testing.
=== Noncompliant code example
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,java]
2021-04-28 16:49:39 +02:00
----
ModelAndView mav = getMyModelAndView();
Assert.assertEquals("register", mav.getViewName());
Assert.assertTrue((Boolean) mav.getModelMap().get("myAttribute"));
Assert.assertFalse((Boolean) mav.getModelMap().get("myAttribute"));
Assert.assertEquals(myObject, mav.getModelMap().get("myAttribute"));
----
=== Compliant solution
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,java]
2021-04-28 16:49:39 +02:00
----
ModelAndView mav = getMyModelAndView();
ModelAndViewAssert.assertViewName(mav, "register");
ModelAndViewAssert.assertModelAttributeValue(mav, "myAttribute", Boolean.TRUE);
ModelAndViewAssert.assertModelAttributeValue(mav, "myAttribute", Boolean.FALSE);
ModelAndViewAssert.assertModelAttributeValue(mav, "myAttribute", myObject);
----
== Resources
2021-04-28 16:49:39 +02:00
* https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/testing.html#unit-testing-spring-mvc[Unit Testing Spring MVC]
* https://docs.spring.io/spring-framework/docs/5.2.8.RELEASE/javadoc-api/org/springframework/test/web/ModelAndViewAssert.html[ModelAndViewAssert Javadoc]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Replace this assertion by "ModelAndViewAssert.assert<...>"
=== Highlighting
The statement doing the assertion
endif::env-github,rspecator-view[]