easymock

Difference between 'same' and 'eq' in EasyMock

末鹿安然 提交于 2019-12-22 01:58:22
问题 Is there a significant(or even any) difference between 'same' and 'eq' in EasyMock? 回答1: same checks if both objects are actually the same instance (reference equality). eq calls equals and therefore checks if both have the same value (value equality). Keep in mind that the default equals implementation uses == internally, and therefore eq will do the same as same if you're using classes that do not have a proper equals override. But still, it's better to state your intent by using same for

Alternatives to @VisibleForTesting

蹲街弑〆低调 提交于 2019-12-20 09:49:26
问题 I understand that @VisibleForTesting is not desirable because it changes the interface of a class just for testing purposes. Ideally we should test the interface that we actually use. But what would be a good alternative? 回答1: You use @VisibleForTesting when, as you said, you want to test a part of code you're not exposing to the end user. If you want to test it then it most likely means it's complicated, or at least not trivial. Two solutions would be: Split the method where you're calling

JUnit testing for IO

依然范特西╮ 提交于 2019-12-20 05:47:05
问题 I am new here and new to junit testing. I have a class with two methods and I want to write unit tests for it. I am not sure how to start I read some basic tutorials but I am not able to start some how. Can anyone of you provide me some basic skeleton to start with. My class is public class CreateCSV { MyWriter csvOutput = null; public void createSortedSet ( final HashMap< String, Signal > map, final long totalSize, final long totalSizewithHeader, File file ) { ArrayList< Signal > messages =

Powermock - how to mock a specific method and leave the rest of the object as-is

一笑奈何 提交于 2019-12-20 05:18:06
问题 I have a Person class with get set for FirstName, LastName A TestClass to execute TestCase1 Can we just only mock a specific method (getLastName) and leave every thing else (other internal fields, functions ... as-is)? public class Person { private String firstName; private String lastName; public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String

Cobertura Showing proper coverage but In sonar many files showing 0% coverage

随声附和 提交于 2019-12-19 10:33:12
问题 I have write multiple JUnit test classes for my project.The code covergae is 80% when I see it in Eclipse using cobertura plugin.But when I try to see my code coverage in Sonar it show only 35%.The reason behind this is that multiple classes have 0% coverage and some classes shows coverage.What is the main reason I don't know.Is it problem of sonar or there is some problem im my code beacuse somewhere I am using PowerMockito somewhere EasyMock and somewhere Mockito. I am attaching the

Mocking EJB injection in tests

岁酱吖の 提交于 2019-12-19 08:12:55
问题 Whenever I want to test a class which uses resource injection I end up including a constructor that will only be used within the test: public class A { @EJB B b; // Used in tests to inject EJB mock protected A(B b) { this.b = b; } public A() {} // Method that I wish to test public void foo() { b.bar(); } } Is there another way of mocking resource injection or this is the correct pattern to follow? 回答1: you could use easy gloss to that effect, it mocks the EJBs injection system. another way is

EasyMock: Mocked object is calling actual method

泄露秘密 提交于 2019-12-19 05:52:31
问题 I've following code snippet in my unit test, ClassToBeMocked mock = createMock(ClassToBeMocked.class); //I've statically imported EasyMock.* mock.callMethod(); //This is a void method expectLastCall(); replay(mock); But when I run the test, instead of seeting up the expectaion, callMethod() is actually called. Am I doing something wrong? I'm fairly new to EasyMock or any mocking framework and blocked because of this problem. Any help would be greatly appreciated. Thanks, AndyS 回答1: This will

Mock private static final variables in the testing class

我的未来我决定 提交于 2019-12-19 04:48:06
问题 I have a few private static final fields in the class I want to test. Like follows public class ClassToTest{ .... private static final Myclass myclass = MyClassFactory.getMyClass(type.firstType); .... } The type is a enum in the MyClassFactory. That factory do is it initialize object according to type passed and return. My question is does powermock support this and if so how to do this. 回答1: You can use reflection also if any mock library works for you. Field f = classToTest.getclass()

EasyMock void method

久未见 提交于 2019-12-18 18:49:44
问题 I'm trying to use EasyMock to mock out some database interface so I can test the business logic off a wrapping method. I've been going ok with methods that return by using the following in my setup of my test. DBMapper dbmapper = EasyMock.createMock(DBMapper.class); userService.setDBMapper(dbmapper); then within my actual test I run EasyMock.expect(dbmapper.getUser(userId1)).andReturn(mockUser1); EasyMock.replay(dbmapper); userService.getUser(userId1); This service then connects to the

Is it possible to create a mock object that implements multiple interfaces with EasyMock?

梦想的初衷 提交于 2019-12-18 12:44:54
问题 Is it possible to create a mock object that implements several interfaces with EasyMock? For example, interface Foo and interface Closeable ? In Rhino Mocks you can provide multiple interfaces when creating a mock object, but EasyMock's createMock() method only takes one type. Is it possbile to achieve this with EasyMock, without resorting to the fallback of creating a temporary interface that extends both Foo and Closeable , and then mocking that? 回答1: EasyMock doesn't support this so you're