在某项目中,需要编写单元测试对LDAP工具类进行测试,但是此工具类中依赖了项目的配置文件,此文提供一种编写这种情况下的单元测试方法。

1.在web构件中编写Test类"xxx-web/src/test/java/com/xxx/web/test/base/common/LdapUtilTest.java"

2.需要在类上添加注解

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration({"/spring.xml", "/spring-hibernate.xml"})
public class LdapUtilTest {
    #...
    @Test
    public void testLdapUtil(){
        #...
    }
}

3.等待服务器启动即可,如果项目较大,可能需要一段时间。

上一篇 下一篇