vendredi 21 avril 2017

JSF-Testing with embedded TomEE: Manipulate injected object

I have a JSF-Project with one class called Label and one xhtml-page, called label.xhtml. In label.xhtml the method Label.getValue() is called via injection. The test LabelTest runs a embedded container and in the a test method label.xhtml is requested and the body content will be checked. So far everything is fine, but I would like to change the value of the attribute Label.value in my test so the test can assert his own set value and not the value of the Postconstruct-method of class Label.

I put a breakpoint in the constructor of class Label. So I can see the stacktrace and I read the code of many of these methods. Maybe it is possible to change the produces class, so I can put there my own AbstractProducer in some way?

Stacktrace Label constructor

Code on GitHub or scroll down.

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 requestIndexHtml_ThenBodyContainsPostConstructValue() {
        assertEquals("HELLO", getBodyValue());
    }

    @Test
    public void manipulateInjectedObjectAndRequestIndexHtml_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>

Aucun commentaire:

Enregistrer un commentaire