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>