Merge pull request #8953 from rdevarakonda/BAEL-3769
BAEL-3769 | Writing templates for test cases using JUnit 5
This commit is contained in:
commit
6b21f217ed
|
@ -46,12 +46,19 @@
|
|||
<version>${junit.platform.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj-core.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<junit.jupiter.version>5.6.0</junit.jupiter.version>
|
||||
<junit.jupiter.version>5.6.2</junit.jupiter.version>
|
||||
<junit.platform.version>1.6.0</junit.platform.version>
|
||||
<log4j2.version>2.8.2</log4j2.version>
|
||||
<assertj-core.version>3.11.1</assertj-core.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
package com.baeldung.junit5.templates;
|
||||
|
||||
public interface UserIdGenerator {
|
||||
String generate(String firstName, String lastName);
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
package com.baeldung.junit5.templates;
|
||||
|
||||
public class UserIdGeneratorImpl implements UserIdGenerator {
|
||||
private boolean isFeatureEnabled;
|
||||
|
||||
public UserIdGeneratorImpl(boolean isFeatureEnabled) {
|
||||
this.isFeatureEnabled = isFeatureEnabled;
|
||||
}
|
||||
|
||||
public String generate(String firstName, String lastName) {
|
||||
String initialAndLastName = firstName.substring(0, 1)
|
||||
.concat(lastName);
|
||||
return isFeatureEnabled ? "bael".concat(initialAndLastName) : initialAndLastName;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package com.baeldung.junit5.templates;
|
||||
|
||||
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
|
||||
import org.junit.jupiter.api.extension.ExecutionCondition;
|
||||
import org.junit.jupiter.api.extension.ExtensionContext;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Properties;
|
||||
|
||||
public class DisabledOnQAEnvironmentExtension implements ExecutionCondition {
|
||||
@Override
|
||||
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
|
||||
Properties properties = new Properties();
|
||||
try {
|
||||
properties.load(DisabledOnQAEnvironmentExtension.class.getClassLoader()
|
||||
.getResourceAsStream("application.properties"));
|
||||
if ("qa".equalsIgnoreCase(properties.getProperty("env"))) {
|
||||
String reason = String.format("The test '%s' is disabled on QA environment", context.getDisplayName());
|
||||
System.out.println(reason);
|
||||
return ConditionEvaluationResult.disabled(reason);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return ConditionEvaluationResult.enabled("Test enabled");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package com.baeldung.junit5.templates;
|
||||
|
||||
import org.junit.jupiter.api.extension.ExtensionContext;
|
||||
import org.junit.jupiter.api.extension.ParameterContext;
|
||||
import org.junit.jupiter.api.extension.ParameterResolutionException;
|
||||
import org.junit.jupiter.api.extension.ParameterResolver;
|
||||
|
||||
public class GenericTypedParameterResolver<T> implements ParameterResolver {
|
||||
T data;
|
||||
|
||||
public GenericTypedParameterResolver(T data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
|
||||
return parameterContext.getParameter()
|
||||
.getType()
|
||||
.isInstance(data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
|
||||
return data;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package com.baeldung.junit5.templates;
|
||||
|
||||
import org.junit.jupiter.api.TestTemplate;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class UserIdGeneratorImplUnitTest {
|
||||
@TestTemplate
|
||||
@ExtendWith(UserIdGeneratorTestInvocationContextProvider.class)
|
||||
public void whenUserIdRequested_thenUserIdIsReturnedInCorrectFormat(UserIdGeneratorTestCase testCase) {
|
||||
UserIdGenerator userIdGenerator = new UserIdGeneratorImpl(testCase.isFeatureEnabled());
|
||||
|
||||
String actualUserId = userIdGenerator.generate(testCase.getFirstName(), testCase.getLastName());
|
||||
|
||||
assertThat(actualUserId).isEqualTo(testCase.getExpectedUserId());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
package com.baeldung.junit5.templates;
|
||||
|
||||
public class UserIdGeneratorTestCase {
|
||||
private String displayName;
|
||||
private boolean isFeatureEnabled;
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
private String expectedUserId;
|
||||
|
||||
public UserIdGeneratorTestCase(String displayName, boolean isFeatureEnabled, String firstName, String lastName, String expectedUserId) {
|
||||
this.displayName = displayName;
|
||||
this.isFeatureEnabled = isFeatureEnabled;
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
this.expectedUserId = expectedUserId;
|
||||
}
|
||||
|
||||
public String getDisplayName() {
|
||||
return displayName;
|
||||
}
|
||||
|
||||
public boolean isFeatureEnabled() {
|
||||
return isFeatureEnabled;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public String getExpectedUserId() {
|
||||
return expectedUserId;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,87 @@
|
|||
package com.baeldung.junit5.templates;
|
||||
|
||||
import org.junit.jupiter.api.extension.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
|
||||
public class UserIdGeneratorTestInvocationContextProvider implements TestTemplateInvocationContextProvider {
|
||||
|
||||
@Override
|
||||
public boolean supportsTestTemplate(ExtensionContext extensionContext) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Stream<TestTemplateInvocationContext> provideTestTemplateInvocationContexts(ExtensionContext extensionContext) {
|
||||
boolean featureDisabled = false;
|
||||
boolean featureEnabled = true;
|
||||
return Stream.of(
|
||||
featureDisabledContext(
|
||||
new UserIdGeneratorTestCase(
|
||||
"Given feature switch disabled When user name is John Smith Then generated userid is JSmith",
|
||||
featureDisabled, "John", "Smith", "JSmith")),
|
||||
featureEnabledContext(
|
||||
new UserIdGeneratorTestCase(
|
||||
"Given feature switch enabled When user name is John Smith Then generated userid is baelJSmith",
|
||||
featureEnabled, "John", "Smith", "baelJSmith"))
|
||||
);
|
||||
}
|
||||
|
||||
private TestTemplateInvocationContext featureDisabledContext(UserIdGeneratorTestCase userIdGeneratorTestCase) {
|
||||
return new TestTemplateInvocationContext() {
|
||||
@Override
|
||||
public String getDisplayName(int invocationIndex) {
|
||||
return userIdGeneratorTestCase.getDisplayName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Extension> getAdditionalExtensions() {
|
||||
return asList(
|
||||
new GenericTypedParameterResolver(userIdGeneratorTestCase),
|
||||
new BeforeTestExecutionCallback() {
|
||||
@Override
|
||||
public void beforeTestExecution(ExtensionContext extensionContext) {
|
||||
System.out.println("BeforeTestExecutionCallback:Disabled context");
|
||||
}
|
||||
},
|
||||
new AfterTestExecutionCallback() {
|
||||
@Override
|
||||
public void afterTestExecution(ExtensionContext extensionContext) {
|
||||
System.out.println("AfterTestExecutionCallback:Disabled context");
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private TestTemplateInvocationContext featureEnabledContext(UserIdGeneratorTestCase userIdGeneratorTestCase) {
|
||||
return new TestTemplateInvocationContext() {
|
||||
@Override
|
||||
public String getDisplayName(int invocationIndex) {
|
||||
return userIdGeneratorTestCase.getDisplayName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Extension> getAdditionalExtensions() {
|
||||
return asList(
|
||||
new GenericTypedParameterResolver(userIdGeneratorTestCase),
|
||||
new DisabledOnQAEnvironmentExtension(),
|
||||
new BeforeEachCallback() {
|
||||
@Override
|
||||
public void beforeEach(ExtensionContext extensionContext) {
|
||||
System.out.println("BeforeEachCallback:Enabled context");
|
||||
}
|
||||
},
|
||||
new AfterEachCallback() {
|
||||
@Override
|
||||
public void afterEach(ExtensionContext extensionContext) {
|
||||
System.out.println("AfterEachCallback:Enabled context");
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
env=qa
|
Loading…
Reference in New Issue