Java 如何配置和运行 Mockito 测试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21492247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:00:07 来源:igfitidea点击:
How to configure and run a Mockito test
提问by Big O
I am working on an Android app which has some pure java based components that i want to test using Mockito.
我正在开发一个 Android 应用程序,它有一些我想使用 Mockito 测试的纯基于 Java 的组件。
I have some questions about configuring Mockito with my code base.
我有一些关于使用我的代码库配置 Mockito 的问题。
- Should I create a test project in my code base?
- Does Mockito has anything to do with JUnit? Do i need to include JUnit libs as well?
- How do i run the Mockito test from Eclispe as opposed to JUnit test which is far more easier to run by doing Run As-> JUnit Test.
- 我应该在我的代码库中创建一个测试项目吗?
- Mockito 与 JUnit 有什么关系吗?我还需要包含 JUnit 库吗?
- 我如何从 Eclispe 运行 Mockito 测试,而不是 JUnit 测试,后者通过执行 Run As-> JUnit 测试更容易运行。
Kindly suggest some tutorial that could demo the setup and running of tests.
请推荐一些可以演示测试设置和运行的教程。
采纳答案by Dawood ibn Kareem
- You don't need to create a separate test project, but you can if you want to. Most people do it by creating a separate test folder in the same project. Whether you have a separate test project or just a test folder, you should replicate your package structure inside your test folder, so that each test class is testing an application class in the same package.
- Yes. Mockito is designed to be used with JUnit, or with some other testing framework, such as TestNG. Most people use it with JUnit. Most of the examples on the Mockito web site use JUnit. So, yes, you'll need the JUnit libraries, or the libraries for whatever other testing framework you choose.
- A Mockito test isa JUnit test (or a TestNG test, or whatever). So there's no "far more easier to run", as you put it. Just write a JUnit test that uses Mockito, and run it in the same way as any other JUnit test.
- Start with the Mockito web siteand Mockito repository.
- 您不需要创建单独的测试项目,但如果您愿意,也可以创建。大多数人通过在同一个项目中创建一个单独的测试文件夹来做到这一点。无论您有一个单独的测试项目还是只有一个测试文件夹,您都应该在测试文件夹中复制您的包结构,以便每个测试类都在测试同一个包中的应用程序类。
- 是的。Mockito 旨在与 JUnit 或其他一些测试框架(例如 TestNG)一起使用。大多数人将它与 JUnit 一起使用。Mockito 网站上的大多数示例都使用 JUnit。所以,是的,您将需要 JUnit 库,或者您选择的任何其他测试框架的库。
- Mockito 测试是JUnit 测试(或 TestNG 测试,或其他)。因此,正如您所说,没有“更容易运行”。只需编写一个使用 Mockito 的 JUnit 测试,并以与任何其他 JUnit 测试相同的方式运行它。
- 从Mockito 网站和Mockito 存储库开始。