Skip to content

Commit

Permalink
feat(#976): Disable/Override standard Citrus reporters
Browse files Browse the repository at this point in the history
  • Loading branch information
Thorsten Schlathoelter committed Sep 16, 2023
1 parent a9026ea commit c82eea8
Show file tree
Hide file tree
Showing 4 changed files with 236 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,37 @@
import org.citrusframework.report.HtmlReporter;
import org.citrusframework.report.JUnitReporter;
import org.citrusframework.report.LoggingReporter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.*;
import org.springframework.core.type.AnnotatedTypeMetadata;

/**
* @author Christoph Deppisch
*/
@Configuration
public class ReporterConfig {

@Bean(name = "citrusLoggingReporter")
public static final String CITRUS_LOGGING_REPORTER = "citrusLoggingReporter";
public static final String CITRUS_JUNIT_REPORTER = "citrusJunitReporter";
public static final String CITRUS_HTML_REPORTER = "citrusHtmlReporter";

public static final String DEFAULT_LOGGING_REPORTER_ENABLED_PROPERTY = "citrus.default.logging.reporter.enabled";
public static final String DEFAULT_JUNIT_REPORTER_ENABLED_PROPERTY = "citrus.default.junit.reporter.enabled";
public static final String DEFAULT_HTML_REPORTER_ENABLED_PROPERTY = "citrus.default.html.reporter.enabled";

@Bean(name = CITRUS_LOGGING_REPORTER)
@Conditional(LoggingReporterEnablementCondition.class)
public LoggingReporter loggingReporter() {
return new LoggingReporter();
}

@Bean(name = "citrusHtmlReporter")
@Bean(name = CITRUS_HTML_REPORTER)
@Conditional(HtmlReporterEnablementCondition.class)
public HtmlReporter htmlReporter() {
return new HtmlReporter();
}

@Bean(name = "citrusJunitReporter")
@Bean(name = CITRUS_JUNIT_REPORTER)
@Conditional(JunitReporterEnablementCondition.class)
public JUnitReporter junitReporter() {
return new JUnitReporter();
}
Expand All @@ -31,4 +42,29 @@ public JUnitReporter junitReporter() {
public TestReportersFactory testReporters() {
return new TestReportersFactory();
}

static class LoggingReporterEnablementCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
return !context.getBeanFactory().containsBean(CITRUS_LOGGING_REPORTER)
&& "true".equals(context.getEnvironment().getProperty(DEFAULT_LOGGING_REPORTER_ENABLED_PROPERTY, "true"));
}
}

static class JunitReporterEnablementCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
return !context.getBeanFactory().containsBean(CITRUS_JUNIT_REPORTER)
&& "true".equals(context.getEnvironment().getProperty(DEFAULT_JUNIT_REPORTER_ENABLED_PROPERTY, "true"));
}
}

static class HtmlReporterEnablementCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
return !context.getBeanFactory().containsBean(CITRUS_HTML_REPORTER)
&& "true".equals(context.getEnvironment().getProperty(DEFAULT_HTML_REPORTER_ENABLED_PROPERTY, "true"));
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package org.citrusframework.reporter;

import org.citrusframework.UnitTestSupport;
import org.citrusframework.report.HtmlReporter;
import org.citrusframework.report.JUnitReporter;
import org.citrusframework.report.LoggingReporter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.testng.Assert;
import org.testng.annotations.Test;

import static org.citrusframework.reporter.ReporterConfig.CITRUS_HTML_REPORTER;
import static org.citrusframework.reporter.ReporterConfig.CITRUS_JUNIT_REPORTER;
import static org.citrusframework.reporter.ReporterConfig.CITRUS_LOGGING_REPORTER;

@ContextConfiguration(classes = OverrideReporterConfigTest.OverrideReporterConfiguration.class)
public class OverrideReporterConfigTest extends UnitTestSupport {

@Test
public void testOverridesLoggingReporter() {
Assert.assertTrue(applicationContext.getBean(CITRUS_LOGGING_REPORTER) instanceof OverrideLoggingReporter);
}

@Test
public void testOverridesJunitReporter() {
Assert.assertTrue(applicationContext.getBean(CITRUS_JUNIT_REPORTER) instanceof OverrideJUnitReporter);
}

@Test
public void testOverridesHtmlReporter() {
Assert.assertTrue(applicationContext.getBean(CITRUS_HTML_REPORTER) instanceof OverrideHtmlReporter);
}

@Configuration
public static class OverrideReporterConfiguration {

@Bean
public LoggingReporter citrusLoggingReporter() {
return new OverrideLoggingReporter();
}

@Bean
public JUnitReporter citrusJunitReporter() {
return new OverrideJUnitReporter();
}

@Bean
public HtmlReporter citrusHtmlReporter() {
return new OverrideHtmlReporter();
}

}

private static class OverrideLoggingReporter extends LoggingReporter {
}

private static class OverrideJUnitReporter extends JUnitReporter {
}

private static class OverrideHtmlReporter extends HtmlReporter {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package org.citrusframework.reporter;

import org.citrusframework.UnitTestSupport;
import org.citrusframework.report.HtmlReporter;
import org.citrusframework.report.JUnitReporter;
import org.citrusframework.report.LoggingReporter;
import org.citrusframework.reporter.ReporterConfig.HtmlReporterEnablementCondition;
import org.citrusframework.reporter.ReporterConfig.JunitReporterEnablementCondition;
import org.citrusframework.reporter.ReporterConfig.LoggingReporterEnablementCondition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.testng.Assert;
import org.testng.annotations.Test;

import static org.citrusframework.reporter.ReporterConfig.CITRUS_HTML_REPORTER;
import static org.citrusframework.reporter.ReporterConfig.CITRUS_JUNIT_REPORTER;
import static org.citrusframework.reporter.ReporterConfig.CITRUS_LOGGING_REPORTER;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;

public class ReporterConfigTest extends UnitTestSupport {

@Test
public void testDefaultLoggingReporter() {
Assert.assertTrue(applicationContext.getBean(CITRUS_LOGGING_REPORTER) instanceof LoggingReporter);
}

@Test
public void testDefaultJunitReporter() {
Assert.assertTrue(applicationContext.getBean(CITRUS_JUNIT_REPORTER) instanceof JUnitReporter);
}

@Test
public void testDefaultHtmlReporter() {
Assert.assertTrue(applicationContext.getBean(CITRUS_HTML_REPORTER) instanceof HtmlReporter);
}
@Test
public void testLoggerReporterConditionDisabled() {

Environment environmentMock = mock(Environment.class);
ConfigurableListableBeanFactory beanFactoryMock = mock(ConfigurableListableBeanFactory.class);
ConditionContext conditionContextMock = mock(ConditionContext.class);
doReturn(environmentMock).when(conditionContextMock).getEnvironment();
doReturn(beanFactoryMock).when(conditionContextMock).getBeanFactory();
doReturn(new LoggingReporter()).when(beanFactoryMock).getBean(CITRUS_LOGGING_REPORTER);

LoggingReporterEnablementCondition condition = new LoggingReporterEnablementCondition();
AnnotatedTypeMetadata annotatedTypeMetadataMock = mock(AnnotatedTypeMetadata.class);
Assert.assertFalse(condition.matches(conditionContextMock, annotatedTypeMetadataMock));

doReturn(null).when(beanFactoryMock).getBean(CITRUS_LOGGING_REPORTER);
doReturn("false").when(environmentMock).getProperty(ReporterConfig.DEFAULT_LOGGING_REPORTER_ENABLED_PROPERTY, "true");
Assert.assertFalse(condition.matches(conditionContextMock, annotatedTypeMetadataMock));

}

@Test
public void testJunitReporterConditionDisabledByOtherBean() {

Environment environmentMock = mock(Environment.class);
ConfigurableListableBeanFactory beanFactoryMock = mock(ConfigurableListableBeanFactory.class);
ConditionContext conditionContextMock = mock(ConditionContext.class);
doReturn(environmentMock).when(conditionContextMock).getEnvironment();
doReturn(beanFactoryMock).when(conditionContextMock).getBeanFactory();
doReturn(new LoggingReporter()).when(beanFactoryMock).getBean(CITRUS_JUNIT_REPORTER);

JunitReporterEnablementCondition condition = new JunitReporterEnablementCondition();
AnnotatedTypeMetadata annotatedTypeMetadataMock = mock(AnnotatedTypeMetadata.class);
Assert.assertFalse(condition.matches(conditionContextMock, annotatedTypeMetadataMock));

doReturn(null).when(beanFactoryMock).getBean(CITRUS_JUNIT_REPORTER);
doReturn("false").when(environmentMock).getProperty(ReporterConfig.DEFAULT_JUNIT_REPORTER_ENABLED_PROPERTY, "true");
Assert.assertFalse(condition.matches(conditionContextMock, annotatedTypeMetadataMock));

}

@Test
public void testHtmlReporterConditionDisabledByOtherBean() {

Environment environmentMock = mock(Environment.class);
ConfigurableListableBeanFactory beanFactoryMock = mock(ConfigurableListableBeanFactory.class);
ConditionContext conditionContextMock = mock(ConditionContext.class);
doReturn(environmentMock).when(conditionContextMock).getEnvironment();
doReturn(beanFactoryMock).when(conditionContextMock).getBeanFactory();
doReturn(new LoggingReporter()).when(beanFactoryMock).getBean(CITRUS_HTML_REPORTER);

HtmlReporterEnablementCondition condition = new HtmlReporterEnablementCondition();
AnnotatedTypeMetadata annotatedTypeMetadataMock = mock(AnnotatedTypeMetadata.class);
Assert.assertFalse(condition.matches(conditionContextMock, annotatedTypeMetadataMock));

doReturn(null).when(beanFactoryMock).getBean(CITRUS_HTML_REPORTER);
doReturn("false").when(environmentMock).getProperty(ReporterConfig.DEFAULT_HTML_REPORTER_ENABLED_PROPERTY, "true");
Assert.assertFalse(condition.matches(conditionContextMock, annotatedTypeMetadataMock));

}
}
34 changes: 34 additions & 0 deletions src/manual/reporting.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,40 @@

The framework generates different reports and results after a test run for you. These report and result pages will help you to get an overview of the test cases that were executed and which one were failing.

In the following chapters, we'll provide an overview of the three standard loggers available in Citrus. It's important to note that in Spring, you have the flexibility to replace these standard loggers with your own implementations. This can be achieved by registering custom implementations, as demonstrated in the following code snippet:

[source,java]
----
@Configuration
public static class OverrideReporterConfiguration {
@Bean
public LoggingReporter citrusLoggingReporter() {
return new OverrideLoggingReporter();
}
@Bean
public JUnitReporter citrusJunitReporter() {
return new OverrideJUnitReporter();
}
@Bean
public HtmlReporter citrusHtmlReporter() {
return new OverrideHtmlReporter();
}
}
----

In Spring, it is also possible to disable these standard reporters using the following properties:
[source,properties]
----
citrus.default.logging.reporter.enabled=false
citrus.default.junit.reporter.enabled=false
citrus.default.html.reporter.enabled=false
----

Note that some

[[console-logging]]
== Console logging

Expand Down

0 comments on commit c82eea8

Please sign in to comment.