A story about trying to get along with Mockito

A story about trying to get along with Mokit

This is the first post. Do you use "Mockito"?

I used Android Studio for business to create an application, and when I did a Unit Test, I had a fight with Mockito, so I will post it to make friends with Mockito and as my memorandum.

usage environment

About the part that quarreled with Mockito

The application I was creating was provided with an interface by aidl and was an application that binds to the service. Initially, I used Robolectric's ShadowApplication.setComponentNameAndServiceForBindService () to pass a Mocked IBinder, but when I retrieve the interface with ʻIBinder.Stub.asInterface ()`, it becomes a different object. I wasn't able to make it into a Mock. (Maybe my Mock was simply wrong.)

It seems that static methods can also be Mocked from Mockito 3.4.0, but I could not find any useful information on the Web, so I will show it below.

How to mock static methods

ʻHow to mock static methods such as IBinder.Stub.asInterface ()`. You can make it a Mock by the following usage.

final IMyService mockMyService = mock(IMyService.class); // ⭐︎1
try(final MockedStatic<IMyService.Stub> mockedStatic = mockStatic(IMyService.Stub.class)) {

// ↓ The return value of IMyService.Stub.asInterface () will be the mocked IMyService (⭐︎1). mockedStatic.when(() -> IMyService.Stub.asInterface(any())).thenReturn(mockMyService); // Please test with bindService () in Activity etc. below. final ActivityController controller = buildActivity(MainActivity.class); controller.create; ... }

By performing the above operation, you can make a static method into a Mock. In the above, it is described as try-with-resources, but if it is not described as try-with-resources, please perform close ().

final MockedStatic<IMyService.Stub> mockedStatic = mockStatic(IMyService.Stub.class);
...
mockedStatic.close();

By the way

You can also Mock a class that is new in a method with Mockito.

final MockedConstruction<SubClass> mockedConstruction = mockConstruction(SubClass.class);

// You can get the new class below // Since the acquired class is Mocked, you can change the return value etc. using when (). List constructed = mockedConstruction.constructed(); mockedConstruction.close();

Finally

Since this is my first post, I'm not sure if I can explain it well. I hope it helps someone even a little. The Mock conversion method introduced this time is only a part. Both MockedStatic andMockedConstruction have several types of constructors, so [Mockito's official documentation](https://javadoc.io/static/org.mockito/mockito-core/3.5.9/org/mockito/ Please refer to Mockito.html) and try various things with IDE such as Android Studio.

Recommended Posts

A story about trying to get along with Mockito
A story about trying to operate JAVA File
A story about trying hard to decompile JAR files
A story about reducing memory consumption to 1/100 with find_in_batches
How to get along with Rails
A story about PKIX path building failed when trying to deploy to tomcat with Jenkins
[PHP] A story about outputting PDF with TCPDF + FPDI
A story about developing ROS called rosjava with java
How to get started with creating a Rails app
I want to get along with Map [Java beginner]
A story stuck with NotSerializableException
A story addicted to toString () of Interface proxied with JdkDynamicAopProxy
A confused story about a ternary operator with multiple conditional expressions
A story about misunderstanding how to use java scanner (memo)
With podman in docker, everyone wants to get along and use docker on a shared computer
A story that I struggled to challenge a competition professional with Java
A funny story stuck in a mess when trying to import fx-clj
How to get started with slim
A story of frustration trying to create a penetration environment on Ubuntu 20.04
[Note] A story about changing Java build tools with VS Code
A story of connecting to a CentOS 8 server with an old Ansible
A story about hitting the League Of Legends API with JAVA
A story about having a hard time aligning a testing framework with Java 6
A story about making a calculator to calculate the shell mound rate
A story about using the CoreImage framework to erase stains with Swift and implement a blur erase function
Memo to get with Struts2 + Ajax
A story about changing jobs from a Christian minister (apprentice) to a web engineer
A note about trying Oracle 11g + Spring boot with Vagrant + Docker compose
Hanashi stumbled a little on path trying to study Java with VScode
A story about converting character codes from UTF-8 to Shift-jis in Ruby
A story about sending a pull request to MinGW to update the libgr version
Get a backup with mysqldump (Docker's MySQL)
A story addicted to JDBC Template placeholders
I tried to get started with WebAssembly
[Note] How to get started with Rspec
A little addictive story with def initialize
How to get the ID of a user authenticated with Firebase in Swift
A story about saving an image with carrierwave in a nested form using a form object.
SpringSecurity I was addicted to trying to log in with a hashed password (solved)
A story about creating a library that operates next-generation sequencer data with Ruby ruby-htslib
It's a pain to deal with old dates
How to mock each case with Mockito 1x
A note about adding Junit 4 to Android Studio
Let's get started with Java-Create a development environment ①
A story addicted to EntityNotFoundException of getOne of JpaRepository
A story about Java 11 support for Web services
[Rails] rails new to create a database with PostgreSQL
How to get a heapdump from a Docker container
A story that took time to establish a connection
Convert a string to a character-by-character array with swift
A story about a very useful Ruby Struct class
A story about Apache Wicket and atomic design
A story about making a Builder that inherits the Builder
Transition to a view controller with Swift WebKit
How to get started with Eclipse Micro Profile
A story packed with Java's standard input Scanner
Try to get redmine API key with ruby
[Android] Two ways to get a Bluetooth Adapter
Rip a CD to MP3 with Ubuntu 18.04 LTS
Rails beginners tried to get started with RSpec
How to get resource files out with spring-boot