java - Spring Boot MockMVC - request scoped bean not autowired properly -
i want have request scoped bean, fill data in filter , access data in controller. works fine when run app, when try test mockmvc, data not set in request scoped bean in controller.
here code:
pom.xml
<?xml version="1.0" encoding="utf-8"?> <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>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version>1.2.3.release</version> </parent> <groupid>org.lebo.test</groupid> <artifactid>spring-boot-rsb-test</artifactid> <version>1.0-snapshot</version> <packaging>jar</packaging> <properties> <spring-boot.version>1.2.3.release</spring-boot.version> <lombok.version>1.16.2</lombok.version> </properties> <dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter</artifactid> <version>${spring-boot.version}</version> <exclusions> <exclusion> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-logging</artifactid> </exclusion> </exclusions> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-log4j</artifactid> <version>${spring-boot.version}</version> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> <version>${spring-boot.version}</version> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <version>${spring-boot.version}</version> <scope>test</scope> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-actuator</artifactid> <version>${spring-boot.version}</version> </dependency> <dependency> <groupid>org.mockito</groupid> <artifactid>mockito-core</artifactid> <version>1.9.5</version> <scope>test</scope> </dependency> <dependency> <groupid>org.hamcrest</groupid> <artifactid>hamcrest-library</artifactid> <version>1.3</version> <scope>test</scope> </dependency> <dependency> <groupid>org.hamcrest</groupid> <artifactid>hamcrest-core</artifactid> <version>1.3</version> <scope>test</scope> </dependency> <dependency> <groupid>org.projectlombok</groupid> <artifactid>lombok</artifactid> <version>${lombok.version}</version> <scope>provided</scope> </dependency> </dependencies> </project>
application
import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.enableautoconfiguration; import org.springframework.context.applicationcontext; import org.springframework.context.annotation.componentscan; import org.springframework.context.annotation.configuration; @configuration @componentscan @enableautoconfiguration public class application { public static void main(string[] args) { applicationcontext ctx = springapplication.run(application.class, args); } }
requestscopedbean
import lombok.data; import org.springframework.context.annotation.scope; import org.springframework.context.annotation.scopedproxymode; import org.springframework.stereotype.component; @component @scope(value = "request", proxymode = scopedproxymode.target_class) @data public class requestscopedbean { private string data; }
controller
import org.springframework.beans.factory.annotation.autowired; import org.springframework.http.httpstatus; import org.springframework.http.responseentity; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.requestmapping; @controller public class somecontroller { @autowired private requestscopedbean requestscopedbean; @requestmapping("/test") public responseentity test() { return new responseentity(requestscopedbean.getdata(), httpstatus.ok); } }
filter
import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.component; import org.springframework.web.filter.onceperrequestfilter; import javax.servlet.filterchain; import javax.servlet.servletexception; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import java.io.ioexception; @component public class somefilter extends onceperrequestfilter { @autowired private requestscopedbean requestscopedbean; @override protected void dofilterinternal(httpservletrequest httpservletrequest, httpservletresponse httpservletresponse, filterchain filterchain) throws servletexception, ioexception { requestscopedbean.setdata("data"); filterchain.dofilter(httpservletrequest, httpservletresponse); } }
and test:
import org.junit.test; import org.junit.runner.runwith; import org.springframework.beans.factory.annotation.autowired; import org.springframework.boot.test.springapplicationconfiguration; import org.springframework.mock.web.mockhttpsession; import org.springframework.test.context.junit4.springjunit4classrunner; import org.springframework.test.context.web.webappconfiguration; import org.springframework.test.web.servlet.mockmvc; import org.springframework.test.web.servlet.mvcresult; import org.springframework.test.web.servlet.setup.mockmvcbuilders; import org.springframework.web.context.webapplicationcontext; import static org.hamcrest.matcherassert.assertthat; import static org.hamcrest.matchers.is; import static org.springframework.test.web.servlet.request.mockmvcrequestbuilders.get; import static org.springframework.test.web.servlet.result.mockmvcresultmatchers.status; @runwith(springjunit4classrunner.class) @webappconfiguration @springapplicationconfiguration(classes = application.class) public class sometest { @autowired private mockhttpsession session; @autowired private webapplicationcontext wac; @autowired private somefilter somefilter; @test public void should_return_data() throws exception { mockmvc mockmvc = mockmvcbuilders.webappcontextsetup(wac) .addfilter(somefilter) .build(); mvcresult result = mockmvc.perform(get("/test") .session(session)) .andexpect(status().isok()) .andreturn(); assertthat(result.getresponse().getcontentasstring(), is("data")); } }
if change scope "session", test passes.
Comments
Post a Comment