JR Utily

* upgrade of pom (spring bom + mockito 2.2)

* example of unit test using the junit5 way
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
18 18
19 <properties> 19 <properties>
20 <!-- dependencies version --> 20 <!-- dependencies version -->
21 - <spring.platform-bom.version>2.0.7.RELEASE</spring.platform-bom.version> 21 + <spring.platform-bom.version>Athens-SR1</spring.platform-bom.version>
22 <omnifaces.version>2.5.1</omnifaces.version> 22 <omnifaces.version>2.5.1</omnifaces.version>
23 <primefaces.version>6.0</primefaces.version> 23 <primefaces.version>6.0</primefaces.version>
24 <myfaces.version>2.2.10</myfaces.version> 24 <myfaces.version>2.2.10</myfaces.version>
...@@ -30,6 +30,7 @@ ...@@ -30,6 +30,7 @@
30 <hibernate-jpa-2.1-api.version>1.0.0.Final</hibernate-jpa-2.1-api.version> 30 <hibernate-jpa-2.1-api.version>1.0.0.Final</hibernate-jpa-2.1-api.version>
31 <junit.platform.version>1.0.0-M2</junit.platform.version> 31 <junit.platform.version>1.0.0-M2</junit.platform.version>
32 <junit.jupiter.version>5.0.0-M2</junit.jupiter.version> 32 <junit.jupiter.version>5.0.0-M2</junit.jupiter.version>
33 + <mockito-core.version>2.2.16</mockito-core.version>
33 34
34 <!-- paths --> 35 <!-- paths -->
35 <custom.web.dir>src/main/java/org/legrog/web</custom.web.dir> 36 <custom.web.dir>src/main/java/org/legrog/web</custom.web.dir>
...@@ -41,6 +42,7 @@ ...@@ -41,6 +42,7 @@
41 <maven.compiler.source>1.8</maven.compiler.source> 42 <maven.compiler.source>1.8</maven.compiler.source>
42 <maven.compiler.target>1.8</maven.compiler.target> 43 <maven.compiler.target>1.8</maven.compiler.target>
43 44
45 +
44 </properties> 46 </properties>
45 47
46 <profiles> 48 <profiles>
...@@ -289,6 +291,7 @@ ...@@ -289,6 +291,7 @@
289 <dependency> 291 <dependency>
290 <groupId>org.mockito</groupId> 292 <groupId>org.mockito</groupId>
291 <artifactId>mockito-core</artifactId> 293 <artifactId>mockito-core</artifactId>
294 + <version>${mockito-core.version}</version>
292 <scope>test</scope> 295 <scope>test</scope>
293 </dependency> 296 </dependency>
294 297
......
...@@ -17,52 +17,51 @@ import java.util.List; ...@@ -17,52 +17,51 @@ import java.util.List;
17 @Named 17 @Named
18 @SessionScoped 18 @SessionScoped
19 public class UpdateUserBean { 19 public class UpdateUserBean {
20 - @Inject 20 +
21 + Logger logger = LoggerFactory.getLogger(getClass());
22 +
21 private UserService userService; 23 private UserService userService;
22 - @Inject
23 private SharedService sharedService; 24 private SharedService sharedService;
24 25
26 + private List<DisplayNameMask> allDisplayNameMasks;
27 + private List<UserRole> availableUserRoles;
28 + private List<UserProperty> availableUserProperties;
29 +
25 @ManagedProperty("#{param.userId}") 30 @ManagedProperty("#{param.userId}")
26 private int userId; 31 private int userId;
27 -
28 private String username; 32 private String username;
29 -
30 private String firstName; 33 private String firstName;
31 -
32 private String lastName; 34 private String lastName;
33 -
34 private String nickName; 35 private String nickName;
35 -
36 private DisplayNameMask displayNameMask; 36 private DisplayNameMask displayNameMask;
37 -
38 - private List<DisplayNameMask> allDisplayNameMasks;
39 -
40 - private List<UserRole> availableUserRoles;
41 -
42 - private List<UserProperty> availableUserProperties;
43 -
44 private String email; 37 private String email;
45 -
46 private boolean anonymous; 38 private boolean anonymous;
47 -
48 private String password; 39 private String password;
49 -
50 private List<UserRole> roles; 40 private List<UserRole> roles;
51 -
52 private String presentation; 41 private String presentation;
53 -
54 private List<UserAttribute> userAttributes; 42 private List<UserAttribute> userAttributes;
55 -
56 private boolean criticProvider; 43 private boolean criticProvider;
57 -
58 private boolean visible; 44 private boolean visible;
59 -
60 private boolean activated; 45 private boolean activated;
61 46
62 47
48 + @Inject
49 + public UpdateUserBean(UserService userService, SharedService sharedService) {
50 + this.userService = userService;
51 + this.sharedService = sharedService;
52 + }
63 53
64 - public String add() 54 +
65 - { 55 + @PostConstruct
56 + public void init() {
57 + logger.info("init");
58 + allDisplayNameMasks = sharedService.getAllDisplayNameMasks();
59 + availableUserRoles = sharedService.getAvailableUserRoles();
60 + availableUserProperties = sharedService.getAvailableUserProperties();
61 + }
62 +
63 +
64 + public String add() {
66 User user = new User(); 65 User user = new User();
67 user.setActivated(activated); 66 user.setActivated(activated);
68 user.setAnonymous(anonymous); 67 user.setAnonymous(anonymous);
...@@ -96,10 +95,9 @@ public class UpdateUserBean { ...@@ -96,10 +95,9 @@ public class UpdateUserBean {
96 } 95 }
97 96
98 public String prepareUpdate(int userId) { 97 public String prepareUpdate(int userId) {
99 - Logger logger = LoggerFactory.getLogger(UpdateUserBean.class);
100 logger.info("prepareUpdate"); 98 logger.info("prepareUpdate");
101 99
102 - logger.info("userId ="+userId); 100 + logger.info("userId =" + userId);
103 this.userId = userId; 101 this.userId = userId;
104 if (userId != 0) { 102 if (userId != 0) {
105 User user = userService.findUserById(userId); 103 User user = userService.findUserById(userId);
...@@ -128,10 +126,9 @@ public class UpdateUserBean { ...@@ -128,10 +126,9 @@ public class UpdateUserBean {
128 } 126 }
129 127
130 public String update() { 128 public String update() {
131 - Logger logger = LoggerFactory.getLogger(UpdateUserBean.class);
132 logger.info("update"); 129 logger.info("update");
133 130
134 - logger.info("userId ="+userId); 131 + logger.info("userId =" + userId);
135 User user = userService.findUserById(userId); 132 User user = userService.findUserById(userId);
136 user.setActivated(activated); 133 user.setActivated(activated);
137 user.setAnonymous(anonymous); 134 user.setAnonymous(anonymous);
...@@ -152,15 +149,6 @@ public class UpdateUserBean { ...@@ -152,15 +149,6 @@ public class UpdateUserBean {
152 } 149 }
153 150
154 151
155 - @PostConstruct
156 - public void init() {
157 - Logger logger = LoggerFactory.getLogger(UpdateUserBean.class);
158 - logger.info("init");
159 - allDisplayNameMasks = sharedService.getAllDisplayNameMasks();
160 - availableUserRoles = sharedService.getAvailableUserRoles();
161 - availableUserProperties = sharedService.getAvailableUserProperties();
162 - }
163 -
164 public List<UserRole> getAvailableUserRoles() { 152 public List<UserRole> getAvailableUserRoles() {
165 return availableUserRoles; 153 return availableUserRoles;
166 } 154 }
......
1 +package org.legrog.test;
2 +
3 +/*
4 +
5 +TAKEN FROM JUnit 5 Sample
6 +https://github.com/junit-team/junit5-samples/blob/master/junit5-mockito-extension/src/main/java/com/example/mockito/MockitoExtension.java
7 +
8 +*/
9 +
10 +/*
11 + * Copyright 2015-2016 the original author or authors.
12 + *
13 + * All rights reserved. This program and the accompanying materials are
14 + * made available under the terms of the Eclipse Public License v1.0 which
15 + * accompanies this distribution and is available at
16 + *
17 + * http://www.eclipse.org/legal/epl-v10.html
18 + *
19 + */
20 +
21 +
22 +import static org.mockito.Mockito.mock;
23 +
24 +import java.lang.reflect.Parameter;
25 +
26 +import org.junit.jupiter.api.extension.ExtensionContext;
27 +import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
28 +import org.junit.jupiter.api.extension.ExtensionContext.Store;
29 +import org.junit.jupiter.api.extension.ParameterContext;
30 +import org.junit.jupiter.api.extension.ParameterResolver;
31 +import org.junit.jupiter.api.extension.TestInstancePostProcessor;
32 +import org.mockito.Mock;
33 +import org.mockito.MockitoAnnotations;
34 +
35 +/**
36 + * {@code MockitoExtension} showcases the {@link TestInstancePostProcessor}
37 + * and {@link ParameterResolver} extension APIs of JUnit 5 by providing
38 + * dependency injection support at the field level and at the method parameter
39 + * level via Mockito 2.x's {@link Mock @Mock} annotation.
40 + */
41 +public class MockitoExtension implements TestInstancePostProcessor, ParameterResolver {
42 +
43 + @Override
44 + public void postProcessTestInstance(Object testInstance, ExtensionContext context) {
45 + MockitoAnnotations.initMocks(testInstance);
46 + }
47 +
48 + @Override
49 + public boolean supports(ParameterContext parameterContext, ExtensionContext extensionContext) {
50 + return parameterContext.getParameter().isAnnotationPresent(Mock.class);
51 + }
52 +
53 + @Override
54 + public Object resolve(ParameterContext parameterContext, ExtensionContext extensionContext) {
55 + return getMock(parameterContext.getParameter(), extensionContext);
56 + }
57 +
58 + private Object getMock(Parameter parameter, ExtensionContext extensionContext) {
59 + Class<?> mockType = parameter.getType();
60 + Store mocks = extensionContext.getStore(Namespace.create(MockitoExtension.class, mockType));
61 + String mockName = getMockName(parameter);
62 +
63 + if (mockName != null) {
64 + return mocks.getOrComputeIfAbsent(mockName, key -> mock(mockType, mockName));
65 + } else {
66 + return mocks.getOrComputeIfAbsent(mockType.getCanonicalName(), key -> mock(mockType));
67 + }
68 + }
69 +
70 + private String getMockName(Parameter parameter) {
71 + String explicitMockName = parameter.getAnnotation(Mock.class).name().trim();
72 + if (!explicitMockName.isEmpty()) {
73 + return explicitMockName;
74 + } else if (parameter.isNamePresent()) {
75 + return parameter.getName();
76 + }
77 + return null;
78 + }
79 +
80 +}
81 +
...@@ -83,7 +83,7 @@ public class ListPublisherVersionsViewTest { ...@@ -83,7 +83,7 @@ public class ListPublisherVersionsViewTest {
83 @Test 83 @Test
84 @DisplayName("Liste pour un éditeur") 84 @DisplayName("Liste pour un éditeur")
85 public void testSetViewNotAllAlsoTestingFilterOnId() { 85 public void testSetViewNotAllAlsoTestingFilterOnId() {
86 - listPublisherVersionsView.setPublisherId(new Integer(1)); 86 + listPublisherVersionsView.setPublisherId(1);
87 listPublisherVersionsView.setView(); 87 listPublisherVersionsView.setView();
88 Mockito.verify(publisherService).getAllPublisherVersions(); 88 Mockito.verify(publisherService).getAllPublisherVersions();
89 assertThat(listPublisherVersionsView.isViewAll()).isFalse(); 89 assertThat(listPublisherVersionsView.isViewAll()).isFalse();
...@@ -93,7 +93,7 @@ public class ListPublisherVersionsViewTest { ...@@ -93,7 +93,7 @@ public class ListPublisherVersionsViewTest {
93 @Test 93 @Test
94 @DisplayName("Test de l'appel de validation") 94 @DisplayName("Test de l'appel de validation")
95 public void testValidate() { 95 public void testValidate() {
96 - listPublisherVersionsView.setPublisherId(new Integer(0)); 96 + listPublisherVersionsView.setPublisherId(0);
97 listPublisherVersionsView.validate(publisherVersion2); 97 listPublisherVersionsView.validate(publisherVersion2);
98 Mockito.verify(publisherService).validatePublisherVersion(publisherVersion2); 98 Mockito.verify(publisherService).validatePublisherVersion(publisherVersion2);
99 } 99 }
......
...@@ -2,6 +2,7 @@ package org.legrog.web.publisher; ...@@ -2,6 +2,7 @@ package org.legrog.web.publisher;
2 2
3 import org.junit.Before; 3 import org.junit.Before;
4 import org.junit.Test; 4 import org.junit.Test;
5 +import org.junit.jupiter.api.BeforeEach;
5 import org.junit.jupiter.api.DisplayName; 6 import org.junit.jupiter.api.DisplayName;
6 import org.junit.runner.RunWith; 7 import org.junit.runner.RunWith;
7 import org.legrog.entities.Country; 8 import org.legrog.entities.Country;
...@@ -26,7 +27,7 @@ public class PublisherVersionViewTest { ...@@ -26,7 +27,7 @@ public class PublisherVersionViewTest {
26 27
27 private PublisherVersionView publisherVersionView; 28 private PublisherVersionView publisherVersionView;
28 29
29 - @Mock(answer = Answers.RETURNS_DEEP_STUBS) 30 + @Mock
30 private PublisherService publisherService; 31 private PublisherService publisherService;
31 32
32 private PublisherVersion publisherVersion; 33 private PublisherVersion publisherVersion;
...@@ -53,7 +54,8 @@ public class PublisherVersionViewTest { ...@@ -53,7 +54,8 @@ public class PublisherVersionViewTest {
53 publisherVersion.setPublisher(new Publisher()); 54 publisherVersion.setPublisher(new Publisher());
54 logger.trace("publisherVersion = {}", publisherVersion); 55 logger.trace("publisherVersion = {}", publisherVersion);
55 56
56 - when(publisherService.getPublisherVersion(0)).thenReturn(publisherVersion); 57 +// todo uncomment when need to use it (new mockito refuses unnecessary stubbing)
58 +// when(publisherService.getPublisherVersion(0)).thenReturn(publisherVersion);
57 59
58 } 60 }
59 61
......
1 +package org.legrog.web.user;
2 +
3 +import org.junit.jupiter.api.BeforeEach;
4 +import org.junit.jupiter.api.DisplayName;
5 +import org.junit.jupiter.api.Nested;
6 +import org.junit.jupiter.api.Test;
7 +import org.junit.jupiter.api.extension.ExtendWith;
8 +import org.junit.platform.runner.JUnitPlatform;
9 +import org.junit.runner.RunWith;
10 +import org.legrog.entities.DisplayNameMask;
11 +import org.legrog.test.MockitoExtension;
12 +import org.legrog.web.xyz.SharedService;
13 +import org.mockito.Mock;
14 +
15 +import java.util.List;
16 +
17 +import static org.assertj.core.api.Assertions.assertThat;
18 +import static org.mockito.Mockito.when;
19 +
20 +/**
21 + * Created by jai on 15/11/16.
22 + */
23 +@DisplayName("Update User Bean")
24 +@ExtendWith(MockitoExtension.class)
25 +@RunWith(JUnitPlatform.class)
26 +public class UpdateUserBeanTest {
27 +
28 + UpdateUserBean updateUserBean;
29 +
30 + @BeforeEach
31 + public void setUp(@Mock UserService userService, @Mock SharedService sharedService) throws Exception {
32 + updateUserBean = new UpdateUserBean(userService, sharedService) ;
33 + }
34 +
35 + @Test
36 + @DisplayName("depends on Shared Service and User Service")
37 + public void testDependencies() {
38 + assertThat(updateUserBean).isNotNull();
39 + }
40 +
41 + @Nested
42 + @DisplayName("post construct method")
43 + class init {
44 +
45 + private List<DisplayNameMask> displayNameMasks;
46 + private List<org.legrog.entities.UserProperty> userProperties;
47 + private List<org.legrog.entities.UserRole> userRoles;
48 +
49 + @BeforeEach
50 + public void setUp(@Mock SharedService sharedService) {
51 + when(sharedService.getAllDisplayNameMasks()).thenReturn(displayNameMasks);
52 + when(sharedService.getAvailableUserProperties()).thenReturn(userProperties);
53 + when(sharedService.getAvailableUserRoles()).thenReturn(userRoles);
54 + }
55 +
56 + @Test
57 + @DisplayName("should set lists of available masks, user roles, and user properties from shared service")
58 + public void testList(@Mock SharedService sharedService) {
59 + updateUserBean.init();
60 + assertThat(updateUserBean.getAllDisplayNameMasks()).isEqualTo(displayNameMasks);
61 + assertThat(updateUserBean.getAvailableUserProperties()).isEqualTo(userProperties);
62 + assertThat(updateUserBean.getAvailableUserRoles()).isEqualTo(userRoles);
63 + }
64 + }
65 +
66 +
67 +}
...\ No newline at end of file ...\ No newline at end of file