easymock

How should I use EasyMock's @Mock annotation (new in version 3.2)?

独自空忆成欢 提交于 2019-12-08 17:49:24
It looks like EasyMock version 3.2 now supports using annotations to setup mock objects. I am new to EasyMock (and Java in general) and am trying to understand how to use this. Do these annotations do something new or just provide an alternative way to do things? The documentation says: Since EasyMock 3.2, it is now possible to create mocks using annotations. This is a nice and shorter way to create your mocks and inject them to the tested class. Here is the example above, now using annotations: ... Then there is a listing that shows use of the @TestSubject and @Mock annotations, but I don't

Easymock isA vs anyObject

蹲街弑〆低调 提交于 2019-12-08 15:49:56
问题 What is the difference between EasyMock.isA(String.class) and EasyMock.anyObject(String.class) (Or any other class supplied) In what situations would would you use one over the other? 回答1: The difference is in checking Nulls. The isA returns false when null but anyObject return true for null also. import static org.easymock.EasyMock.*; import org.easymock.EasyMock; import org.testng.annotations.Test; public class Tests { private IInterface createMock(boolean useIsA) { IInterface testInstance

Is it possible to mock a single method in an already existing object?

感情迁移 提交于 2019-12-07 13:33:21
问题 For an integration test, I need to mock a specific method in a java service client without destroying the rest of the information in it. It has no self-constructor, so a solution like this is out of the question: private DBClient mockClient = new DBClient(alreadyExistingClient){ @Override void deleteItem(Item i){ //my stuff goes here } }; Is there a way to mock the deleteItem method such that the credentials, endpoints, etc... are preserved in an existing DBClient object? edit: mockito is not

How to unit test a DAO that is extending SqlMapClientDaoSupport

心不动则不痛 提交于 2019-12-07 05:39:38
问题 Spring DA helps in writing DAOs. When using iBATIS as the persistence framework, and extending SqlMapClientDaoSupport, a SqlMapClient mock should be set for the DAO, but I can't do it. SqlMapClientTemplate is not an interface and EasyMock cannot creates a mock for it. 回答1: DAO and unit tests do not get along well ! That does not make sense to mock anything in a component that does not hold any business logic and is focused on database access. You should try instead to write an integration

How do I mock a method inherited from an abstract class with EasyMock?

拟墨画扇 提交于 2019-12-07 04:46:41
问题 I'm struggling with EasyMock. I've written two small classes to illustrate my problem: public abstract class A { private AtomicReference<Integer> id = new AtomicReference<Integer>(null); public final int getId() { return id.get(); } public final boolean setId(int id) { return this.id.compareAndSet(null, id); } } public class B extends A { } Then I proceed to write a test method as follows: public class EasyMockTester extends EasyMockSupport { @Test public void test() { B b = EasyMock

Mocking a final method with PowerMock + EasyMock

回眸只為那壹抹淺笑 提交于 2019-12-07 02:50:22
问题 I'm trying to mock a call to the final method ResourceBundle.getString() . With PowerMock 1.4.12 and EasyMock 3.1, the call is not being mocked; instead, the "real" method is called. My test class: @RunWith(PowerMockRunner.class) @PrepareForTest(ResourceBundle.class) public class TestSuite { @Before public void setUp() throws Exception { ResourceBundle resourceBundleMock = PowerMock.createNiceMock(ResourceBundle.class); expect(resourceBundleMock.getString(BundleConstants.QUEUE)).andReturn(

How do I write a unit test to verify that a function sorts its result? [duplicate]

前提是你 提交于 2019-12-06 22:43:38
问题 This question already has answers here : How to test the ordering of elements in a Collection in JUnit test? (4 answers) Closed 2 years ago . I have a data source from which I can request a list of people that live in a (any) country, and a method which retrieves the people from that data source and sorts them by their name alphabetically. How should I write my unit test to make sure that the sorting part of my method works properly? This is what my SUT looks like: class PeopleStuff { public

EasyMock : java.lang.IllegalStateException: 1 matchers expected, 2 recorded

南笙酒味 提交于 2019-12-06 17:27:54
问题 I am having a problem with EasyMock 2.5.2 and JUnit 4.8.2 (running through Eclipse). I have read all the similar posts here but have not found an answer. I have a class containing two tests which test the same method. I am using matchers. Each test passes when run alone. The first test always passes - this is true if I switch the order of the tests in the file. Here is a simplified version of the test code: private Xthing mockXthing; private MainThing mainThing; @Before public void setUp() {

easymock的部署

社会主义新天地 提交于 2019-12-06 14:28:48
Easy-Mock主要是用来解决以下的问题: 1. 前后端分离的项目,后台接口还没有开发出来,前台开发好了暂时没数据调试; 2. 第三方接口;比如支付,模拟支付失败等等; 3. 前端测试保证每一种测试结果都测试出来。 一、EasyMock的部署 1. 首先,安装docker-compose 2. 安装后查看其版本,如下: 3. 查看桌面生成了一个easymock文件夹,查看其下的文件内容 4. 查看docker-compose.yml的内容,可以看到此eaymock的端口号:7300,如图: 5. 在easymock文件夹下,启动此服务: 6. 然后就可以再浏览器下浏览此网站了,注册后就可以登录了,如图: 7. easyMock停止服务,命令如下: 这样easymock的服务就搭建好了。后面就可以再其中添加项目了。 来源: https://www.cnblogs.com/irisWhq/p/11989936.html

PowerMock - Mocking static system class throws IllegalStateException

自作多情 提交于 2019-12-06 12:33:00
I have the following code public class A{ public void createFile() { File tempXmlFile = null; String extension = ".xml"; String name = "someName"; try { tempXmlFile = File.createTempFile(name, extension); if (tempXmlFile.exists()) { tempXmlFile.delete(); } } catch (IOException e) { System.out.println(e.getStackTrace()); } } } @RunWith(PowerMockRunner.class) @PrepareForTest(A.class) public class testA extends TestCase{ private A classUnderTest; @Override @Before public void setUp() { classUnderTest = PowerMock.createMock(A.class); //the class is more complex in my case and I have to mock it }