本文介绍了如何在吊索模型中锁定RequestAttribute值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个适应@Model(Adapables=SlingHttpServletRequest.class)的吊索模型,并且有一个请求属性
现在我想模拟上面的参数,并使用True/False值测试代码。为此,我尝试了以下方法,但均未奏效
// register adapter factory
BundleContext bundleContext = MockOsgi.newBundleContext();
MockSling.setAdapterManagerBundleContext(bundleContext);
// prepare sling request
ResourceResolver resourceResolver = MockSling.newResourceResolver(bundleContext);
MockSlingHttpServletRequest request = new
MockSlingHttpServletRequest(resourceResolver);
request.setAttribute("isRegistration", "true");
和
@Mock
SlingHttpServletRequest slingHttpServletRequest;
@Mock
RequestParameter requestParameter;
Mockito.when(slingHttpServletRequest.getRequestParameter("isRegistration")).thenReturn(requestParameter);
Mockito.when(requestParameter.getString()).thenReturn("true");
有没有其他方法来模拟这些类型的变量,或者我在这里漏掉了什么?
推荐答案
使用Mockito进行Sling/AEM JUnit测试不会有多大效果。模仿一个相当复杂的产品将是一个永无止境的故事--只是为了你的Sling模型中的几行代码。
最好使用Sling Mocks或AEM Mocks!
那么您的吊带型号:
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.models.annotations.Default;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.RequestAttribute;
@Model(adaptables = SlingHttpServletRequest.class)
public class TestModel1 {
@RequestAttribute
@Default(booleanValues = false)
private boolean isRegistration;
public boolean isRegistration() {
return isRegistration;
}
}
可以使用这个基于Sling Mock的JUnit测试进行测试。AEM模拟几乎是相同的,只是它还提供了许多AEM服务和功能来进行测试。
import static org.junit.Assert.*;
import org.apache.sling.models.factory.ModelFactory;
import org.apache.sling.testing.mock.sling.junit.SlingContext;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
public class TestModel1Test {
@Rule
public final SlingContext context = new SlingContext();
private ModelFactory modelFactory;
@Before
public void setUp() {
context.addModelsForClasses(TestModel1.class);
this.modelFactory = context.getService(ModelFactory.class);
}
@Test
public void isRegistrationTrue() {
context.request().setAttribute("isRegistration", true);
TestModel1 model = modelFactory.createModel(context.request(), TestModel1.class);
assertTrue(model.isRegistration());
}
@Test
public void isRegistrationFalse() {
context.request().setAttribute("isRegistration", false);
TestModel1 model = modelFactory.createModel(context.request(), TestModel1.class);
assertFalse(model.isRegistration());
}
@Test
public void isRegistrationDefault() {
TestModel1 model = modelFactory.createModel(context.request(), TestModel1.class);
assertFalse(model.isRegistration());
}
}
PS:不确定您是否真的想在您的模型中使用@RequestAttribute注入器。它在Sling中不是查询参数。有关详细信息,请参阅here。那么您的SlingModel应该是
import java.util.Optional;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.request.RequestParameter;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.Self;
@Model(adaptables = SlingHttpServletRequest.class)
public class TestModel2 {
@Self
private SlingHttpServletRequest request;
public boolean isRegistration() {
return Optional.of(request)
.map(req -> req.getRequestParameter("isRegistration"))
.map(RequestParameter::getString)
.map(BooleanUtils::toBooleanObject)
.orElse(false);
}
}
测试将是:
import static org.junit.Assert.*;
import org.apache.sling.models.factory.ModelFactory;
import org.apache.sling.testing.mock.sling.junit.SlingContext;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
public class TestModelTest2 {
@Rule
public final SlingContext context = new SlingContext();
private ModelFactory modelFactory;
@Before
public void setUp() {
context.addModelsForClasses(TestModel2.class);
this.modelFactory = context.getService(ModelFactory.class);
}
@Test
public void isRegistrationTrue() {
context.request().setQueryString("isRegistration=true");
TestModel2 model = modelFactory.createModel(context.request(), TestModel2.class);
assertTrue(model.isRegistration());
}
@Test
public void isRegistrationFalse() {
context.request().setQueryString("isRegistration=false");
TestModel2 model = modelFactory.createModel(context.request(), TestModel2.class);
assertFalse(model.isRegistration());
}
@Test
public void isRegistrationDefault() {
TestModel2 model = modelFactory.createModel(context.request(), TestModel2.class);
assertFalse(model.isRegistration());
}
}
这是运行代码所需的最小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>
<groupId>org.example</groupId>
<artifactId>simpletest</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.api</artifactId>
<version>2.22.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.models.api</artifactId>
<version>1.3.8</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.testing.sling-mock.junit4</artifactId>
<version>3.0.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
这篇关于如何在吊索模型中锁定RequestAttribute值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!