Mockito in six easy examples
Mockito is a fantastic mock library for Java. I’m fascinated by how easy it is to use, compared to other things out there both in the Java and .NET world. Here is everything you need to know to get started in six really easy examples. First of all, get mockito from Almost everything really interesting can be imported with the org.mockito.Mockito class (or a static import of its methods, which I’ll use in this post). To create a stub (or a mock), use mock(class). import static org.mockito.Mockito.*; import static org.junit.Assert.*; import java.util.Iterator; import org.junit.Test; .... This example creates a mock iterator and makes it return “Hello” the first time method next() is called. Stubs can also return different values depending on arguments passed into the method. @Test public void with_arguments(){ Comparable c=mock(Comparable.class); when(c.compareTo("Test")).thenReturn(1); assertEquals(1,c.compareTo("Test")); }
FindBugs™ - Find Bugs in Java Programs
schuchert - Mockito.LoginServiceExample
package com.om.example.loginservice; import org.junit.Test;import static org.mockito.Mockito.*; public class LoginServiceTest { @Test public void itShouldSetAccountToLoggedInWhenPasswordMatches() { IAccount account = mock(IAccount.class); when(account.passwordMatches(anyString())).thenReturn(true); IAccountRepository accountRepository = mock(IAccountRepository.class); when(accountRepository.find(anyString())).thenReturn(account); LoginService service = new LoginService(accountRepository); service.login("brett", "password"); verify(account, times(1)).setLoggedIn(true); }} Test Description Things Created for Compilation package com.om.example.loginservice; public interface IAccount { void setLoggedIn(boolean value); boolean passwordMatches(String candidate);}
JAutodoc - Eclipse Plugin
CDI et l’injection de mock pour les tests unitaires
L’injection de dépendances avec CDI me semble vraiment très intéressante en terme d’usabilité et de lisibilité du code. La seule chose qui me dérange un peu est le manque de facilité à tester un objet utilisant l’injection de dépendances. Ou, plutôt, comment utiliser un framework de mock et injecter un mock sur lequel j’ai la main en test. Je préviens d’avance. Voyons voir sur un exemple simple comment procéder pour arriver à utiliser un framework tel que Mockito. Tout d’abord, voilà mes classes et l’interface côté implémentation. L’interface représentant le service que je vais vouloir mocker par la suite. public interface MyService { String foo(); Son implémentation par défaut : public class DefaultMyService implements MyService { public String foo() { return "DefaultMyService.foo()" ; La classe dans laquelle le service est injecté par constructeur : public class MyApplication { private MyService myService; @Inject public MyApplication(MyService myService) { this .myService = myService; app.run();
ModelGoon UML4Java
fest - Fixtures for Easy Software Testing
FEST is a collection of libraries, released under the Apache 2.0 license, whose mission is to simplify software testing. It is composed of various modules, which can be used with TestNG or JUnit. Our users include: Google, Square, Eclipse Foundation, Oracle, IBM, Guidewire, and many more! For more details, like user testimonials and news, please visit the project's home page. GUI Functional Swing Testing This module provides a simple and intuitive API for functional testing of Swing user interfaces, resulting in tests that are compact, easy to write, and read like a specification. FEST makes troubleshooting failures a lot easier. The following example simulates a user logging-in into a Swing application. dialog.comboBox("domain").select("Users");dialog.textBox("username").enterText("alex.ruiz");dialog.button("ok").click();dialog.optionPane().requireErrorMessage() .requireMessage("Please enter your .*"); // regular expression matching Fluent Assertions Here are some examples: Fluent Reflection
Test Driven - Code Like This
Test-Driven by Alex Chaffee alex @ stinky.com Intended Audience Developers or QA EngineersFamiliar with Unit Testing (optional)Want more detail on Automated Testing in generalWant to know the case for Test-Driven DevelopmentWant to know style tips and gotchas for Testing and TDD Red, Green, Refactor First, write a test and watch it fail Make sure it's failing for the right reason! Make it green, then make it clean Addicted to green You get a little rush every time a new test passesSteady, incremental feeling of progressDon't write code all day without knowing if it works Three As Arrange (set up preconditions)Act (call production code)Assert (check the results) Assert The heart of a unit testAn assertion is a declaration of truthFailed assertion -> incorrect behavior"assert your postconditions"Example: Set set = new MySet(); set.add("ice cream"); assertTrue(set.contains("ice cream")); In RSpec, "assert" is called "should" One Step At A Time The Null Test Test List Fake it till you make it Simple Rule
For unit testing, I find this far easier to work with than jmock.org or www.easymock.org. One thing it cannot do is mock static methods. I of the other 2 can do that by superunknown Apr 10