ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
目录与包的结构无需匹配:源代码可以在文件系统的任意位置。 如下示例 ![](https://box.kancloud.cn/00b6d48bb008a68e24dce158cbce0330_461x624.png) 在测试源代码目录src/test/kotlin 下面新建一个包,跟 src/main/kotlin 下的包是同一个包`package com.easy.kotlin ` 然后,在此包下面新建一个测试类PackageDemoTest * `PackageDemo.kt`在`src/main/kotlin`下的包`package com.easy.kotlin `中 * `DefaultPackageDemo.kt`在在`src/main/kotlin`中 * 测试类PackageDemoTest在`src/test/kotlin`下的包`package com.easy.kotlin `中 * 测试类DefaultPackageDemoTest在`src/test/kotlin`中 **PackageDemo.kt**代码如下 ``` package com.wsc.study fun main(args: Array<String>) { println("hello kotlin") } fun what(){ println("This is WHAT ?") } class Motorbike{ fun drive(){ println("Drive The Motorbike .........") } } ``` **测试类PackageDemoTest**代码如下 ``` package com.wsc.study import org.junit.Test import org.junit.runner.RunWith import org.junit.runners.JUnit4 import now import Car @RunWith(JUnit4::class) class PackageDemoTest { @Test fun testWhat(){ what() } @Test fun testDriveMotorbike(){ val motorbike=Motorbike() motorbike.drive() } @Test fun testDefaultPackage(){ now() val car=Car() car.drive() } } ``` 上面我们使用JUnit4测试框架。在 build.gradle 中的依赖是 ~~~ testCompile group: 'junit', name: 'junit', version: '4.12' ~~~ 右击测试类PackageDemoTest,点击执行 ![](https://box.kancloud.cn/368b0182c765c42f3382292ca6cd71d1_833x716.png) 运行结果如下 ![](https://box.kancloud.cn/c1364accff4ad4a947f6e831ff9f93e4_1093x847.png) 另外,如果我们不定义package命令空间,则默认在根级目录。例如直接在 src/main/kotlin 源代码目录下面新建 **DefaultPackageDemo.kt** 类 ``` import java.util.* fun now(){ println("Now Date is:"+Date()) } class Car{ fun drive(){ println("Drive The Car....") } } ``` 同样在 src/test/kotlin 目录下面新建测试类**DefaultPackageDemoTest** ``` import org.junit.Test import org.junit.runner.RunWith import org.junit.runners.JUnit4 @RunWith(JUnit4::class) class DefaultPackageDemoTest { @Test fun testDefaultPackage(){ now() val car=Car() car.drive() } } ``` 右击测试类DefaultPackageDemoTest,点击执行,运行结果如下 ![](https://box.kancloud.cn/9cce23d4ec0942cd25e53cc0adaf2141_1100x692.png) 我们不需要import now() 函数和 Car 类,可以直接调用。 如果我们在` src/test/kotlin/com/easy/kotlin/PackageDemoTest.kt` 测试类里面调用 now() 函数和 Car 类, 我们按照下面的方式import ``` import now import Car ``` 如果不在同一个package下面,我们就需要import对应的类和函数。例如,我们在src/test/kotlin 目录下新建一个`package com.easy.kotlin.test `, 使用 `package com.easy.kotlin `下面的类和函数,如下图所示 ![](https://box.kancloud.cn/cd169209a0a58714a7b387e62b57ab15_475x405.png) 我们在src/test/kotlin的包package com.easy.kotlin下又新建一个包test,即`package com.easy.kotlin.test `,在这个包下我们创建**测试类PackageDemoTest2** ``` package com.wsc.study.test import com.wsc.study.Motorbike import com.wsc.study.what import org.junit.Test import org.junit.runner.RunWith import org.junit.runners.JUnit4 @RunWith(JUnit4::class) class PackageDemoTest2 { @Test fun testWhat(){ what() } @Test fun testDriveMotorbike(){ val motorbike=Motorbike() motorbike.drive() } } ``` 测试类PackageDemoTest2类使用到了`package com.easy.kotlin `下面的类Motorbike和函数what(),我们使用`import com.wsc.study.Motorbike `导入类,直接使用`import com.wsc.study.what `导入包级函数。