Matchers und Eclipse-Shortcuts

OliverE
Mausschubser
Mausschubser
Beiträge: 45
Registriert: 21. Okt 2010 16:48

Matchers und Eclipse-Shortcuts

Beitrag von OliverE »

Hallo!

In der letzten Übung hat Sven in seinen Code-Retreat-Code Hamcrest.Matcher verwendet.
In Mockito gibt es auch Matcher-Klassen. Die API sagt, dass sie "flexible verification or stubbing" erlauben...aha!
Also, was tun Matcher-Klassen jetzt i.A. so und wozu braucht man sie?

Außerdem hat Sven eine Ecipse Funktionalität genutzt, bei der beim Schreiben der Variable (in einer methode glaub ich)
automatisch eine klassenvariable angelegt wurde, wie geht das bitte?

Gruß

Oliver

svenamann
BASIC-Programmierer
BASIC-Programmierer
Beiträge: 145
Registriert: 6. Mai 2013 18:04

Re: Matchers und Eclipse-Shortcuts

Beitrag von svenamann »

Hi Oliver,

I'll switch to English so everybody can understand.

First: Matchers

There are different kinds of matchers out there. There intent is similar, but they are completely unrelated from am OO viewpoint, i.e., you cannot use one for the other.

One kind is the Hamcrest matchers [1]. They are intended to provide a flexible, humand-readable DSL for writing assertions in unit tests. Part of the hamcrest library comes with JUnit, but there's more than just that. These matchers essentially define conditions on objects, e.g., assertThat(myList, contains(anElement)) or assertThat(myNumber, is(greaterOrEqual(42)).

Then there are the Mockito matchers. They are intended to define conditions on call arguments passed to methods-stubs. These matchers of course come with Mockito itself. You can define such conditions for both mocking and verifying, e.g., when(comparatorMock.areEqual(any(FirstArgumentType.class), eq(anInstance)).thenReturn(true) will mock the areEqual method to return true if it is called with any instance of FirstArgumentType as the first and anInstance as the second argument. And verify(comparatorMock).areEqual(any(FirstArgumentType.class), eq(anInstance)) will verify that such a call actually occurred. Again, there is far more to it [2].

[1] http://code.google.com/p/hamcrest/wiki/Tutorial
[2] http://code.google.com/p/mockito/

Second: Eclipse Magic

All of the Eclipse-Functionality is available though the menus; much is available though shortcuts. If I discover that I tend to use something quite often, I look up the respective shortcut and try to remember it. Unfortunately, it is not possible to use Eclipse completely without a mouse, but that's a story for another day ;)

In general there are a view important(TM) shortcuts in the Java-Editor:
  • Quick-Fix (Cmd-1 or Ctrl-1): Opens a list of "fixes" for the code at the carret position, e.g., make a variable a field or add an import or stuff like that.
  • Rename (Cmd-Alt-R or Ctrl-Alt-R): Renames the element (like method, variable, or class) at the carret position. This is the probably most common refactoring...
  • Refactor (Cmd-Alt-T or Ctrl-Alt-T): Opens a list of possible refactorings
  • Extract local variable (Cmd-Alt-L or Ctrl-Alt-L): Makes a selected expression a local variable
There are many more helpful things. You can have a look at the shortcut list available though Cmd-Shift-L or Ctrl-Shift-L. Another good practice I took from a Code-Retreat was to try coding without using the mouse. That makes you learn your shortcuts ;)

General: Know your tools!

It is always a good idea to play around with some tools before you have to use them in productive mode. If you have questions related to any such tools, just tell me. Given I know the tool, we can make small tool demos in the exercise.

Hope this book I just wrote helps somewhat ^^
Best,
Sven

mister_tt
Kernelcompilierer
Kernelcompilierer
Beiträge: 502
Registriert: 29. Sep 2008 15:54

Re: Matchers und Eclipse-Shortcuts

Beitrag von mister_tt »

Hey guys,

regarding keyboard shortcuts the following plugin could be quite helpful: http://marketplace.eclipse.org/content/mousefeed It shows a popup with the respective shortcut every time one uses a function with the mouse.

Cheers
Simon

OliverE
Mausschubser
Mausschubser
Beiträge: 45
Registriert: 21. Okt 2010 16:48

Re: Matchers und Eclipse-Shortcuts

Beitrag von OliverE »

Hello Sven,

i want to thank you for writing such a long and comprehensive reply. It helped a lot.

Also Thanks to simon, i´ll definitely try out this one.

Cheers
Oliver

Antworten

Zurück zu „Archiv“