开发者

Mock @org.jboss.seam.annotations.in behaviour for unittest

开发者 https://www.devze.com 2023-02-22 10:48 出处:网络
The test: public class BeanTest { private SomeBean target; @Test(groups = \"integration\") public void checkIfAuthenticationWorks() {

The test:

public class BeanTest {

    private SomeBean target;

    @Test(groups = "integration")
    public void checkIfAuthenticationWorks() {

        ApplicationBean applicationBean = mock(ApplicationBean.class);
        target = new SomeBean();

        // Some cool code to inject applicationBean to target class

        assertEquals("token", target.authenticate(USERNAME, PASSWORD));
    }
}

The class:

@AutoCreate
@Name("someBean")
@Scope(ScopeType.SESSION)
public class someBean implements Serializable {

    @Logger
    private static Log log;

    @In
    ApplicationBean applic开发者_如何学GoationBean;

    public String authenticate(String username, String password) {

     // Very cool code!

    return "token";
    }
}

Is there some smart way of solving the applicationBean injection part?

// Jakob


First, make the test the Seam way, that is extending SeamTest:

public class BeanTest extends SeamTest {

    private SomeBean target;

    @Test(groups = "integration")
    public void checkIfAuthenticationWorks() {

        target = (SomeBean) Component.getInstance(SomeBean.class);
        // target get injected with the MockApplicationBean


        assertEquals("token", target.authenticate(USERNAME, PASSWORD));
    }
}

Then, create a MockApplicationBean with MOCK precedence and put it in the test classpath so that it will be injected in place of the real ApplicationBean:

@Name("applicationBean")
@Install(precedence = MOCK)
public class MockApplicationBean extends ApplicationBean
{
  // your mocked ApplicationBean  

}

Finally, note that target must be instantiated as a Seam component, not with "new":

SomeBean target = (SomeBean) Component.getInstance(SomeBean.class);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号