【问题标题】:Mock @org.jboss.seam.annotations.in behaviour for unittest模拟单元测试的@org.jboss.seam.annotations.in 行为
【发布时间】:2011-04-04 12:55:46
【问题描述】:

测试:

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));
    }
}

班级:

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

    @Logger
    private static Log log;

    @In
    ApplicationBean applicationBean;

    public String authenticate(String username, String password) {

     // Very cool code!

    return "token";
    }
}

有解决applicationBean注入部分的聪明方法吗?

//雅各布

【问题讨论】:

    标签: seam testng mockito


    【解决方案1】:

    首先,以Seam方式进行测试,即扩展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));
        }
    }
    

    然后,创建一个具有MOCK 优先级的MockApplicationBean 并将其放入测试类路径中,以便将其注入到真正的ApplicationBean 的位置:

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

    最后,注意target必须被实例化为Seam组件,而不是“new”:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-04
      • 2013-11-12
      • 2018-11-21
      • 2018-10-19
      • 2013-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多