diff --git a/spring-cloud-netflix-eureka-server/src/test/java/org/springframework/cloud/netflix/eureka/server/InstanceRegistryTest.java b/spring-cloud-netflix-eureka-server/src/test/java/org/springframework/cloud/netflix/eureka/server/InstanceRegistryTest.java index 0bd67f00..0346bad4 100644 --- a/spring-cloud-netflix-eureka-server/src/test/java/org/springframework/cloud/netflix/eureka/server/InstanceRegistryTest.java +++ b/spring-cloud-netflix-eureka-server/src/test/java/org/springframework/cloud/netflix/eureka/server/InstanceRegistryTest.java @@ -1,10 +1,5 @@ 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 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 - instanceRegisteredEventListenerMock; - - @MockBean - private ApplicationListener - instanceCanceledEventListenerMock; - - @MockBean - private ApplicationListener 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 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 +}