BAEL-373: Improve/Upgrade existing Mockito articles to Mockito 2 (#2311)

* BAEL-373: Improve/Upgrade existing Mockito articles to Mockito 2

* Fix formatting issue with LoginControllerIntegrationTest
This commit is contained in:
Jose Carvajal 2017-07-30 00:29:25 +02:00 committed by KevinGilmore
parent 9f45d709db
commit f445a4d11b
10 changed files with 57 additions and 32 deletions

View File

@ -382,7 +382,7 @@
</profile>
</profiles>
<properties>
<properties>
<!-- marshalling -->
<jackson.version>2.8.5</jackson.version>
@ -408,7 +408,7 @@
<!-- testing -->
<org.hamcrest.version>1.3</org.hamcrest.version>
<junit.version>4.12</junit.version>
<mockito.version>1.10.19</mockito.version>
<mockito.version>2.8.9</mockito.version>
<assertj.version>3.6.1</assertj.version>
<avaitility.version>1.7.0</avaitility.version>

View File

@ -63,6 +63,6 @@ public class MappedByteBufferUnitTest {
private Path getFileURIFromResources(String fileName) throws Exception {
ClassLoader classLoader = getClass().getClassLoader();
return Paths.get(classLoader.getResource(fileName).getPath());
return Paths.get(classLoader.getResource(fileName).toURI());
}
}

View File

@ -179,7 +179,7 @@ public class JavaMoneyUnitTest {
MonetaryAmountFormat customFormat = MonetaryFormats.getAmountFormat(AmountFormatQueryBuilder
.of(Locale.US)
.set(CurrencyStyle.NAME)
.set("pattern", "00000.00 ¤")
.set("pattern", "00000.00 ¤")
.build());
String customFormatted = customFormat.format(oneDollar);

View File

@ -11,6 +11,7 @@ import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.Locale;
import java.util.Scanner;
import org.junit.Test;
@ -105,6 +106,7 @@ public class JavaScannerUnitTest {
public void whenScanString_thenCorrect() throws IOException {
final String input = "Hello 1 F 3.5";
final Scanner scanner = new Scanner(input);
scanner.useLocale(Locale.US);
assertEquals("Hello", scanner.next());
assertEquals(1, scanner.nextInt());

View File

@ -41,7 +41,7 @@
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<artifactId>powermock-api-mockito2</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
@ -65,7 +65,7 @@
<commons-lang3.version>3.5</commons-lang3.version>
<!-- testing -->
<powermock.version>1.6.6</powermock.version>
<powermock.version>1.7.0</powermock.version>
</properties>

View File

@ -53,6 +53,6 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- testing -->
<mockito.version>2.7.5</mockito.version>
<mockito.version>2.8.9</mockito.version>
</properties>
</project>

View File

@ -13,7 +13,7 @@
<name>mock-comparisons</name>
<properties>
<mockito.version>1.10.19</mockito.version>
<mockito.version>2.8.9</mockito.version>
<easymock.version>3.4</easymock.version>
<jmockit.version>1.29</jmockit.version>

View File

@ -7,7 +7,12 @@ import org.baeldung.mocks.testCase.UserForm;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.*;
import org.mockito.ArgumentMatcher;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;
public class LoginControllerIntegrationTest {
@ -41,50 +46,63 @@ public class LoginControllerIntegrationTest {
public void assertTwoMethodsHaveBeenCalled() {
UserForm userForm = new UserForm();
userForm.username = "foo";
Mockito.when(loginService.login(userForm)).thenReturn(true);
Mockito.when(loginService.login(userForm))
.thenReturn(true);
String login = loginController.login(userForm);
Assert.assertEquals("OK", login);
Mockito.verify(loginService).login(userForm);
Mockito.verify(loginService).setCurrentUser("foo");
Mockito.verify(loginService)
.login(userForm);
Mockito.verify(loginService)
.setCurrentUser("foo");
}
@Test
public void assertOnlyOneMethodHasBeenCalled() {
UserForm userForm = new UserForm();
userForm.username = "foo";
Mockito.when(loginService.login(userForm)).thenReturn(false);
Mockito.when(loginService.login(userForm))
.thenReturn(false);
String login = loginController.login(userForm);
Assert.assertEquals("KO", login);
Mockito.verify(loginService).login(userForm);
Mockito.verify(loginService)
.login(userForm);
Mockito.verifyNoMoreInteractions(loginService);
}
@Test
public void mockExceptionThrowing() {
UserForm userForm = new UserForm();
Mockito.when(loginService.login(userForm)).thenThrow(IllegalArgumentException.class);
Mockito.when(loginService.login(userForm))
.thenThrow(IllegalArgumentException.class);
String login = loginController.login(userForm);
Assert.assertEquals("ERROR", login);
Mockito.verify(loginService).login(userForm);
Mockito.verify(loginService)
.login(userForm);
Mockito.verifyZeroInteractions(loginService);
}
@Test
public void mockAnObjectToPassAround() {
UserForm userForm = Mockito.when(Mockito.mock(UserForm.class).getUsername()).thenReturn("foo").getMock();
Mockito.when(loginService.login(userForm)).thenReturn(true);
UserForm userForm = Mockito.when(Mockito.mock(UserForm.class)
.getUsername())
.thenReturn("foo")
.getMock();
Mockito.when(loginService.login(userForm))
.thenReturn(true);
String login = loginController.login(userForm);
Assert.assertEquals("OK", login);
Mockito.verify(loginService).login(userForm);
Mockito.verify(loginService).setCurrentUser("foo");
Mockito.verify(loginService)
.login(userForm);
Mockito.verify(loginService)
.setCurrentUser("foo");
}
@Test
@ -92,19 +110,22 @@ public class LoginControllerIntegrationTest {
UserForm userForm = new UserForm();
userForm.username = "foo";
// default matcher
Mockito.when(loginService.login(Mockito.any(UserForm.class))).thenReturn(true);
Mockito.when(loginService.login(Mockito.any(UserForm.class)))
.thenReturn(true);
String login = loginController.login(userForm);
Assert.assertEquals("OK", login);
Mockito.verify(loginService).login(userForm);
Mockito.verify(loginService)
.login(userForm);
// complex matcher
Mockito.verify(loginService).setCurrentUser(Mockito.argThat(new ArgumentMatcher<String>() {
@Override
public boolean matches(Object argument) {
return argument instanceof String && ((String) argument).startsWith("foo");
}
}));
Mockito.verify(loginService)
.setCurrentUser(Mockito.argThat(new ArgumentMatcher<String>() {
@Override
public boolean matches(String argument) {
return argument.startsWith("foo");
}
}));
}
@Test
@ -114,12 +135,14 @@ public class LoginControllerIntegrationTest {
UserForm userForm = new UserForm();
userForm.username = "foo";
// let service's login use implementation so let's mock DAO call
Mockito.when(loginDao.login(userForm)).thenReturn(1);
Mockito.when(loginDao.login(userForm))
.thenReturn(1);
String login = loginController.login(userForm);
Assert.assertEquals("OK", login);
// verify mocked call
Mockito.verify(spiedLoginService).setCurrentUser("foo");
Mockito.verify(spiedLoginService)
.setCurrentUser("foo");
}
}

View File

@ -18,7 +18,7 @@
<!-- <gib.enabled>false</gib.enabled>-->
<junit.version>4.12</junit.version>
<org.hamcrest.version>1.3</org.hamcrest.version>
<mockito.version>1.10.19</mockito.version>
<mockito.version>2.8.9</mockito.version>
<!-- logging -->
<org.slf4j.version>1.7.21</org.slf4j.version>
<logback.version>1.1.7</logback.version>

View File

@ -164,7 +164,7 @@
<commons-io.version>2.5</commons-io.version>
<!-- testing -->
<mockito.version>2.2.26</mockito.version>
<mockito.version>2.8.9</mockito.version>
<httpcore.version>4.4.1</httpcore.version>
<httpclient.version>4.5</httpclient.version>