Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
spring-cloud-netflix
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
openSource
spring-cloud-netflix
Commits
1fffc923
Unverified
Commit
1fffc923
authored
Jan 12, 2017
by
Spencer Gibb
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Remove mocks for application event listeners.
fixes gh-1616
parent
b4102719
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
50 additions
and
54 deletions
+50
-54
InstanceRegistryTest.java
...ork/cloud/netflix/eureka/server/InstanceRegistryTest.java
+50
-54
No files found.
spring-cloud-netflix-eureka-server/src/test/java/org/springframework/cloud/netflix/eureka/server/InstanceRegistryTest.java
View file @
1fffc923
package
org
.
springframework
.
cloud
.
netflix
.
eureka
.
server
;
import
static
org
.
junit
.
Assert
.*;
import
static
org
.
mockito
.
Matchers
.
isA
;
import
static
org
.
mockito
.
Mockito
.
doAnswer
;
import
static
org
.
mockito
.
Mockito
.
doReturn
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.LinkedList
;
...
...
@@ -13,20 +8,19 @@ import java.util.List;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.mockito.invocation.InvocationOnMock
;
import
org.mockito.stubbing.Answer
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.autoconfigure.EnableAutoConfiguration
;
import
org.springframework.boot.builder.SpringApplicationBuilder
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.mock.mockito.MockBean
;
import
org.springframework.boot.test.mock.mockito.SpyBean
;
import
org.springframework.cloud.netflix.eureka.server.InstanceRegistryTest.TestApplication
;
import
org.springframework.cloud.netflix.eureka.server.event.EurekaInstanceCanceledEvent
;
import
org.springframework.cloud.netflix.eureka.server.event.EurekaInstanceRegisteredEvent
;
import
org.springframework.cloud.netflix.eureka.server.event.EurekaInstanceRenewedEvent
;
import
org.springframework.context.ApplicationEvent
;
import
org.springframework.context.
ApplicationListener
;
import
org.springframework.context.
annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.event.EventListener
;
import
org.springframework.test.context.junit4.SpringJUnit4ClassRunner
;
import
com.netflix.appinfo.InstanceInfo
;
...
...
@@ -34,6 +28,11 @@ import com.netflix.appinfo.LeaseInfo;
import
com.netflix.discovery.shared.Application
;
import
com.netflix.eureka.registry.PeerAwareInstanceRegistry
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertFalse
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
static
org
.
mockito
.
Mockito
.
doReturn
;
/**
* @author Bartlomiej Slota
*/
...
...
@@ -44,7 +43,6 @@ import com.netflix.eureka.registry.PeerAwareInstanceRegistry;
+
"cloud.netflix.eureka.server.InstanceRegistry=DEBUG"
})
public
class
InstanceRegistryTest
{
private
final
List
<
ApplicationEvent
>
applicationEvents
=
new
LinkedList
<>();
private
static
final
String
APP_NAME
=
"MY-APP-NAME"
;
private
static
final
String
HOST_NAME
=
"my-host-name"
;
private
static
final
String
INSTANCE_ID
=
"my-host-name:8008"
;
...
...
@@ -53,29 +51,13 @@ public class InstanceRegistryTest {
@SpyBean
(
PeerAwareInstanceRegistry
.
class
)
private
InstanceRegistry
instanceRegistry
;
@MockBean
private
ApplicationListener
<
EurekaInstanceRegisteredEvent
>
instanceRegisteredEventListenerMock
;
@MockBean
private
ApplicationListener
<
EurekaInstanceCanceledEvent
>
instanceCanceledEventListenerMock
;
@MockBean
private
ApplicationListener
<
EurekaInstanceRenewedEvent
>
instanceRenewedEventListener
;
@Before
public
void
setup
()
{
applicationEvents
.
clear
();
Answer
applicationListenerAnswer
=
prepareListenerMockAnswer
();
doAnswer
(
applicationListenerAnswer
).
when
(
instanceRegisteredEventListenerMock
)
.
onApplicationEvent
(
isA
(
EurekaInstanceRegisteredEvent
.
class
));
doAnswer
(
applicationListenerAnswer
).
when
(
instanceCanceledEventListenerMock
)
.
onApplicationEvent
(
isA
(
EurekaInstanceCanceledEvent
.
class
));
doAnswer
(
applicationListenerAnswer
).
when
(
instanceRenewedEventListener
)
.
onApplicationEvent
(
isA
(
EurekaInstanceRenewedEvent
.
class
));
this
.
testEvents
.
applicationEvents
.
clear
();
}
@Autowired
private
TestEvents
testEvents
;
@Test
public
void
testRegister
()
throws
Exception
{
...
...
@@ -85,11 +67,11 @@ public class InstanceRegistryTest {
// calling tested method
instanceRegistry
.
register
(
instanceInfo
,
false
);
// event of proper type is registered
assertEquals
(
1
,
applicationEvents
.
size
());
assertTrue
(
applicationEvents
.
get
(
0
)
instanceof
EurekaInstanceRegisteredEvent
);
assertEquals
(
1
,
this
.
testEvents
.
applicationEvents
.
size
());
assertTrue
(
this
.
testEvents
.
applicationEvents
.
get
(
0
)
instanceof
EurekaInstanceRegisteredEvent
);
// event details are correct
final
EurekaInstanceRegisteredEvent
registeredEvent
=
(
EurekaInstanceRegisteredEvent
)
(
applicationEvents
.
get
(
0
));
(
EurekaInstanceRegisteredEvent
)
(
this
.
testEvents
.
applicationEvents
.
get
(
0
));
assertEquals
(
instanceInfo
,
registeredEvent
.
getInstanceInfo
());
assertEquals
(
leaseInfo
.
getDurationInSecs
(),
registeredEvent
.
getLeaseDuration
());
assertEquals
(
instanceRegistry
,
registeredEvent
.
getSource
());
...
...
@@ -104,7 +86,7 @@ public class InstanceRegistryTest {
instanceRegistry
.
register
(
instanceInfo
,
false
);
// instance info duration is set to default
final
EurekaInstanceRegisteredEvent
registeredEvent
=
(
EurekaInstanceRegisteredEvent
)
(
applicationEvents
.
get
(
0
));
(
EurekaInstanceRegisteredEvent
)
(
this
.
testEvents
.
applicationEvents
.
get
(
0
));
assertEquals
(
LeaseInfo
.
DEFAULT_LEASE_DURATION
,
registeredEvent
.
getLeaseDuration
());
}
...
...
@@ -114,11 +96,11 @@ public class InstanceRegistryTest {
// calling tested method
instanceRegistry
.
internalCancel
(
APP_NAME
,
HOST_NAME
,
false
);
// event of proper type is registered
assertEquals
(
1
,
applicationEvents
.
size
());
assertTrue
(
applicationEvents
.
get
(
0
)
instanceof
EurekaInstanceCanceledEvent
);
assertEquals
(
1
,
this
.
testEvents
.
applicationEvents
.
size
());
assertTrue
(
this
.
testEvents
.
applicationEvents
.
get
(
0
)
instanceof
EurekaInstanceCanceledEvent
);
// event details are correct
final
EurekaInstanceCanceledEvent
registeredEvent
=
(
EurekaInstanceCanceledEvent
)
(
applicationEvents
.
get
(
0
));
(
EurekaInstanceCanceledEvent
)
(
this
.
testEvents
.
applicationEvents
.
get
(
0
));
assertEquals
(
APP_NAME
,
registeredEvent
.
getAppName
());
assertEquals
(
HOST_NAME
,
registeredEvent
.
getServerId
());
assertEquals
(
instanceRegistry
,
registeredEvent
.
getSource
());
...
...
@@ -140,12 +122,12 @@ public class InstanceRegistryTest {
instanceRegistry
.
renew
(
APP_NAME
,
INSTANCE_ID
,
false
);
instanceRegistry
.
renew
(
APP_NAME
,
"my-host-name:8009"
,
false
);
// event of proper type is registered
assertEquals
(
2
,
applicationEvents
.
size
());
assertTrue
(
applicationEvents
.
get
(
0
)
instanceof
EurekaInstanceRenewedEvent
);
assertTrue
(
applicationEvents
.
get
(
1
)
instanceof
EurekaInstanceRenewedEvent
);
assertEquals
(
2
,
this
.
testEvents
.
applicationEvents
.
size
());
assertTrue
(
this
.
testEvents
.
applicationEvents
.
get
(
0
)
instanceof
EurekaInstanceRenewedEvent
);
assertTrue
(
this
.
testEvents
.
applicationEvents
.
get
(
1
)
instanceof
EurekaInstanceRenewedEvent
);
// event details are correct
final
EurekaInstanceRenewedEvent
event1
=
(
EurekaInstanceRenewedEvent
)
(
applicationEvents
.
get
(
0
));
(
this
.
testEvents
.
applicationEvents
.
get
(
0
));
assertEquals
(
APP_NAME
,
event1
.
getAppName
());
assertEquals
(
INSTANCE_ID
,
event1
.
getServerId
());
assertEquals
(
instanceRegistry
,
event1
.
getSource
());
...
...
@@ -153,7 +135,7 @@ public class InstanceRegistryTest {
assertFalse
(
event1
.
isReplication
());
final
EurekaInstanceRenewedEvent
event2
=
(
EurekaInstanceRenewedEvent
)
(
applicationEvents
.
get
(
1
));
(
this
.
testEvents
.
applicationEvents
.
get
(
1
));
assertEquals
(
instanceInfo2
,
event2
.
getInstanceInfo
());
}
...
...
@@ -161,11 +143,36 @@ public class InstanceRegistryTest {
@EnableAutoConfiguration
@EnableEurekaServer
protected
static
class
TestApplication
{
@Bean
public
TestEvents
testEvents
()
{
return
new
TestEvents
();
}
public
static
void
main
(
String
[]
args
)
{
new
SpringApplicationBuilder
(
TestApplication
.
class
).
run
(
args
);
}
}
protected
static
class
TestEvents
{
public
final
List
<
ApplicationEvent
>
applicationEvents
=
new
LinkedList
<>();
@EventListener
(
EurekaInstanceRegisteredEvent
.
class
)
public
void
onEvent
(
EurekaInstanceRegisteredEvent
event
)
{
this
.
applicationEvents
.
add
(
event
);
}
@EventListener
(
EurekaInstanceCanceledEvent
.
class
)
public
void
onEvent
(
EurekaInstanceCanceledEvent
event
)
{
this
.
applicationEvents
.
add
(
event
);
}
@EventListener
(
EurekaInstanceRenewedEvent
.
class
)
public
void
onEvent
(
EurekaInstanceRenewedEvent
event
)
{
this
.
applicationEvents
.
add
(
event
);
}
}
private
LeaseInfo
getLeaseInfo
()
{
LeaseInfo
.
Builder
leaseBuilder
=
LeaseInfo
.
Builder
.
newBuilder
();
leaseBuilder
.
setRenewalIntervalInSecs
(
10
);
...
...
@@ -183,14 +190,4 @@ public class InstanceRegistryTest {
builder
.
setLeaseInfo
(
leaseInfo
);
return
builder
.
build
();
}
private
Answer
prepareListenerMockAnswer
()
{
return
new
Answer
()
{
@Override
public
Object
answer
(
InvocationOnMock
invocation
)
throws
Throwable
{
return
applicationEvents
.
add
((
ApplicationEvent
)
invocation
.
getArguments
()[
0
]);
}
};
}
}
\ No newline at end of file
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment