mardi 2 juin 2015

Testing JSF Managed Bean with Arquillian

i am trying to test my JSf aplication with Arquillian. The problem i am facing is that i am unable to inject the Managed Bean named TicketBean in to my test class which is TicketBeanTest. What i have tried so far to inject the TicketBean in to TicketBeanTest was,

  1. tried using @ManagedProperty but did not work , threw NullPointerException when i called any method on TicketBean
  2. tried using @inject but thew java.lang.RuntimeException , stating could not inject member
  3. tried using

    @Before public void instantiate(){ ticketBean=new TicketBean(); }

    but it failed to call @PostConstruct method inside TicketBean , so again NullPointerException while calling DAO method from inside TicketBean. i am testing my application in JBoss AS container.

following is my Test Class,

@RunWith(Arquillian.class)
public class TicketBeanTest {
    @Inject
    TicketBean ticketBean;

    @Deployment
    public static JavaArchive createDeployment() {
        return ShrinkWrap.create(JavaArchive.class)
                .addClasses(TicketBean.class, Ticket.class, TicketDao.class)
                .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
    }

    @Before
    public void instantiate() {
        ticketBean = new TicketBean();
    }

    @Test
    public void dotest() {
        Ticket ticket = new Ticket();
        ticket.setSummary("JUnite-Arquillian Test");
        ticket.setDescription("Testing from Arquillian");
        ticket.setPriority(2);
        ticket.setStatus(2);
        ticket.setAssignedTo("alok.dac");
        ticket.setProject(100);
        ticket.setTicketId(2);
        Assert.assertEquals("home", ticketBean.updateTicket(ticket));

    }

    @Ignore
    @Test
    public void should_Return_true() {
        Assert.assertTrue(ticketBean.testArquillian());

    }
}

following is the TicketBean class ,

@ManagedBean(name = "ticketBean")
public class TicketBean implements Serializable {

private transient TicketDao tDao;
@PostConstruct
    public void init() {
this.tDao = new TicketDao();
}
public String updateTicket(Ticket ticket) {
        boolean flag = this.tDao.updateTicket(ticket);
        if (flag) {
            FacesContext.getCurrentInstance().addMessage(
                    "",
                    new FacesMessage(FacesMessage.SEVERITY_INFO,
                            "Ticket Updated Successfully !", ""));
            return "home";
        }
        FacesContext.getCurrentInstance().addMessage(
                "",
                new FacesMessage(FacesMessage.SEVERITY_ERROR,
                        "Ticket Updated Failed ! ",
                        "Some error occured , Try Again !"));
        return Constants.ERRPAGE;
    }
}

Aucun commentaire:

Enregistrer un commentaire