Toggle navigation
Toggle navigation
This project
Loading...
Sign in
grogv3
/
grog-cubi
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Authored by
JR Utily
2016-11-15 21:51:34 +0100
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
8167ba753c91e6ebbb5519379f6c5e39bf0506b4
8167ba75
1 parent
02f1d088
* upgrade of pom (spring bom + mockito 2.2)
* example of unit test using the junit5 way
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
183 additions
and
42 deletions
pom.xml
src/main/java/org/legrog/web/user/UpdateUserBean.java
src/test/java/org/legrog/test/MockitoExtension.java
src/test/java/org/legrog/web/publisher/ListPublisherVersionsViewTest.java
src/test/java/org/legrog/web/publisher/PublisherVersionViewTest.java
src/test/java/org/legrog/web/user/UpdateUserBeanTest.java
pom.xml
View file @
8167ba7
...
...
@@ -18,7 +18,7 @@
<properties>
<!-- dependencies version -->
<spring.platform-bom.version>
2.0.7.RELEASE
</spring.platform-bom.version>
<spring.platform-bom.version>
Athens-SR1
</spring.platform-bom.version>
<omnifaces.version>
2.5.1
</omnifaces.version>
<primefaces.version>
6.0
</primefaces.version>
<myfaces.version>
2.2.10
</myfaces.version>
...
...
@@ -30,6 +30,7 @@
<hibernate-jpa-2.1-api.version>
1.0.0.Final
</hibernate-jpa-2.1-api.version>
<junit.platform.version>
1.0.0-M2
</junit.platform.version>
<junit.jupiter.version>
5.0.0-M2
</junit.jupiter.version>
<mockito-core.version>
2.2.16
</mockito-core.version>
<!-- paths -->
<custom.web.dir>
src/main/java/org/legrog/web
</custom.web.dir>
...
...
@@ -41,6 +42,7 @@
<maven.compiler.source>
1.8
</maven.compiler.source>
<maven.compiler.target>
1.8
</maven.compiler.target>
</properties>
<profiles>
...
...
@@ -289,6 +291,7 @@
<dependency>
<groupId>
org.mockito
</groupId>
<artifactId>
mockito-core
</artifactId>
<version>
${mockito-core.version}
</version>
<scope>
test
</scope>
</dependency>
...
...
src/main/java/org/legrog/web/user/UpdateUserBean.java
View file @
8167ba7
...
...
@@ -17,52 +17,51 @@ import java.util.List;
@Named
@SessionScoped
public
class
UpdateUserBean
{
@Inject
Logger
logger
=
LoggerFactory
.
getLogger
(
getClass
());
private
UserService
userService
;
@Inject
private
SharedService
sharedService
;
private
List
<
DisplayNameMask
>
allDisplayNameMasks
;
private
List
<
UserRole
>
availableUserRoles
;
private
List
<
UserProperty
>
availableUserProperties
;
@ManagedProperty
(
"#{param.userId}"
)
private
int
userId
;
private
String
username
;
private
String
firstName
;
private
String
lastName
;
private
String
nickName
;
private
DisplayNameMask
displayNameMask
;
private
List
<
DisplayNameMask
>
allDisplayNameMasks
;
private
List
<
UserRole
>
availableUserRoles
;
private
List
<
UserProperty
>
availableUserProperties
;
private
String
email
;
private
boolean
anonymous
;
private
String
password
;
private
List
<
UserRole
>
roles
;
private
String
presentation
;
private
List
<
UserAttribute
>
userAttributes
;
private
boolean
criticProvider
;
private
boolean
visible
;
private
boolean
activated
;
@Inject
public
UpdateUserBean
(
UserService
userService
,
SharedService
sharedService
)
{
this
.
userService
=
userService
;
this
.
sharedService
=
sharedService
;
}
public
String
add
()
{
@PostConstruct
public
void
init
()
{
logger
.
info
(
"init"
);
allDisplayNameMasks
=
sharedService
.
getAllDisplayNameMasks
();
availableUserRoles
=
sharedService
.
getAvailableUserRoles
();
availableUserProperties
=
sharedService
.
getAvailableUserProperties
();
}
public
String
add
()
{
User
user
=
new
User
();
user
.
setActivated
(
activated
);
user
.
setAnonymous
(
anonymous
);
...
...
@@ -96,10 +95,9 @@ public class UpdateUserBean {
}
public
String
prepareUpdate
(
int
userId
)
{
Logger
logger
=
LoggerFactory
.
getLogger
(
UpdateUserBean
.
class
);
logger
.
info
(
"prepareUpdate"
);
logger
.
info
(
"userId ="
+
userId
);
logger
.
info
(
"userId ="
+
userId
);
this
.
userId
=
userId
;
if
(
userId
!=
0
)
{
User
user
=
userService
.
findUserById
(
userId
);
...
...
@@ -128,10 +126,9 @@ public class UpdateUserBean {
}
public
String
update
()
{
Logger
logger
=
LoggerFactory
.
getLogger
(
UpdateUserBean
.
class
);
logger
.
info
(
"update"
);
logger
.
info
(
"userId ="
+
userId
);
logger
.
info
(
"userId ="
+
userId
);
User
user
=
userService
.
findUserById
(
userId
);
user
.
setActivated
(
activated
);
user
.
setAnonymous
(
anonymous
);
...
...
@@ -152,15 +149,6 @@ public class UpdateUserBean {
}
@PostConstruct
public
void
init
()
{
Logger
logger
=
LoggerFactory
.
getLogger
(
UpdateUserBean
.
class
);
logger
.
info
(
"init"
);
allDisplayNameMasks
=
sharedService
.
getAllDisplayNameMasks
();
availableUserRoles
=
sharedService
.
getAvailableUserRoles
();
availableUserProperties
=
sharedService
.
getAvailableUserProperties
();
}
public
List
<
UserRole
>
getAvailableUserRoles
()
{
return
availableUserRoles
;
}
...
...
src/test/java/org/legrog/test/MockitoExtension.java
0 → 100644
View file @
8167ba7
package
org
.
legrog
.
test
;
/*
TAKEN FROM JUnit 5 Sample
https://github.com/junit-team/junit5-samples/blob/master/junit5-mockito-extension/src/main/java/com/example/mockito/MockitoExtension.java
*/
/*
* Copyright 2015-2016 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v1.0 which
* accompanies this distribution and is available at
*
* http://www.eclipse.org/legal/epl-v10.html
*
*/
import
static
org
.
mockito
.
Mockito
.
mock
;
import
java.lang.reflect.Parameter
;
import
org.junit.jupiter.api.extension.ExtensionContext
;
import
org.junit.jupiter.api.extension.ExtensionContext.Namespace
;
import
org.junit.jupiter.api.extension.ExtensionContext.Store
;
import
org.junit.jupiter.api.extension.ParameterContext
;
import
org.junit.jupiter.api.extension.ParameterResolver
;
import
org.junit.jupiter.api.extension.TestInstancePostProcessor
;
import
org.mockito.Mock
;
import
org.mockito.MockitoAnnotations
;
/**
* {@code MockitoExtension} showcases the {@link TestInstancePostProcessor}
* and {@link ParameterResolver} extension APIs of JUnit 5 by providing
* dependency injection support at the field level and at the method parameter
* level via Mockito 2.x's {@link Mock @Mock} annotation.
*/
public
class
MockitoExtension
implements
TestInstancePostProcessor
,
ParameterResolver
{
@Override
public
void
postProcessTestInstance
(
Object
testInstance
,
ExtensionContext
context
)
{
MockitoAnnotations
.
initMocks
(
testInstance
);
}
@Override
public
boolean
supports
(
ParameterContext
parameterContext
,
ExtensionContext
extensionContext
)
{
return
parameterContext
.
getParameter
().
isAnnotationPresent
(
Mock
.
class
);
}
@Override
public
Object
resolve
(
ParameterContext
parameterContext
,
ExtensionContext
extensionContext
)
{
return
getMock
(
parameterContext
.
getParameter
(),
extensionContext
);
}
private
Object
getMock
(
Parameter
parameter
,
ExtensionContext
extensionContext
)
{
Class
<?>
mockType
=
parameter
.
getType
();
Store
mocks
=
extensionContext
.
getStore
(
Namespace
.
create
(
MockitoExtension
.
class
,
mockType
));
String
mockName
=
getMockName
(
parameter
);
if
(
mockName
!=
null
)
{
return
mocks
.
getOrComputeIfAbsent
(
mockName
,
key
->
mock
(
mockType
,
mockName
));
}
else
{
return
mocks
.
getOrComputeIfAbsent
(
mockType
.
getCanonicalName
(),
key
->
mock
(
mockType
));
}
}
private
String
getMockName
(
Parameter
parameter
)
{
String
explicitMockName
=
parameter
.
getAnnotation
(
Mock
.
class
).
name
().
trim
();
if
(!
explicitMockName
.
isEmpty
())
{
return
explicitMockName
;
}
else
if
(
parameter
.
isNamePresent
())
{
return
parameter
.
getName
();
}
return
null
;
}
}
src/test/java/org/legrog/web/publisher/ListPublisherVersionsViewTest.java
View file @
8167ba7
...
...
@@ -83,7 +83,7 @@ public class ListPublisherVersionsViewTest {
@Test
@DisplayName
(
"Liste pour un éditeur"
)
public
void
testSetViewNotAllAlsoTestingFilterOnId
()
{
listPublisherVersionsView
.
setPublisherId
(
new
Integer
(
1
)
);
listPublisherVersionsView
.
setPublisherId
(
1
);
listPublisherVersionsView
.
setView
();
Mockito
.
verify
(
publisherService
).
getAllPublisherVersions
();
assertThat
(
listPublisherVersionsView
.
isViewAll
()).
isFalse
();
...
...
@@ -93,7 +93,7 @@ public class ListPublisherVersionsViewTest {
@Test
@DisplayName
(
"Test de l'appel de validation"
)
public
void
testValidate
()
{
listPublisherVersionsView
.
setPublisherId
(
new
Integer
(
0
)
);
listPublisherVersionsView
.
setPublisherId
(
0
);
listPublisherVersionsView
.
validate
(
publisherVersion2
);
Mockito
.
verify
(
publisherService
).
validatePublisherVersion
(
publisherVersion2
);
}
...
...
src/test/java/org/legrog/web/publisher/PublisherVersionViewTest.java
View file @
8167ba7
...
...
@@ -2,6 +2,7 @@ package org.legrog.web.publisher;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.junit.jupiter.api.BeforeEach
;
import
org.junit.jupiter.api.DisplayName
;
import
org.junit.runner.RunWith
;
import
org.legrog.entities.Country
;
...
...
@@ -26,7 +27,7 @@ public class PublisherVersionViewTest {
private
PublisherVersionView
publisherVersionView
;
@Mock
(
answer
=
Answers
.
RETURNS_DEEP_STUBS
)
@Mock
private
PublisherService
publisherService
;
private
PublisherVersion
publisherVersion
;
...
...
@@ -53,7 +54,8 @@ public class PublisherVersionViewTest {
publisherVersion
.
setPublisher
(
new
Publisher
());
logger
.
trace
(
"publisherVersion = {}"
,
publisherVersion
);
when
(
publisherService
.
getPublisherVersion
(
0
)).
thenReturn
(
publisherVersion
);
// todo uncomment when need to use it (new mockito refuses unnecessary stubbing)
// when(publisherService.getPublisherVersion(0)).thenReturn(publisherVersion);
}
...
...
src/test/java/org/legrog/web/user/UpdateUserBeanTest.java
0 → 100644
View file @
8167ba7
package
org
.
legrog
.
web
.
user
;
import
org.junit.jupiter.api.BeforeEach
;
import
org.junit.jupiter.api.DisplayName
;
import
org.junit.jupiter.api.Nested
;
import
org.junit.jupiter.api.Test
;
import
org.junit.jupiter.api.extension.ExtendWith
;
import
org.junit.platform.runner.JUnitPlatform
;
import
org.junit.runner.RunWith
;
import
org.legrog.entities.DisplayNameMask
;
import
org.legrog.test.MockitoExtension
;
import
org.legrog.web.xyz.SharedService
;
import
org.mockito.Mock
;
import
java.util.List
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
import
static
org
.
mockito
.
Mockito
.
when
;
/**
* Created by jai on 15/11/16.
*/
@DisplayName
(
"Update User Bean"
)
@ExtendWith
(
MockitoExtension
.
class
)
@RunWith
(
JUnitPlatform
.
class
)
public
class
UpdateUserBeanTest
{
UpdateUserBean
updateUserBean
;
@BeforeEach
public
void
setUp
(
@Mock
UserService
userService
,
@Mock
SharedService
sharedService
)
throws
Exception
{
updateUserBean
=
new
UpdateUserBean
(
userService
,
sharedService
)
;
}
@Test
@DisplayName
(
"depends on Shared Service and User Service"
)
public
void
testDependencies
()
{
assertThat
(
updateUserBean
).
isNotNull
();
}
@Nested
@DisplayName
(
"post construct method"
)
class
init
{
private
List
<
DisplayNameMask
>
displayNameMasks
;
private
List
<
org
.
legrog
.
entities
.
UserProperty
>
userProperties
;
private
List
<
org
.
legrog
.
entities
.
UserRole
>
userRoles
;
@BeforeEach
public
void
setUp
(
@Mock
SharedService
sharedService
)
{
when
(
sharedService
.
getAllDisplayNameMasks
()).
thenReturn
(
displayNameMasks
);
when
(
sharedService
.
getAvailableUserProperties
()).
thenReturn
(
userProperties
);
when
(
sharedService
.
getAvailableUserRoles
()).
thenReturn
(
userRoles
);
}
@Test
@DisplayName
(
"should set lists of available masks, user roles, and user properties from shared service"
)
public
void
testList
(
@Mock
SharedService
sharedService
)
{
updateUserBean
.
init
();
assertThat
(
updateUserBean
.
getAllDisplayNameMasks
()).
isEqualTo
(
displayNameMasks
);
assertThat
(
updateUserBean
.
getAvailableUserProperties
()).
isEqualTo
(
userProperties
);
assertThat
(
updateUserBean
.
getAvailableUserRoles
()).
isEqualTo
(
userRoles
);
}
}
}
\ No newline at end of file
Please
register
or
login
to post a comment