Modelling View Controller Unit Test with Mockite and Spring

Asked

Viewed 153 times

0

I need to mount the unit test for the controller below (for example only):

import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RestController(value = "/test")
public class TestController {

    @GetMapping(value = "/list")
    public ModelAndView list(ModelAndView model, HttpServletRequest request) {
        model.addObject("returnedAttribute", request.getSession().getAttribute("test"));
        return model;
    }
}

I received the suggestion below from colleague @nullptr in another topic:

@Test
public void testIniciaTela() {

    ModelAndView modelAndView = new ModelAndView();
    ModelAndView modelResultado = controller.iniciaTela(modelAndView, getMockServletRequest());
    assertEquals(
        "1234", 
        modelResultado.getModelMap().get(AbstractConstantes.PERFIL_USUARIO_LOGADO)
    );

}

private HttpServletRequest getMockServletRequest() {
    MockHttpServletRequest mockRequest = new MockHttpServletRequest(); //aqui está dando erro...
    mockRequest.setSession(getMockSession());
    return mockRequest;
}

private HttpSession getMockSession() {

    PerfilUsuarioLogadoVO perfilUsuarioLogado = new PerfilUsuarioLogadoVO();
    perfilUsuarioLogado.setNuUnidade("1234");

    MockHttpSession mockSession = new MockHttpSession();
    mockSession.setAttribute(AbstractConstantes.PERFIL_USUARIO_LOGADO, perfilUsuarioLogado);

    return mockSession;

}

But I’m getting this mistake:

java.lang.NoSuchMethodError: org.springframework.util.StreamUtils.emptyInput()Ljava/io/InputStream;
at org.springframework.mock.web.MockHttpServletRequest.<clinit>(MockHttpServletRequest.java:102)
at xxx.controller.ManterEnvioManualControllerTest.getMockServletRequest(ManterEnvioManualControllerTest.java:110)

when trying to run the line below:

MockHttpServletRequest mockRequest = new MockHttpServletRequest();

My file pom.xml is like this:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>br.xxx.sigms</groupId>
    <artifactId>sigms</artifactId>
    <version>4.0.0-SNAPSHOT</version>
</parent>
<artifactId>sigms-web</artifactId>
<packaging>war</packaging>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<name>SIGMS - WEB</name>
<dependencies>
    <dependency>
        <groupId>br.xxx.sigms</groupId>
        <artifactId>sigms-common</artifactId>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>br.xxx.sigms</groupId>
        <artifactId>sigms-dao</artifactId>
        <type>ejb</type>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>br.xxx.sigms</groupId>
        <artifactId>sigms-ws</artifactId>
        <scope>provided</scope>
    </dependency>
    <!-- Testes -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <scope>test</scope>
    </dependency>
    <!-- Testes fim -->

    <dependency>
        <groupId>org.jboss.logmanager</groupId>
        <artifactId>log4j-jboss-logmanager</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>commons-lang</groupId>
        <artifactId>commons-lang</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>commons-beanutils</groupId>
        <artifactId>commons-beanutils</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.codehaus.groovy</groupId>
        <artifactId>groovy-all</artifactId>
        <version>${version.groovy.all}</version>
    </dependency>
    <dependency>
        <groupId>org.jboss.spec.javax.ejb</groupId>
        <artifactId>jboss-ejb-api_3.2_spec</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>${version.spring.aop}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${version.spring.web}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${version.spring.webmvc}</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>${version.jackson.mapper.asl}</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>${version.jackson.core}</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>${version.jackson.databind}</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.ldap</groupId>
        <artifactId>spring-ldap-core</artifactId>
        <version>${version.spring.ldap.core}</version>
    </dependency>

    <dependency>
        <groupId>org.sitemesh</groupId>
        <artifactId>sitemesh</artifactId>
        <version>${version.sitmesh}</version>
    </dependency>
    <dependency>
        <groupId>commons-pool</groupId>
        <artifactId>commons-pool</artifactId>
        <version>${version.commons.pool}</version>
    </dependency>
    <dependency>
        <groupId>org.jboss.spec.javax.servlet</groupId>
        <artifactId>jboss-servlet-api_3.1_spec</artifactId>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>${version.jstl}</version>
    </dependency>
    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>${version.javax.validation.api}</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>br.xxx.sigms</groupId>
        <artifactId>sigms-core</artifactId>
        <version>${project.version}</version><!--$NO-MVN-MAN-VER$ -->
        <scope>provided</scope>
    </dependency>
    <!-- Spring Security -->
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-web</artifactId>
        <version>${version.org.spring.framework.sucurity}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-config</artifactId>
        <version>${version.org.spring.framework.config}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-taglibs</artifactId>
        <version>${version.org.spring.framework.sucurity}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-ldap</artifactId>
        <version>${version.org.spring.framework.ldap}</version>
    </dependency>

    <dependency>
        <groupId>net.sf.jasperreports</groupId>
        <artifactId>jasperreports</artifactId>
        <version>${version.jasperreports}</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>com.thoughtworks.xstream</groupId>
        <artifactId>xstream</artifactId>
        <version>1.4.8</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <version>${version.org.spring.framework.boot}</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>test</scope>
    </dependency>

</dependencies>

<build>
    <finalName>${project.artifactId}</finalName>
    <plugins>
        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>${version.war.plugin}</version>
            <configuration>
                <warSourceDirectory>src/main/webapp</warSourceDirectory>
                <!-- Java EE 6 doesn't require web.xml, Maven needs to catch up! -->
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>
    </plugins>
</build>

The properties of pom.xml of the parent project (where dependencies versions are defined) is like this:

    <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <jvm-version>1.7</jvm-version>
    <!-- maven-compiler-plugin -->
    <maven.compiler.target>1.7</maven.compiler.target>
    <maven.compiler.source>1.7</maven.compiler.source>

    <!-- other plugin versions -->
    <version.xxx.parent>4.0.0-SNAPSHOT</version.xxx.parent>
    <version.ejb.api>3.1</version.ejb.api>
    <version.commons.pool>1.6</version.commons.pool>
    <version.org.spring.framework.sucurity>3.2.3.RELEASE</version.org.spring.framework.sucurity>
    <version.org.spring.framework.web>3.2.3.RELEASE</version.org.spring.framework.web>
    <version.org.spring.framework.config>3.2.3.RELEASE</version.org.spring.framework.config>
    <version.org.spring.framework.ldap>3.2.8.RELEASE</version.org.spring.framework.ldap>
    <version.org.spring.framework.boot>2.2.2.RELEASE</version.org.spring.framework.boot>
    <version.javax.validation.api>1.1.0.Final-redhat-1</version.javax.validation.api>
    <version.jstl>1.2</version.jstl>
    <version.javax.servlet.servlet.api>2.3</version.javax.servlet.servlet.api>
    <version.javax.servlet.servlet.api>1.0.0.Final-redhat-1</version.javax.servlet.servlet.api>
    <version.sitmesh>3.0.0</version.sitmesh>
    <version.spring.ldap.core>2.0.4.RELEASE</version.spring.ldap.core>
    <version.spring.ldap>1.2.1</version.spring.ldap>
    <version.jackson.core>2.8.9.redhat-1</version.jackson.core>
    <version.jackson.databind>2.8.9.redhat-1</version.jackson.databind>
    <version.jackson.mapper.asl>1.9.13.redhat-4</version.jackson.mapper.asl>
    <version.spring.webmvc>4.1.4.RELEASE</version.spring.webmvc>
    <version.spring.web>4.1.4.RELEASE</version.spring.web>
    <version.spring.context>4.1.4.RELEASE</version.spring.context>
    <version.spring.aop>4.1.4.RELEASE</version.spring.aop>
    <version.groovy.all>1.7.5</version.groovy.all>
    <version.arquillian.protocol.servlet>1.1.5.Final</version.arquillian.protocol.servlet>
    <version.slf4j.simple>1.7.7</version.slf4j.simple>
    <version.weld.core>1.1.23.Final-redhat-1</version.weld.core>
    <version.arquillian.weld.ee.embedded>1.1.2.Final</version.arquillian.weld.ee.embedded>
    <version.jboss.as.arquillian.container.managed>7.1.1.Final</version.jboss.as.arquillian.container.managed>
    <version.jboss.javaee>3.0.2.Final-redhat-4</version.jboss.javaee>
    <version.arquillian.protocol.servlet>1.1.5.Final</version.arquillian.protocol.servlet><!--$NO-MVN-MAN-VER$ -->
    <version.arquillian.bom>1.1.7.Final</version.arquillian.bom>
    <version.maven.surefire.plugin>2.12</version.maven.surefire.plugin>
    <version.maven.compiler>2.3.2</version.maven.compiler>
    <version.sp1.rehat>4.2.14.SP1-redhat-1</version.sp1.rehat>
    <version.spring.context.support>3.1.2.RELEASE</version.spring.context.support>
    <version.jexelapi.jxl>2.6.10</version.jexelapi.jxl>
    <version.jasperreports>6.4.0</version.jasperreports>
    <version.ear.plugin>2.8</version.ear.plugin>
    <version.ejb.plugin>2.3</version.ejb.plugin>
    <version.surefire.plugin>2.10</version.surefire.plugin>
    <version.war.plugin>2.1.1</version.war.plugin>
    <version.junit>4.11</version.junit>
    <version.log4j>1.1.4.Final-redhat-1</version.log4j>
    <version.commons.beanutils>1.9.2.redhat-1</version.commons.beanutils>
    <version.commons.lang>2.6.0.redhat-6</version.commons.lang>
    <version.jboss.ejb.api_spec>1.0.0.Final-redhat-1</version.jboss.ejb.api_spec>
    <version.hibernate.jpa.api>1.0.0.Final-redhat-2</version.hibernate.jpa.api>
    <version.hibernate.core>5.1.10.Final-redhat-1</version.hibernate.core>
    <version.hibernate.entitymanager>5.1.10.Final-redhat-1</version.hibernate.entitymanager>
    <version.hibernate.infinispan>5.1.10.Final-redhat-1</version.hibernate.infinispan>
    <version.jboss.jsf.api>2.1.28.Final-redhat-1</version.jboss.jsf.api>
    <version.jboss.servlet.api>1.0.0.Final-redhat-1</version.jboss.servlet.api>
    <version.jboss.jms.api>1.0.1.Final-redhat-1</version.jboss.jms.api>
    <version.primefaces>5.0</version.primefaces>
    <version.commons.digester>2.1</version.commons.digester>
    <version.cdi.api>1.2.0.redhat-2</version.cdi.api>
    <version.javax.inject>1.0.0.redhat-6</version.javax.inject>
    <version.hibernate.validator>5.3.5.Final-redhat-2</version.hibernate.validator>
    <version.jboss.ejb3>2.2.0.Final-redhat-1</version.jboss.ejb3>
    <version.org.infinispan>5.2.11.Final-redhat-2</version.org.infinispan>
    <!--<version.hibernate-c3p0>5.4.4.Final</version.hibernate-c3p0>-->
    <version.drive-oracle>12.2.0.1</version.drive-oracle>
    <version.h2>1.4.192</version.h2>
</properties>
  • @nullptr there is the continuation of our conversation.

  • I know what it could be, so I’ll answer if no one answers first :)

  • 1

    Let’s link to another issue here also for knowledge

1 answer

1


The problem is the compatibility between the versions of Jars you are using, your project is based on Spring 4.1.4.RELEASE according to its dependencies:

<version.spring.webmvc>4.1.4.RELEASE</version.spring.webmvc>
<version.spring.web>4.1.4.RELEASE</version.spring.web>
<version.spring.context>4.1.4.RELEASE</version.spring.context>
<version.spring.aop>4.1.4.RELEASE</version.spring.aop>

But for testing you are using a dependency on Spring Boot, which causes compatibility problems between the libraries of your project:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <version>${version.org.spring.framework.boot}</version>
    <scope>test</scope>
</dependency>

I suggest using the test dependency compatible with the library version of your project, which will probably be this:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>4.1.4.RELEASE</version>
    <scope>test</scope>
</dependency>

Spring Boot has brought many new features, but unfortunately its libraries are not compatible with older versions of Spring

  • 1

    That must be right, man. I had to follow up on other parts of the project but soon I will resume to test your solution. A lot !!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.