import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.ApplicationContext; import org.springframework.test.context.junit4.SpringRunner; import java.io.File; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; /** * @author liuam * @since 2018/6/25 0025 下午 17:08 */ @RunWith(SpringRunner.class) @SpringBootTest(classes = SpringBootTestApplication.class) public class SpringBootTestApplication { @Autowired private ApplicationContext applicationContext; @Test public void contextLoad() { Class objClass = applicationContext.getClass(); String strRealPath = objClass.getClassLoader().getResource("").getFile(); try { strRealPath = URLDecoder.decode(strRealPath, "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } File objFile = new File(strRealPath); String classPath = objFile.getParent() + File.separator; if (classPath.contains("/")) { classPath = "/" + classPath; } System.out.println(classPath); File file = new File(classPath); String appPath = file.getParent() + File.separator; System.out.println(appPath); file = new File(appPath); String filePath = file.getParent() + File.separator; System.out.println(filePath); } }