How to mock each case with Mockito 1x

Overview

When using Mockito, I was always googled how to mock in this case, but I decided to summarize it because it is inefficient. I really want to use JMockit, but black magic is prohibited, so I use Mockito.

Premise

We haven't been able to move to the 2nd system yet, so Mockito1x is a prerequisite. Mockito 1.10.19 JUnit 4.11 Java 1.8.0_151

Case

One thing to keep in mind with Mockito is that the mocking method differs depending on whether the return value of the method is void or not. Based on this, we have classified it into the following six categories.

No Return type of mock target method Mock range Mock operation
1 Other than void The entire Returns the specified return value
2 Other than void The entire Throw an exception
3 void The entire Throw an exception
4 Other than void Only some methods Returns the specified return value
5 Other than void Only some methods Throw an exception
6 void Only some methods Throw an exception

By the way, the case where private method, static method and dependent object are created by new is within the scope of PowerMock, so I will write another article. → I wrote it. How to mock each case with PowerMock + Mockito1x https://qiita.com/taka_22/items/27ea1fbf9c305ba83dcc

How to write a test

No.1 /**

No.2 /**

No.3 The point is that doThrow comes in front.

/**

No.4 If you want to mock only part of it, use spy instead of mock. It is used when you want to change the behavior of a method that is not a test target and is called from a test target method.

/**

// I added "bbb" to the real object spiedList.add("bbb"); // Spy now get (0) returns "aaa" when(spiedList.get(0)).thenReturn("aaa");

// The result is "aaa" assertThat((String)spiedList.get(0), is("aaa")); // Other methods return real results assertThat(spiedList.size(), is(1)); }

No.5 /**

// I added "bbb" to the real object spiedList.add("bbb"); // Spy now throws get (0) a RuntimeException when(spiedList.get(0)).thenThrow(new RuntimeException("aaa"));

	try {
		spiedList.get(0);
		fail();
	} catch(Exception e) {
		assertThat(e.getMessage(), is("aaa"));
	}

// Other methods return real results assertThat(spiedList.size(), is(1)); }

No.6 /**

// Add "bbb" to real objects spiedList.add("bbb"); // Throw a RuntimeException when clear is called doThrow(new RuntimeException("aaa")).when(spiedList).clear();

	try {
		spiedList.clear();
		fail();
	} catch(RuntimeException e) {
		assertThat(e.getMessage(), is("aaa"));
	}

// Other methods return real results assertThat(spiedList.size(), is(1)); assertThat((String)spiedList.get(0), is("bbb")); }

reference

Mockito http://site.mockito.org/

Project confirmed to work

https://github.com/taka2/mockito-sample

How to mock each case with PowerMock + Mockito1x

https://qiita.com/taka_22/items/27ea1fbf9c305ba83dcc

Recommended Posts

How to mock each case with Mockito 1x
How to mock each case with PowerMock + Mockito1x
How to write Mockito
How to number (number) with html.erb
How to update with activerecord-import
How to get started with Gatsby (TypeScript) x Netlify x Docker
How to scroll horizontally with ScrollView
How to get started with slim
How to enclose any character with "~"
How to use mssql-tools with alpine
How to get along with Rails
How to start Camunda with Docker
How to realize huge file upload with TERASOLUNA 5.x (= Spring MVC)
How to crop an image with libGDX
How to adjustTextPosition with iOS Keyboard Extension
How to share files with Docker Toolbox
How to compile Java with VsCode & Ant
[Java] How to compare with equals method
[Android] How to deal with dark themes
How to use BootStrap with Play Framework
[Rails] How to use rails console with docker
How to switch thumbnail images with JavaScript
[Note] How to get started with Rspec
How to do API-based control with cancancan
How to achieve file download with Feign
How to build a Ruby on Rails development environment with Docker (Rails 6.x)
How to update related models with accepts_nested_attributes_for
How to set JAVA_HOME with Maven appassembler-maven-plugin
How to implement TextInputLayout with validation function
[Rails 5.x] How to introduce free fonts
How to handle sign-in errors with devise
How to delete data with foreign key
How to test private scope with JUnit
How to monitor nginx with docker-compose with datadog
How to build a Ruby on Rails development environment with Docker (Rails 5.x)
How to deal with Precompiling assets failed.
How to achieve file upload with Feign
How to run Blazor (C #) with Docker
How to build Rails 6 environment with Docker
How to download Oracle JDK 8 rpm with curl
[Java] How to test for null with JUnit
How to execute and mock methods using JUnit
How to use MyBatis2 (iBatis) with Spring Boot 1.4 (Spring 4)
How to save to multiple tables with one input
How to test interrupts during Thread.sleep with JUnit
How to use built-in h2db with spring boot
How to search multiple columns with gem ransack
How to use Java framework with AWS Lambda! ??
[Swift] How to link the app with Firebase
How to create multiple pull-down menus with ActiveHash
Notes on how to use each JUnit Rule
How to use Java API with lambda expression
How to get started with Eclipse Micro Profile
How to give your image to someone with docker
How to insert all at once with MyBatis
How to monitor SPA site transitions with WKWebView
How to write test code with Basic authentication
[Rails] How to easily implement numbers with pull-down
How to build API with GraphQL and Rails
How to use nfs protocol version 2 with ubuntu 18.04
How to use docker compose with NVIDIA Jetson