jmock

How to mock JdbcTemplate.queryForObject() method

坚强是说给别人听的谎言 提交于 2019-12-21 17:36:47
问题 My method looks like this: public class Decompile extends JdbcDaoSupport public void getRunner(){ String val = this.getJdbcTemplate().queryForObject(sql,String.class, new Object[]{1001}); } } Please suggest how I would mock this. 回答1: an EasyMock-3.0 example String sql = "select * from t1"; Object[] params = new Object[] { 1001 }; JdbcTemplate t = EasyMock.createMock(JdbcTemplate.class); EasyMock.expect( t.queryForObject(sql, String.class, params)).andReturn("res"); EasyMock.replay(t); 回答2:

Does mockito have an equivalent idiom to jMock's States?

心已入冬 提交于 2019-12-18 19:33:23
问题 The book Growing Object Oriented Software gives several examples in jMock where the state is made explicit without exposing it through an API. I really like this idea. Is there a way to do this in Mockito? Here's one example from the book public class SniperLauncherTest { private final States auctionState = context.states("auction state") .startsAs("not joined"); @Test public void addsNewSniperToCollectorAndThenJoinsAuction() { final String itemId = "item 123"; context.checking(new

What is the difference between mocks and stubs ( JMock)

人走茶凉 提交于 2019-12-13 01:17:56
问题 What is the difference between mocks and stubs in jMock? I can create both with jMock? how i can create stubs with it and what the situation is most appropriate for this, I believe that using stubs is when I need to prepare some state for test. Thanks 回答1: Wikipedia has an article regarding Mock objects, but the terminology is not explained as good as could be. We used to make this distinction (which may be subject to discussion, of course): Mocks and stubs both simulate an object which is

JMock- java.lang.NoSuchMethodError: org.hamcrest.Matcher.describeMismatch()

眉间皱痕 提交于 2019-12-12 13:05:23
问题 I understand that the solution is to somehow make sure that Junit is loaded after hamcrest. I have an intellij project, in which I setup an external library, which contains both JUnit and JMock and hamcrest. How can I make sure that this error does not show up 回答1: You should make sure the compatibility of libraries(jars). If a class inside jar uses some method from class which is in another jar, and this used method is newly added and you are using old jar then definitely you will get java

Can you mock out method calls in the class you're testing?

倾然丶 夕夏残阳落幕 提交于 2019-12-12 12:03:51
问题 I'm trying to write JUnit tests for my code but with in some of my methods other methods are called. Is it possible to mock these calls out? E.g. s3FileWrite(File file, Status status) { S3 s3 = new S3(file.getName, s3Service) String key = s3.getKey(); String bucket = s3.getBucket(); File tmp = new File("tmp/" + s3.getName()); writeFile(key, bucket, tmp, status); //local method call I want to mock out }//awsFileWrite The writeFile method is what I want to mock out and it's part of the class I

How to get started with testing(jMock)

喜你入骨 提交于 2019-12-12 07:57:55
问题 I'm trying to learn how to write tests. I'm also learning Java, I was told I should learn/use/practice jMock, I've found some articles online that help to certain extend like : http://www.theserverside.com/news/1365050/Using-JMock-in-Test-Driven-Development http://jeantessier.com/SoftwareEngineering/Mocking.html#jMock And most articles I found was about test driven development, write tests first then write code to make the test pass. I'm not looking for that at the moment, I'm trying to write

jMock - allowing() a call multiple times with different results

时光怂恿深爱的人放手 提交于 2019-12-12 04:56:09
问题 I want to call allowing() several times and provide different results. But I'm finding that the first allowing() specification absorbs all the calls and I can't change the return value. @Test public void example() { timeNow(100); // do something timeNow(105); // do something else } private void timeNow(final long timeNow) { context.checking(new Expectations() {{ allowing(clock).timeNow(); will(returnValue(timeNow)); }}); } If I change allowing(clock) to oneOf(clock) it works fine. But ideally

Mockito equivalent to this Hamcrest “samePropertyValuesAs”/jMock “with” idiom?

こ雲淡風輕ζ 提交于 2019-12-11 05:48:41
问题 Hamcrest/jMock code looks like this: @Test public void setsSniperValuesInColumns() { context.checking(new Expectations() {{ one(listener).tableChanged(with(aRowChangedEvent())); }}); model.sniperStatusChanged(new SniperState("item id", 555, 666), MainWindow.STATUS_BIDDING); ... } private Matcher<TableModelEvent> aRowChangedEvent() { return samePropertyValuesAs(new TableModelEvent(model, 0)); } NB this is taken from "Growing Object-Oriented Software Guided by Tests" (p. 157). The authors of

JMock matchers with setAttribute(String, Object)

折月煮酒 提交于 2019-12-11 05:14:09
问题 I am facing issue while expecting a setAttribute call from a mock. MyClass { public final void setAttribute(String name, Object value) { // Do Something } myClass.setAttribute("Key", "Value"); While invoking the setAttribute operation, String is passed as a value. I have a mock of the above class by the name mockMyClass and in my Expectations block I have the below code. oneOf(mockMyClass).setAttribute(with(equal("Key")), with(equal("Value"))); I have also tried using any, just to see if the

Mocking spring controller validator

帅比萌擦擦* 提交于 2019-12-11 03:52:23
问题 I want to unit test this Spring controller method: @Autowired private MyValidator validator; public String register( HttpServletRequest request, ModelMap model, Principal principal, @PathVariable Plain plain, RedirectAttributes ratts, @ModelAttribute @Valid PlainMoreObject pmo, BindingResult result) { validator.validate(pmo, result); I'm using JMock. How do I mock the validator in order to test controller by calling controller.register(....) ? 回答1: There is a helper class in Spring called