0
我有一个JSF项目一个名为标签和一个XHTML页面一类,称为label.xhtml。在label.xhtml方法Label.getValue()通过注入被调用。测试LabelTest运行的嵌入式容器和在测试方法label.xhtml请求和主体内容将被检查。到目前为止,一切都很好,但我想改变我的测试属性Label.value的值,因此测试可以断言自己的一套价值而不是类标签的Postconstruct法的价值。JSF测试嵌入TomEE:操纵注射对象
我把一个断点类标签的构造。所以我可以看到堆栈跟踪,并阅读了许多这些方法的代码。也许有可能改变生产类,所以我可以以某种方式放置我自己的AbstractProducer?
Code on GitHub或向下滚动。
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");
}
}
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();
}
}
label.xhtml
<html>
<h:body>
#{label.value}
</h:body>
</html>