我有一个名为标签的一个JSF项目和一个名为 label.xhtml 的xhtml页面。在 label.xhtml 中,通过注入调用方法 Label.getValue()。测试 LabelTest 运行嵌入式容器,并在测试方法中请求 label.xhtml ,并检查正文内容。到目前为止一切都很好,但我想在我的测试中更改属性 Label.value 的值,以便测试可以断言自己的设置值而不是Postconstruct-class类的值的标签即可。
我在类标签的构造函数中放置了一个断点。所以我可以看到堆栈跟踪,并且我读了很多这些方法的代码。也许有可能改变产品类,所以我可以用某种方式把我自己的 AbstractProducer 放在那里?
Code on GitHub或向下滚动。
PagerAdapter
import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;
@Named
@RequestScoped
public class Label {
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@PostConstruct
public void fillValue() {
setValue("HELLO");
}
}
label.xhtml
import org.apache.tomee.embedded.EmbeddedTomEEContainer;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import javax.ejb.embeddable.EJBContainer;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import static org.junit.Assert.assertEquals;
public class LabelTest {
private WebDriver driver;
@Before
public void setup() {
driver = new HtmlUnitDriver();
Map<Object, Object> properties = new HashMap();
properties.put(EJBContainer.PROVIDER, EmbeddedTomEEContainer.class);
properties.put(EJBContainer.MODULES, new File[]{new File("src/main/webapp/")});
properties.put(EJBContainer.APP_NAME, "hfe");
System.setProperty("tomee.webapp.externalRepositories", "build/classes/main,build/classes/test");
EmbeddedTomEEContainer.createEJBContainer(properties);
}
@After
public void cleanup() {
driver.close();
}
@Test
public void requestHtmlPage_ThenBodyContainsPostConstructValue() {
assertEquals("HELLO", getBodyValue());
}
@Test
public void manipulateInjectedObjectAndRequestHtmlPage_ThenBodyContainsValueOfManipulatedInjectedObject() {
// how is it possible to manipulate the injected object with value=MY_VALUE?
assertEquals("MY_VALUE", getBodyValue());
}
private String getBodyValue() {
driver.get("http://localhost:8080/hfe/faces/label.xhtml");
WebElement body = driver.findElement(By.tagName("body"));
return body.getText();
}
}
答案 0 :(得分:0)
我从TomEE User MailingList获得了一个解决方案:使用注释@Specializes或使用ServletFilter。您可以在GitHub项目中找到示例代码。