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 d37056d
Show file tree
Hide file tree
Showing 4 changed files with 222 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,34 @@
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 {

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 = "citrusLoggingReporter")
@Conditional(LoggingReporterEnablementCondition.class)
public LoggingReporter loggingReporter() {
return new LoggingReporter();
}

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

@Bean(name = "citrusJunitReporter")
@Conditional(JunitReporterEnablementCondition.class)
public JUnitReporter junitReporter() {
return new JUnitReporter();
}
Expand All @@ -31,4 +39,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("citrusLogginRepporter")
&& "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("citrusJunitRepporter")
&& "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("citrusHtmlRepporter")
&& "true".equals(context.getEnvironment().getProperty(DEFAULT_HTML_REPORTER_ENABLED_PROPERTY, "true"));
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
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;

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

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

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

@Test
public void testOverridesHtmlReporter() {
Assert.assertTrue(applicationContext.getBean("citrusHtmlReporter") 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,95 @@
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.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;

public class ReporterConfigTest extends UnitTestSupport {

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

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

@Test
public void testDefaultHtmlReporter() {
Assert.assertTrue(applicationContext.getBean("citrusHtmlReporter") 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("citrusLoggingReporter");

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

doReturn(null).when(beanFactoryMock).getBean("citrusLoggingReporter");
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("citrusJunitReporter");

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

doReturn(null).when(beanFactoryMock).getBean("citrusJunitReporter");
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("citrusHtmlReporter");

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

doReturn(null).when(beanFactoryMock).getBean("citrusHtmlReporter");
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 d37056d

Please sign in to comment.