junit

EasyMock

浪尽此生 提交于 2019-12-30 05:13:02
  JUnit是Java开发中常用的单元测试工具,对方法的测试很合适,但是一些情况下,JUnit就不是很适用了:    对象结构复杂、难以构造,对象的某些行为很难触发 。这时可以使用Mock来创建对象进行测试,同时还可以Mock一个未实现的接口来进行测试,简单的看就是模拟。   EasyMock是针对Java的Mock工具,通过 EasyMock,我们可以为指定的接口动态的创建 Mock 对象:    使用 EasyMock 生成 Mock 对象 设定 Mock 对象的预期行为和输出 将 Mock 对象切换到 Replay 状态 调用 Mock 对象方法进行单元测试 对 Mock 对象的行为进行验证   下载所需的jar包:easymock-3.2.jar,junit-4.4.jar,objenesis-1.1.jar。      EasyMock-3.1 之前默认只支持为接口生成 Mock 对象,如果需要为类生成 Mock 对象,需要扩展包实现此功能。EasyMock-3.1之后的可为类Mock对象,但是需要objenesis的jar包。   final,private和静态方法,easymock也无法mock其行为,这时可以考虑PowerMock。   假如有一个IStudent接口类和StudentApplication类

Run all tests in a source tree, not a package

半腔热情 提交于 2019-12-30 05:01:13
问题 My unit tests are in a separate directory tree from my integration tests, but with the same package structure. My integration tests need external resources (e.g. a server) to be available, but my unit tests are properly independent of each other and the environment. In IntelliJ-IDEA (v7) I have defined a JUnit Run/Debug Configuration to run all the tests in the top-level package, and this of course picks up my integration tests which fail. I want to define a run-junit configuration that runs

Force run of @Ignore'd JUnit test using Maven command line

余生长醉 提交于 2019-12-30 02:56:08
问题 I have a JUnit test with an @Ignore attribute, for example - public class HealthCareGov { @Test @Ignore public void performancetest() { // stuff } } In most IDEs (IntelliJ for example) I can forcefully run this test by selecting the method name even though there is an @Ignore attribute. I'm guessing Eclipse also allows this? However, when I try to do the same behavior in Maven by executing the following - mvn -Dtest=HealthCareGov#performancetest test It appears that Maven skips the test. This

@BeforeClass vs static{}

强颜欢笑 提交于 2019-12-30 01:54:04
问题 I am writing some test cases using JUnit. I need to initialize some static variables which will be used for all the test cases in that class. For this I can use either Static initializer block or Static method with @BeforeClass What are the advantages of using one over another? 回答1: There are very different semantics for @BeforeClass or a static initializer. A static initializer is invoked by the JVM and not by JUnit. If an exception is thrown within a static initializer, the test framework

How to use stubs in JUnit and Java?

℡╲_俬逩灬. 提交于 2019-12-30 01:12:09
问题 I have worked with JUnit and Mocks but I'm wondering, what are the differences between Mocks and Stubs in JUnit and how to use Stubs in JUnit, Java? And as Mocks that have EasyMock, Mockito and so on, what does Stubs uses in Java? Please give some example code for Stubs in Java. 回答1: To use stubs in junit you don't need any frameworks. If you want to stub some interface just implement it: interface Service { String doSomething(); } class ServiceStub implements Service { public String

Selenium cannot find SVG element in XPath

别说谁变了你拦得住时间么 提交于 2019-12-30 00:15:25
问题 I have the following HTML: <div id="imageholder> <svg> <g> <image href='blah.gif'> </g> </svg> </div> And I cannot seem to locate the svg with selenium IDE on firefox at all. I have tried: //svg //svg:svg //*[name()='svg'] //*[namespace-uri()='http://www.w3.org/2000/svg'] None of them can locate my svg element. Sometimes I get the error: error = TypeError: e.scrollIntoView is not a function I'm using this as a means to use the locator in JUnit 4 testing if that helps. 回答1: Try the following

How to unit test Retrofit api calls?

半腔热情 提交于 2019-12-29 11:32:08
问题 I am trying to integrate Unit test cases for every chunk of code possible. But I am facing issues while adding test cases for api calls that are made through retrofit. The JUnit compiler never executes the code in the CallBack functions . There is another option of making all the api calls Synchronous for testing purpose, but that's not possible for every case in my app. How can I sort this out? I have to add test cases in the api calls by any means. 回答1: I test my Retrofit callbacks using

实验2实验报告

老子叫甜甜 提交于 2019-12-29 10:58:30
我将使用一个例子来展示不同的测试用例:一个计算器。该示例计算器很简单,效率并不高,甚至还有一些错误;它仅仅操作整数,并且把结果存储在一个静态变量中。Substract方法并不返回一个有效的结果,而且也没有实现乘法运算,而且看上去在squareRoot方法中还存在一个错误:无限循环。这些错误将帮助说明使用JUnit 4进行测试的有效性。你可以打开和关闭这个计算器,而且你可以清除这些结果。下面是其实现代码: package calc; public class Calculator {   private static int result; // 存储结果的静态变量   public void add(int n) { result = result + n;   }   public void substract(int n) { result = result - 1; // 错误:应该是 "result = result - n"   }   public void multiply(int n) {} // 还没实现   public void divide(int n) { result = result / n;   }   public void square(int n) { result = n * n;   }   public void squareRoot

Junit: splitting integration test and Unit tests

孤人 提交于 2019-12-29 10:03:45
问题 I've inherited a load of Junit test, but these tests (apart from most not working) are a mixture of actual unit test and integration tests (requiring external systems, db etc). So I'm trying to think of a way to actually separate them out, so that I can run the unit test nice and quickly and the integration tests after that. The options are.. Split them into separate directories. Move to Junit4 (from v3) and annotate the classes to separate them. Use a file naming convention to tell what a

What is TestSuite?

拜拜、爱过 提交于 2019-12-29 08:47:25
问题 I am relatively new to Java and new to JUnit testing. It's absolutely clear to me what the Test class uis, but the TestSuite class confuses me. Can someone explain me what TestSuite is for? 回答1: Its a collection of tests. It allows you to run such a collection as a group. Example from the first link I found with google. import junit.framework.Test; import junit.framework.TestSuite; public class EcommerceTestSuite { public static Test suite() { TestSuite suite = new TestSuite(); // // The