background preloader

Mockito

Facebook Twitter

Java - learning resources for mockito please. Adzic » 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). So let’s get right into it. 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")); }

Blog Archive » Mocking, Stubbing and Test Spying using the Mockito Framework and PowerMock. Today we’re going to take a look at the Mockito framework that not only does sound like my favourite summer cocktail but also offers nice testing, mocking/stubbing, test-spying features and mock injections. After that we’re going to take a look on how to mock static or final classes by extending Mockito’s capabilities with PowerMock. Prerequisites We don’t need much for the following samples .. Java of course, Maven dependency management and that’s all .. Setup Project We don’t have much to do here .. we need a mavenized project and one dependency .. We’re creating a new Maven project using the favourite IDE with Maven support or via command line using In the next step, we’re adding the dependency for mockito-core to our pom.xml that finally looks like this <?

Classes under Test Before we’re starting to write our tests we need some classes to be tested and mocked.. they’re all created in src/main/java in the package com.hascode.tutorial. UserBean UserRepository NotificationService AccountService. Mockito. 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);}

Mockito. Mockito Example (Java Mocking Framework) Posted by Brett Schuchert on 05/27/2009 Recently I was lamenting how much I liked Moq for C# and its API . I like that is uses lambdas and generics and I think both the interface and general approach are great. Someone I follow on twitter, @tieTYT , suggested that I should give Mockito a try. It is a mocking library for Java, written by Szczepan Faber and friends. Well I gave it a try and I found it to be an excellent Mocking framework similar in spirit to Moq and it worked well while not requiring lambdas. I was in a mood, so I went ahead and wrote a quick tutorial . Develop something using TDD . Using a mockist approach Use Inversion of Control Refactor code to use the GoF state pattern Refactor code to use the GoF Template Method Pattern Fix the code to avoid violation of the Liskov substitution principle (this is in there though not currently emphasized) Fix the code to avoid violation of the Dependency Inversion Principle .

Have fun and please comment. Mockito: Java Unit Testing with Mock Objects. One of the hallmarks of an experienced developer is an appreciation of the importance of unit and integration testing. Although integration testing is the ultimate way to verify application functionality, it does come with a lot of startup/execution/shutdown overhead. Sometimes it is easier to verify particular application components in isolation, without the actual application running. In this article, I show how you can still thoroughly test components that would usually require integration testing using regular, basic JUnit tests. Using code samples, I will walk you through writing unit tests for a JAX-RS REST Web service that fetches Person data from a database and returns it to the caller. I will demonstrate how to write a JUnit unit test for such service -- without having to actually deploy it into a running Web container. You will mock out the Hibernate DAO to simulate interaction with a database.

Unit Tests, Mock Objects and Mockito JAX-RS Service Code Sample Page 1 of 2.