Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maintenance/use more junit5 features #518

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
02c1497
clean up code
jens-kaiser Nov 26, 2024
f7705c4
use @Mock annotation
jens-kaiser Nov 26, 2024
2b21a64
replace times(0) by never and times(1) by default verify call
jens-kaiser Nov 26, 2024
55d0791
use parameter injection
jens-kaiser Nov 26, 2024
53ad10a
test on configuration properties
jens-kaiser Nov 26, 2024
c0e5afd
clean up code
jens-kaiser Nov 26, 2024
9ce386b
it makes no sense to test in one test the attributes of a pojo and in…
jens-kaiser Nov 26, 2024
bf71f68
brain overload, BDDAssertions is only used in a single test
jens-kaiser Nov 26, 2024
6198877
user parameter injection
jens-kaiser Nov 26, 2024
80bb3d6
don't mock POJOs
jens-kaiser Nov 26, 2024
ee2ea13
remove useless tests
jens-kaiser Nov 26, 2024
8fc2d70
apply spotless rules
jens-kaiser Nov 26, 2024
8574068
deactivate the unused test instead of blocking it incorrectly
jens-kaiser Nov 28, 2024
833242d
apply spotless rules
jens-kaiser Nov 28, 2024
0909bed
use @Spy instead of @Mock to test AssaultProperties too.
jens-kaiser Nov 30, 2024
2d33998
don't mix assertNotNull with assertThat
jens-kaiser Nov 30, 2024
dacd15f
mock the ApplicationEventPublisher and not the MetricEventPublisher
jens-kaiser Nov 30, 2024
c82fa2d
add some tests
jens-kaiser Dec 2, 2024
44219e8
apply spotless
jens-kaiser Dec 2, 2024
8c02d4e
add unit tests
jens-kaiser Dec 2, 2024
d27a845
add unit tests
jens-kaiser Dec 2, 2024
945cd9f
remove internal ConfigurationBean from test Application, remove empty…
jens-kaiser Dec 6, 2024
8b81231
fix unit test
jens-kaiser Dec 6, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,54 +15,47 @@
*/
package de.codecentric.spring.boot.chaos.monkey;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import de.codecentric.spring.boot.chaos.monkey.component.ChaosMonkeyRequestScope;
import de.codecentric.spring.boot.chaos.monkey.configuration.AssaultProperties;
import de.codecentric.spring.boot.chaos.monkey.configuration.WatcherProperties;
import de.codecentric.spring.boot.demo.chaos.monkey.ChaosDemoApplication;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.env.Environment;
import org.springframework.test.context.TestPropertySource;

/** @author Benjamin Wilms */
@SpringBootTest(classes = ChaosDemoApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@TestPropertySource("classpath:application-test-default-profile.properties")
class ChaosDemoApplicationDefaultProfileTest {

@Autowired(required = false)
private ChaosMonkeyRequestScope chaosMonkeyRequestScope;

@Autowired
private Environment env;
private AssaultProperties assaultProperties;

@Test
void contextLoads() {
assertNull(chaosMonkeyRequestScope);
}
WtfJoke marked this conversation as resolved.
Show resolved Hide resolved
@Autowired
private WatcherProperties watcherProperties;

@Test
void checkEnvWatcherController() {
assertThat(env.getProperty("chaos.monkey.watcher.controller")).isEqualTo("true");
}

@Test
void checkEnvAssaultLatencyRangeStart() {
assertThat(env.getProperty("chaos.monkey.assaults.latency-range-start")).isEqualTo("100");
assertTrue( watcherProperties.isController());
}

@Test
void checkEnvAssaultLatencyRangeEnd() {
assertThat(env.getProperty("chaos.monkey.assaults.latency-range-end")).isEqualTo("200");
void checkEnvAssaultLatencyRange() {
assertAll(
() -> assertEquals(100, assaultProperties.getLatencyRangeStart()),
() -> assertEquals(200, assaultProperties.getLatencyRangeEnd())
);
}

@Test
void checkEnvCustomServiceWatcherList() {
List<String> stringList = env.getProperty("chaos.monkey.assaults.watchedCustomServices", List.class);
assertThat(stringList).hasSize(2);
assertThat(stringList.get(0)).isEqualTo("com.example.chaos.monkey.chaosdemo.controller.HelloController.sayHello");
assertThat(stringList.get(1)).isEqualTo("com.example.chaos.monkey.chaosdemo.controller.HelloController.sayGoodbye");
assertEquals(List.of(
"com.example.chaos.monkey.chaosdemo.controller.HelloController.sayHello",
"com.example.chaos.monkey.chaosdemo.controller.HelloController.sayGoodbye"),
assaultProperties.getWatchedCustomServices());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
package de.codecentric.spring.boot.chaos.monkey.assaults;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.when;

import de.codecentric.spring.boot.chaos.monkey.component.MetricEventPublisher;
import de.codecentric.spring.boot.chaos.monkey.configuration.AssaultProperties;
import de.codecentric.spring.boot.chaos.monkey.configuration.ChaosMonkeySettings;
import org.junit.jupiter.api.Test;
Expand All @@ -29,32 +29,23 @@
@ExtendWith(MockitoExtension.class)
class LatencyAssaultTest {

@Mock
private ChaosMonkeySettings chaosMonkeySettings;

@Mock
private AssaultProperties assaultProperties;

@Test
void threadSleepHasBeenCalled() {
int latencyRangeStart = 100;
int latencyRangeEnd = 200;
TestLatencyAssaultExecutor executor = new TestLatencyAssaultExecutor();
void threadSleepHasBeenCalled(@Mock MetricEventPublisher publisher) {
AssaultProperties assaultProperties = new AssaultProperties();
assaultProperties.setLatencyRangeStart(100);
assaultProperties.setLatencyRangeEnd(200);
ChaosMonkeySettings chaosMonkeySettings = new ChaosMonkeySettings();
chaosMonkeySettings.setAssaultProperties(assaultProperties);

when(assaultProperties.getLatencyRangeStart()).thenReturn(latencyRangeStart);
when(assaultProperties.getLatencyRangeEnd()).thenReturn(latencyRangeEnd);
when(chaosMonkeySettings.getAssaultProperties()).thenReturn(assaultProperties);

LatencyAssault latencyAssault = new LatencyAssault(chaosMonkeySettings, null, executor);
TestLatencyAssaultExecutor executor = new TestLatencyAssaultExecutor();
LatencyAssault latencyAssault = new LatencyAssault(chaosMonkeySettings, publisher, executor);
latencyAssault.attack();

assertTrue(executor.executed);
String assertionMessage = "Latency not in range 100-200, actual latency: " + executor.duration;
assertTrue(executor.duration >= latencyRangeStart, assertionMessage);
assertTrue(executor.duration <= latencyRangeEnd, assertionMessage);
assertTrue(executor.duration >= 100 && executor.duration <= 200, "Latency not in range 100-200, actual latency: " + executor.duration);
}

class TestLatencyAssaultExecutor implements ChaosMonkeyLatencyAssaultExecutor {
static class TestLatencyAssaultExecutor implements ChaosMonkeyLatencyAssaultExecutor {

private long duration;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ void allAssaultsActiveExpectLatencyAttack() {

chaosMonkeyRequestScope.callChaosMonkey(null, null);

verify(latencyAssault, times(1)).attack();
verify(latencyAssault).attack();
}

@Test
Expand All @@ -111,7 +111,7 @@ void allAssaultsActiveExpectExceptionAttack() {

chaosMonkeyRequestScope.callChaosMonkey(null, null);

verify(exceptionAssault, times(1)).attack();
verify(exceptionAssault).attack();
}

@Test
Expand All @@ -121,7 +121,7 @@ void isLatencyAssaultActive() {

chaosMonkeyRequestScope.callChaosMonkey(null, null);

verify(latencyAssault, times(1)).attack();
verify(latencyAssault).attack();
}

@Test
Expand All @@ -131,7 +131,7 @@ void isExceptionAssaultActive() {

chaosMonkeyRequestScope.callChaosMonkey(null, null);

verify(exceptionAssault, times(1)).attack();
verify(exceptionAssault).attack();
}

@Test
Expand All @@ -142,7 +142,7 @@ void isExceptionAndLatencyAssaultActiveExpectExceptionAttack() {

chaosMonkeyRequestScope.callChaosMonkey(null, null);

verify(exceptionAssault, times(1)).attack();
verify(exceptionAssault).attack();
}

@Test
Expand All @@ -154,7 +154,7 @@ void isExceptionAndLatencyAssaultActiveExpectLatencyAttack() {

chaosMonkeyRequestScope.callChaosMonkey(null, null);

verify(latencyAssault, times(1)).attack();
verify(latencyAssault).attack();
}

@Test
Expand All @@ -164,7 +164,7 @@ void isExceptionActiveExpectExceptionAttack() {

chaosMonkeyRequestScope.callChaosMonkey(null, null);

verify(exceptionAssault, times(1)).attack();
verify(exceptionAssault).attack();
}

@Test
Expand All @@ -174,7 +174,7 @@ void isLatencyActiveExpectLatencyAttack() {

chaosMonkeyRequestScope.callChaosMonkey(null, null);

verify(latencyAssault, times(1)).attack();
verify(latencyAssault).attack();
}

@Test
Expand Down Expand Up @@ -221,7 +221,7 @@ void chaosMonkeyIsCalledWhenServiceIsWatched() {

chaosMonkeyRequestScope.callChaosMonkey(null, customService);

verify(latencyAssault, times(1)).attack();
verify(latencyAssault).attack();
verify(exceptionAssault, never()).attack();
}

Expand All @@ -238,7 +238,7 @@ void chaosMonkeyIsCalledWhenServiceIsWatchedWhenSimpleNameIsMethodReference() {
String simpleName = customRepository + ".findAll";
chaosMonkeyRequestScope.callChaosMonkey(null, simpleName);

verify(latencyAssault, times(1)).attack();
verify(latencyAssault).attack();
verify(exceptionAssault, never()).attack();
}

Expand All @@ -255,7 +255,7 @@ void chaosMonkeyIsCalledWhenServiceIsWatchedWhenSimpleNameIsPackageReference() {
String simpleName = packageName + "CrudRepository.findAll";
chaosMonkeyRequestScope.callChaosMonkey(null, simpleName);

verify(latencyAssault, times(1)).attack();
verify(latencyAssault).attack();
verify(exceptionAssault, never()).attack();
}

Expand Down Expand Up @@ -292,6 +292,6 @@ void assaultShouldBeDeterministicIfConfigured() {
customScope.callChaosMonkey(null, "foo");
verify(customAssault, never()).attack();
customScope.callChaosMonkey(null, "foo");
verify(customAssault, times(1)).attack();
verify(customAssault).attack();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ void shouldNotScheduleNewTasksAfterUnrelatedUpdate() {
when(registrar.scheduleCronTask(any())).thenReturn(oldTask);

ChaosMonkeyScheduler cms = new ChaosMonkeyScheduler(registrar, config, Collections.singletonList(memoryAssault));
verify(registrar, times(1)).scheduleCronTask(argThat(hasScheduleLike(schedule)));
verify(registrar).scheduleCronTask(argThat(hasScheduleLike("*/1 * * * * ?")));

reset(registrar);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ public void setup() {

@Test
public void chaosTypeCanBeNull() {
assertEquals(sut.mapName(null, "com.example.MyController.hello"), "toggle.prefix.unknown");
assertEquals("toggle.prefix.unknown", sut.mapName(null, "com.example.MyController.hello"));
}

@Test
public void chaosTypeNameIsUsedAsSuffix() {
assertEquals(sut.mapName(ChaosTarget.REPOSITORY, "com.example.MyController.hello"), "toggle.prefix.repository");
assertEquals("toggle.prefix.repository", sut.mapName(ChaosTarget.REPOSITORY, "com.example.MyController.hello"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import de.codecentric.spring.boot.chaos.monkey.configuration.ChaosMonkeySettings;
import de.codecentric.spring.boot.demo.chaos.monkey.ChaosDemoApplication;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
Expand All @@ -34,21 +33,9 @@
@TestPropertySource("classpath:test-chaos-monkey-endpoints-disabled.properties")
class ChaosMonkeyRequestScopeRestEndpointDisabledIntegrationTest {

@LocalServerPort
private int serverPort;

@Autowired
private TestRestTemplate testRestTemplate;

private String baseUrl;

@BeforeEach
void setUp() {
baseUrl = "http://localhost:" + this.serverPort + "/actuator/chaosmonkey";
}

@Test
void getConfiguration() {
void getConfiguration(@LocalServerPort int serverPort, @Autowired TestRestTemplate testRestTemplate) {
String baseUrl = "http://localhost:" + serverPort + "/actuator/chaosmonkey";
ResponseEntity<ChaosMonkeySettings> chaosMonkeySettingsResult = testRestTemplate.getForEntity(baseUrl, ChaosMonkeySettings.class);

assertEquals(HttpStatus.NOT_FOUND, chaosMonkeySettingsResult.getStatusCode());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@
@SpringBootTest(classes = ChaosDemoApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@TestPropertySource("classpath:application-test-chaos-monkey-profile.properties")
class ChaosMonkeyRequestScopeRestEndpointIntegrationTest {

@LocalServerPort
private int serverPort;

@Autowired
private ChaosMonkeySettings chaosMonkeySettings;

Expand All @@ -60,8 +56,8 @@ class ChaosMonkeyRequestScopeRestEndpointIntegrationTest {
private String baseUrl;

@BeforeEach
void setUp() {
baseUrl = "http://localhost:" + this.serverPort + "/actuator/chaosmonkey";
void setUp(@LocalServerPort int serverPort) {
baseUrl = "http://localhost:" + serverPort + "/actuator/chaosmonkey";
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import de.codecentric.spring.boot.chaos.monkey.component.ChaosTarget;
import de.codecentric.spring.boot.chaos.monkey.component.MetricEventPublisher;
import de.codecentric.spring.boot.chaos.monkey.component.MetricType;
import de.codecentric.spring.boot.chaos.monkey.configuration.WatcherProperties;
import de.codecentric.spring.boot.demo.chaos.monkey.component.DemoComponent;
import org.aopalliance.aop.Advice;
import org.junit.jupiter.api.BeforeEach;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package de.codecentric.spring.boot.chaos.monkey.watcher.advice;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;

import de.codecentric.spring.boot.chaos.monkey.component.ChaosMonkeyRequestScope;
Expand Down Expand Up @@ -84,15 +84,15 @@ class ChaosMonkeyPointcutAdvisorIntegrationTest {
@Test
public void chaosMonkeyIsCalledWhenComponentIsNotFinal() {
demoComponent.sayHello();
verify(chaosMonkeyRequestScopeMock, times(1)).callChaosMonkey(ChaosTarget.COMPONENT, demoComponentSimpleName);
verify(metricsMock, times(1)).publishMetricEvent(demoComponentPointcutName, MetricType.COMPONENT);
verify(chaosMonkeyRequestScopeMock).callChaosMonkey(ChaosTarget.COMPONENT, demoComponentSimpleName);
verify(metricsMock).publishMetricEvent(demoComponentPointcutName, MetricType.COMPONENT);
}

@Test
public void chaosMonkeyIsNotCalledWhenComponentIsFinal() {
finalDemoComponent.sayHello();
verify(chaosMonkeyRequestScopeMock, times(0)).callChaosMonkey(ChaosTarget.COMPONENT, finalDemoComponentSimpleName);
verify(metricsMock, times(0)).publishMetricEvent(finalDemoComponentPointcutName, MetricType.COMPONENT);
verify(chaosMonkeyRequestScopeMock, never()).callChaosMonkey(ChaosTarget.COMPONENT, finalDemoComponentSimpleName);
verify(metricsMock, never()).publishMetricEvent(finalDemoComponentPointcutName, MetricType.COMPONENT);
}

@Test
Expand All @@ -101,16 +101,16 @@ public void chaosMonkeyDoesNotProxyIgnoredSpringInterfaces() {
applicationListenerComponent.onApplicationEvent(mock(ApplicationEvent.class));
factoryBeanComponent.getObject();

verify(chaosMonkeyRequestScopeMock, times(0)).callChaosMonkey(null, beanPostProcessorComponentSimpleName);
verify(metricsMock, times(0)).publishMetricEvent(beanPostProcessorComponentPointcutName, MetricType.COMPONENT);
verify(chaosMonkeyRequestScopeMock, never()).callChaosMonkey(null, beanPostProcessorComponentSimpleName);
verify(metricsMock, never()).publishMetricEvent(beanPostProcessorComponentPointcutName, MetricType.COMPONENT);

verify(chaosMonkeyRequestScopeMock, times(0)).callChaosMonkey(null, applicationListenerComponentSimpleName);
verify(metricsMock, times(0)).publishMetricEvent(applicationListenerComponentPointcutName, MetricType.COMPONENT);
verify(chaosMonkeyRequestScopeMock, never()).callChaosMonkey(null, applicationListenerComponentSimpleName);
verify(metricsMock, never()).publishMetricEvent(applicationListenerComponentPointcutName, MetricType.COMPONENT);

verify(chaosMonkeyRequestScopeMock, times(0)).callChaosMonkey(null, beanFactorySingletonComponentSimpleName);
verify(chaosMonkeyRequestScopeMock, times(0)).callChaosMonkey(null, beanFactoryObjectTypeComponentSimpleName);
verify(metricsMock, times(0)).publishMetricEvent(beanFactorySingletonComponentPointcutName, MetricType.COMPONENT);
verify(metricsMock, times(0)).publishMetricEvent(beanFactoryObjectTypeComponentPointcutName, MetricType.COMPONENT);
verify(chaosMonkeyRequestScopeMock, never()).callChaosMonkey(null, beanFactorySingletonComponentSimpleName);
verify(chaosMonkeyRequestScopeMock, never()).callChaosMonkey(null, beanFactoryObjectTypeComponentSimpleName);
verify(metricsMock, never()).publishMetricEvent(beanFactorySingletonComponentPointcutName, MetricType.COMPONENT);
verify(metricsMock, never()).publishMetricEvent(beanFactoryObjectTypeComponentPointcutName, MetricType.COMPONENT);
}

@Configuration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ class HealthIndicatorEnabledIntegrationTest {

@Test
public void testIndicatorsAreDown() {
this.healthIndicators.forEach(healthIndicator -> {
assertThat(healthIndicator.getHealth(Boolean.TRUE).getStatus()).isEqualTo(Health.down().build().getStatus());
});
healthIndicators.forEach(healthIndicator -> assertThat(healthIndicator.getHealth(Boolean.TRUE).getStatus()).isEqualTo(Health.down().build().getStatus()));
}
}

Expand All @@ -57,9 +55,7 @@ class HealthIndicatorDisabledIntegrationTest {

@Test
public void testIndicatorsAreUp() {
this.healthIndicators.forEach(healthIndicator -> {
assertThat(healthIndicator.getHealth(Boolean.TRUE).getStatus()).isEqualTo(Health.up().build().getStatus());
});
healthIndicators.forEach(healthIndicator -> assertThat(healthIndicator.getHealth(Boolean.TRUE).getStatus()).isEqualTo(Health.up().build().getStatus()));
}
}
}
Loading