点,
这不是微不足道的任务,但绝对可以实现您想要的。
首先看看https://code.google.com/p/tadedon上的 Tadedon 项目,
尤其是tadedon-guice-servlet-mock
。
您将需要类似假容器之类的东西来进行测试。我的假容器也包含Apache Shiro
集成,所以你可以把它扔掉,它看起来像:
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.Module;
import com.xemantic.tadedon.guice.servlet.mock.FakeServletContainer;
import com.xemantic.tadedon.guice.servlet.mock.FakeServletContainerModule;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.subject.support.SubjectThreadState;
import org.apache.shiro.web.subject.WebSubject;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import javax.servlet.ServletException;
import java.io.IOException;
import java.util.Arrays;
public class FakeTestContainerInit {
private final FakeServletContainer servletContainer;
private final Injector internalInjector;
private Subject internalSubject;
public FakeTestContainerInit() {
this(new Module[] {});
}
public FakeTestContainerInit(Module... modules) {
super();
modules = Arrays.copyOf(modules, modules.length + 1);
modules[modules.length-1] = new FakeServletContainerModule();
internalInjector = Guice.createInjector(modules);
servletContainer = internalInjector.getInstance(FakeServletContainer.class);
}
public void start() throws ServletException, IOException {
this.start(true);
}
public void start(boolean initializeSecurityContext) throws ServletException, IOException {
getServletContainer().start();
MockHttpServletRequest request = servletContainer.newRequest("GET","/");
MockHttpServletResponse response = new MockHttpServletResponse();
if(initializeSecurityContext) {
SecurityManager scm = internalInjector.getInstance(SecurityManager.class);
internalSubject = new WebSubject.Builder(scm, request, response).buildWebSubject();
SubjectThreadState sts = new SubjectThreadState(internalSubject);
sts.bind();
} else { internalSubject = null; }
getServletContainer().service(request, response);
}
public void stop() {
servletContainer.stop();
}
public FakeServletContainer getServletContainer() {
return servletContainer;
}
public <T> T getInstance(final Class<T> type) throws IOException, ServletException {
return getServletContainer().getInstance(type);
}
public <T> T getInstance(final Key<T> key) throws IOException, ServletException {
return getServletContainer().getInstance(key);
}
public Subject getSubject() {
return internalSubject;
}
}
依赖项:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
<dependency>
<groupId>org.sonatype.sisu</groupId>
<artifactId>sisu-guice</artifactId>
</dependency>
<dependency>
<groupId>com.xemantic.tadedon</groupId>
<artifactId>tadedon-guice-servlet-mock</artifactId>
</dependency>
和你不需要的 Apache Shiro:
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-web</artifactId>
</dependency>
您需要做的就是创建FakeTestContainerInit
、调用start()
和stop()
方法。此外,所有对象的创建都必须通过FakeTestContainerInit.getInstance
测试内部的方法来完成。
好吧,我用它来测试 Vaadin 应用程序,所以我不需要发送请求和检查响应。因此,您将需要实施这一点。可以通过getServletContainer().service(request, response);
. 但我想你会明白的。希望它会帮助你。