我一直在训练自己参加JUNIT测试。我想测试配置文件是否配置正确?我写了一个测试,它应该抛出一个FileNotFoundException。但是当我运行测试时--尽管它会抛出异常--它会通过测试。我想要设计的是,如果PropertiesConfiguration.configure方法抛出一个异常,它应该会失败测试。我的代码如下:
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.io.FileNotFoundException;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class isPropertiesOfLog4jExistAndConfiguredCorrectly {
Logger log = null;
File f=null;
@Before
public void setUp() throws Exception {
log=Logger.getLogger(GuestBookExample.class.getName());
f=new File("/web-inf/classes/log4j.properties");
}
@After
public void tearDown() throws Exception {
}
@Test
public void test() {
assertTrue("Configuration file is exist.",f!=null);
}
@Test
public void test2(){
PropertyConfigurator.configure("/....web-inf/classes/log4j.properties");
}
}发布于 2014-07-15 07:47:06
PropertyConfigurator配置log4j。您不能修改该类的行为(例如,它如何处理故障)。因此,您只能检查配置是否正确完成。尝试验证记录器的一些属性:
@Test
public void test2(){
PropertyConfigurator.configure("/....web-inf/classes/log4j.properties");
Logger log = Logger.getLogger(GuestBookExample.class.getName());
assertEquals(Level.WARN, log.getLevel());
}https://stackoverflow.com/questions/24736286
复制相似问题