easymock

easymock的使用

匿名 (未验证) 提交于 2019-12-02 21:53:52
进入easymock官网界面: https://www.easy-mock.com/ 如果没有注册过的小伙伴,点击右上角的登陆,不用注册,登陆即可注册 登陆成功后,点击右下角的加号,添加项目 根据提示,创建项目 进入demo项目,然后点击创建接口 写入伪json数据,填写url地址,并创建 回到项目中,点击预览 复制上述地址 使用postman来call这个url,成功返回伪json数据 文章来源: easymock的使用

Alternatives to @VisibleForTesting

∥☆過路亽.° 提交于 2019-12-02 20:28:54
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? 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 this into several methods so you feel more comfortable about not having one big method doing a bunch of stuff

How can I mock a method in easymock that shall return one of its parameters?

徘徊边缘 提交于 2019-12-02 20:08:12
public Object doSomething(Object o); which I want to mock. It should just return its parameter. I tried: Capture<Object> copyCaptcher = new Capture<Object>(); expect(mock.doSomething(capture(copyCaptcher))) .andReturn(copyCatcher.getValue()); but without success, I get just an AssertionError as java.lang.AssertionError: Nothing captured yet . Any ideas? rems I was looking for the same behavior, and finally wrote the following : import org.easymock.EasyMock; import org.easymock.IAnswer; /** * Enable a Captured argument to be answered to an Expectation. * For example, the Factory interface

EasyMock and testing Protected Methods

懵懂的女人 提交于 2019-12-02 10:27:15
问题 Trying to use EasyMock to test if a protected method gets called, not sure if this is the best way to do it ... but given the below, how can I tell that didIgetCalled() actually was called when callMe() was called? public Class testMe(){ public int callMe(){ if(true){ didIgetCalled(); } return 1; } protected int didIgetCalled(){ return 2; } } 回答1: This is a way you can test the method without using EasyMock, however my recommendation is that: If it's not public, don't write a test for it

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

≯℡__Kan透↙ 提交于 2019-12-02 06:22:16
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 lastName) { this.lastName = lastName; } } import static org.powermock.api.easymock.PowerMock.*; import

JUnit testing for IO

半腔热情 提交于 2019-12-02 06:20:22
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 = new ArrayList< Signal >(); try { messages.addAll( map.values() ); map.clear(); for ( Signal signal :

EasyMock and testing Protected Methods

怎甘沉沦 提交于 2019-12-02 04:26:38
Trying to use EasyMock to test if a protected method gets called, not sure if this is the best way to do it ... but given the below, how can I tell that didIgetCalled() actually was called when callMe() was called? public Class testMe(){ public int callMe(){ if(true){ didIgetCalled(); } return 1; } protected int didIgetCalled(){ return 2; } } This is a way you can test the method without using EasyMock, however my recommendation is that: If it's not public, don't write a test for it Method method = testMe.class.getDeclaredMethod("didIgetCalled", new Class[]{}); method.setAccessible(true);

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

旧巷老猫 提交于 2019-12-01 11:13:45
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 snapshots of both the coverage one shown by cobertura and one shown by Sonar. Kindly help me. Thanks There is

EasyMock matcher for class data type

我与影子孤独终老i 提交于 2019-12-01 09:35:19
I am having nightmares with the syntax for this and easymock: public void foo(Class<?> clazz); EasyMock.expects(object.foo(EasyMock.isA(???))); What should I be putting if my argument is String.class? I initially thought: EasyMock.isA(((Class<?>)(String.class)).getClass()) Yet when I make the call foo(String.class) I get: java.lang.IllegalStateException: missing behavior definition for the preceding method call: You're attempting to verify a generic type that will be erased at runtime anyway. Use a capture object instead: Capture<Class<?>> classCapture = new Capture<Class<?>>(); EasyMock

Java generics and overloading with Groovy

好久不见. 提交于 2019-12-01 07:34:57
I write my unit tests for my Java application with Groovy, JUnit and EasyMock. In EasyMock there are several overloaded methods capture() which have been deprecated with the note "Because of harder erasure enforcement, doesn't compile in Java 7". The methods take as parameter an object of type Capture<T> . There exist, among others, the following methods: static boolean capture(Capture<Boolean> captured) static boolean capture(Capture<Integer> captured) ... static <T> T capture(Capture<T> captured) This is not allowed any more in Java but if you invoke that code directly from Java the right