2023-01-01

Controller tests using MockMvcBuilder for spring-framework (non spring-boot) projects

Until now where you have a spring-framework project that is not using spring-boot, Diffblue Cover would produce non-canonical Spring tests (still using @Mock and @InjectMock however). In this release Cover now writes tests using MockMvcBuilder. As you can see below, the example uses MockMvc to test the method.

  @ExtendWith(MockitoExtension.class)
  class WebControllerDiffblueTest {
  @InjectMocks
  private WebController webController;

  @Mock
  private WebSvc webSvc;
  /**
  * Method under test: {@link WebController#getId(String)}
  */
  @Test
  void testGetId() throws Exception {
    // Arrange
    when(webSvc.nextId()).thenReturn(123);
    MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.get("/id").param("prefix", "foo");

    // Act and Assert
    MockMvcBuilders.standaloneSetup(webController)
        .build()
        .perform(requestBuilder)
        .andExpect(MockMvcResultMatchers.status().isOk())
        .andExpect(MockMvcResultMatchers.content().contentType("text/plain;charset=ISO-8859-1"))
        .andExpect(MockMvcResultMatchers.content().string("foo123"));
    }
  }

Tests for Spring Controllers with Map-valued RequestBody that create non-empty maps

In this release Cover is able to produce tests with valuable maps where a Spring Controller has Map-valued RequestBody that create non-empty maps. For Spring Controller methods with a @RequestBody Map argument Cover looks for relevant keys and values to populate the map in the resulting test, providing higher coverage, as shown in the following example:

  @PutMapping("/hr/pass")
  public RespBean updateHrPasswd(@RequestBody Map<String, Object> info) {
    String oldpass = (String) info.get("oldpass");
    String pass = (String) info.get("pass");
    Integer hrid = (Integer) info.get("hrid");
    if (hrService.updateHrPasswd(oldpass, pass, hrid)) {
      return RespBean.ok("Update successful!");
    }
    return RespBean.error("Update failed!");
  }

Enhancements

  • Cover now writes Controller tests using MockMvcBuilder for spring-framework projects without spring-boot. [Ref: TG-18388]

  • Cover now writes better tests for Spring Controller methods with @RequestBody parameters. [Ref: TG-17827]

  • IntelliJ Plugin: Cover now reports E067 (JVM not a 64-bit implementation) if a 32-bit JVM has been detected. [Ref: TG-17612]

  • Reports: The 'Overview dashboard' now includes a counter showing the total number of lines of code in the project. [Ref: TG-18368]

  • Reports: Performance improvements. [Ref: TG-18375]

Resolved Issues

  • CLI: Resolved an issue which could cause Cover to discard the Gradle stack trace, rather than logging it to the user log file when an error with the build system is encountered. [Ref: TG-18365]

  • CLI: Resolved an issue which could cause Cover to rebuild all Gradle modules when tests are written. [Ref: TG-18349]

  • Resolved an issue which could cause Cover to not identify some Spring services, causing R013 (No inputs found that don't throw a trivial exception) to be reported. [Ref: TG-17392]

  • Reports: Resolved an issue which could cause Cover to not halt with an ERROR whilst running Cover CLI with the --coverage-reports option with an incompatible version of Surefire. [Ref: TG-17288]

Known Issues

  • Cover may produce tests for JUnit 4.10 and lower that do not compile due to the use of assertNotEquals. [Ref: TG-17605]

  • Reports: Firefox users may encounter It seems like we encountered an error. Try refreshing this page or contact your administrator. while loading Coverage Reports. [Ref: TG-18257]

  • IntelliJ Plugin: Upgrading to IntelliJ IDEA 2022.3 may, in some circumstances, cause existing run configurations to no longer produce tests. Removing any run configurations for that entity and writing tests again will recreate a working run configuration. [Ref: TG-18282]

Last updated