BAEL-1596 Spring Events (#5605)
* Annotation-driven event listener + generic events * update examples + add tests * add article link * add more tests for Spring Events
This commit is contained in:
+8
-1
@@ -3,16 +3,23 @@ package org.baeldung.springevents.synchronous;
|
||||
import org.baeldung.springevents.synchronous.SynchronousSpringEventsConfig;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
import static org.springframework.util.Assert.isTrue;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { SynchronousSpringEventsConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
public class ContextRefreshedListenerIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private ContextRefreshedListener listener;
|
||||
|
||||
@Test
|
||||
public void testContextRefreshedListener() throws InterruptedException {
|
||||
public void testContextRefreshedListener() {
|
||||
System.out.println("Test context re-freshed listener.");
|
||||
isTrue(listener.isHitContextRefreshedHandler(), "Refresh should be called once");
|
||||
}
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package org.baeldung.springevents.synchronous;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
import static org.springframework.util.Assert.isTrue;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { SynchronousSpringEventsConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
public class GenericAppEventListenerIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private CustomSpringEventPublisher publisher;
|
||||
@Autowired
|
||||
private GenericSpringEventListener listener;
|
||||
|
||||
@Test
|
||||
public void testGenericSpringEvent() {
|
||||
isTrue(!listener.isHitEventHandler(), "The initial value should be false");
|
||||
publisher.publishGenericAppEvent("Hello world!!!");
|
||||
isTrue(listener.isHitEventHandler(), "Now the value should be changed to true");
|
||||
}
|
||||
|
||||
}
|
||||
+28
-1
@@ -1,5 +1,6 @@
|
||||
package org.baeldung.springevents.synchronous;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -7,16 +8,42 @@ import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
import static org.springframework.util.Assert.isTrue;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { SynchronousSpringEventsConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
public class SynchronousCustomSpringEventsIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private CustomSpringEventPublisher publisher;
|
||||
@Autowired
|
||||
private AnnotationDrivenEventListener listener;
|
||||
|
||||
@Test
|
||||
public void testCustomSpringEvents() throws InterruptedException {
|
||||
public void testCustomSpringEvents() {
|
||||
isTrue(!listener.isHitCustomEventHandler(), "The value should be false");
|
||||
publisher.publishEvent("Hello world!!");
|
||||
System.out.println("Done publishing synchronous custom event. ");
|
||||
isTrue(listener.isHitCustomEventHandler(), "Now the value should be changed to true");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGenericSpringEvent() {
|
||||
isTrue(!listener.isHitSuccessfulEventHandler(), "The initial value should be false");
|
||||
publisher.publishGenericEvent("Hello world!!!", true);
|
||||
isTrue(listener.isHitSuccessfulEventHandler(), "Now the value should be changed to true");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGenericSpringEventNotProcessed() {
|
||||
isTrue(!listener.isHitSuccessfulEventHandler(), "The initial value should be false");
|
||||
publisher.publishGenericEvent("Hello world!!!", false);
|
||||
isTrue(!listener.isHitSuccessfulEventHandler(), "The value should still be false");
|
||||
}
|
||||
|
||||
@Ignore("fix me")
|
||||
@Test
|
||||
public void testContextStartedEvent() {
|
||||
isTrue(listener.isHitContextStartedHandler(), "Start should be called once");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user