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

Junit5 support #107

Draft
wants to merge 27 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
da7e2ba
Initial POC of Testing JUnit 5 Support in JMAQS
May 26, 2020
1d5c27a
Initial POC of Testing JUnit 5 Support in JMAQS
May 27, 2020
df847b4
Merge branch 'main' into junit5-support
jredingcsv May 6, 2022
f418a20
pull in main
jredingcsv May 6, 2022
29aa533
delete old base content, put junit content in the new maqs base
jredingcsv May 6, 2022
0a00439
Merge branch 'main' into junit5-support
jredingcsv Aug 11, 2022
ad12753
updated and set JUnit tests up
jredingcsv Aug 11, 2022
8d8971c
comments and copyright update
jredingcsv Aug 11, 2022
3f8d3dd
checkstyle updates
jredingcsv Aug 11, 2022
01a0ab2
checkstyle updates
jredingcsv Aug 11, 2022
8c8e3f3
checkstyle updates
jredingcsv Aug 11, 2022
a908d46
checkstyle updates
jredingcsv Aug 11, 2022
11c9b87
checkstyle updates
jredingcsv Aug 11, 2022
42ea7e9
checkstyle updates
jredingcsv Aug 11, 2022
4c70b59
code smells
jredingcsv Aug 11, 2022
8d55c69
code smells
jredingcsv Aug 11, 2022
9781d7e
fixing code smells and bugs
jredingcsv Aug 12, 2022
4a21f15
fixing code smells and bugs
jredingcsv Aug 12, 2022
607fb74
fixing code smells
jredingcsv Aug 12, 2022
01b372b
fixing code smells
jredingcsv Aug 12, 2022
38b308b
organizing code
jredingcsv Aug 15, 2022
c5c76e8
Merge branch 'main' into junit5-support
jredingcsv Sep 14, 2022
04a5587
Merge branch 'main' into junit5-support
jredingcsv Oct 24, 2022
7c07dcc
merge with main
jredingcsv Oct 24, 2022
40b6639
checkstyle changes
jredingcsv Oct 24, 2022
a880ca4
Merge branch 'main' into junit5-support
jredingcsv Nov 11, 2022
f479f1a
Merge branch 'main' into junit5-support
jredingcsv Nov 28, 2022
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
14 changes: 14 additions & 0 deletions maqs-base/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
<version>${revision}</version>
<packaging>jar</packaging>

<properties>
<junit.version>5.8.1</junit.version>
</properties>

<dependencies>
<dependency>
<groupId>com.cognizantsoftvision.maqs.utilities</groupId>
Expand All @@ -24,5 +28,15 @@
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.version}</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ protected void beforeLoggingTeardown(ITestResult resultType) {
@Override
protected void createNewTestObject() {
this.setTestObject(
new BaseTestObject(this.createLogger(), this.getFullyQualifiedTestClassName()));
new BaseTestObject(this.createLogger(), this.getFullyQualifiedTestClassName()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import static java.lang.System.out;

import com.cognizantsoftvision.maqs.base.exceptions.MAQSRuntimeException;
import com.cognizantsoftvision.maqs.base.interfaces.TestResult;
import com.cognizantsoftvision.maqs.base.watcher.JunitTestWatcher;
import com.cognizantsoftvision.maqs.utilities.helper.StringProcessor;
import com.cognizantsoftvision.maqs.utilities.logging.ConsoleLogger;
import com.cognizantsoftvision.maqs.utilities.logging.FileLogger;
Expand All @@ -16,6 +18,7 @@
import com.cognizantsoftvision.maqs.utilities.logging.LoggingConfig;
import com.cognizantsoftvision.maqs.utilities.logging.LoggingEnabled;
import com.cognizantsoftvision.maqs.utilities.logging.MessageType;
import com.cognizantsoftvision.maqs.utilities.logging.TestResultType;
import com.cognizantsoftvision.maqs.utilities.performance.IPerfTimerCollection;
import com.cognizantsoftvision.maqs.utilities.performance.PerfTimerCollection;
import java.lang.reflect.Method;
Expand All @@ -27,15 +30,19 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.TestInfo;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.testng.ITestContext;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;

/**
* The Base test class.
* The Base Test class.
*/
public abstract class BaseTest {

Expand All @@ -54,6 +61,8 @@ public abstract class BaseTest {
*/
private ITestResult testResult;

private TestResult junitTestResult;

/**
* The Collection of Base Test Objects to use.
*/
Expand All @@ -74,12 +83,15 @@ public abstract class BaseTest {
*/
ThreadLocal<String> fullyQualifiedTestClassName = new ThreadLocal<>();

@RegisterExtension public JunitTestWatcher testWatcher;

/**
* Initializes a new instance of the BaseTest class.
*/
protected BaseTest() {
this.loggedExceptions = new ConcurrentHashMap<>();
this.baseTestObjects = new ConcurrentManagerHashMap();
this.testWatcher = new JunitTestWatcher(this);
}

/**
Expand Down Expand Up @@ -215,7 +227,36 @@ public void setTestObject(BaseTestObject baseTestObject) {
}

/**
* Setup before a test.
* Setup before a JUnit test.
*
* @param info the test info of the unit test being run
*/
@BeforeEach
public void setup(TestInfo info) {
String testClassName = null;
String testMethodName = null;

// Get the Fully Qualified Test Class Name and set it in the object
if (info.getTestClass().isPresent()) {
Optional<Class<?>> optional = info.getTestClass();
if (optional.isPresent()) {
testClassName = optional.get().getName();
}
}

if (info.getTestMethod().isPresent()) {
Optional<Method> optional = info.getTestMethod();
if (optional.isPresent()) {
testMethodName = optional.get().getName();
}
}

String testName = testClassName + "." + testMethodName;
customSetup(testName);
}

/**
* Setup before a testng unit test.
*
* @param method The initial executing Method object
* @param testContext The initial executing Test Context object
Expand All @@ -237,22 +278,25 @@ public void setup(Method method, ITestContext testContext) {
*/
public void customSetup(String testName, ITestContext testContext) {
this.testContextInstance = testContext;
customSetup(testName);
}

private void customSetup(String testName) {
testName = testName.replaceFirst("class ", "");
this.fullyQualifiedTestClassName.set(testName);

this.createNewTestObject();
}

/**
* Cleanup after a test.
* Cleanup after a TestNG test.
*/
@AfterMethod(alwaysRun = true)
public void teardown() {
try {
this.beforeLoggingTeardown(testResult);
} catch (Exception e) {
this.tryToLog(MessageType.WARNING, "Failed before logging teardown because: %s", e.getMessage());
this.tryToLog(MessageType.WARNING, "Failed before logging teardown because: %s",
e.getMessage());
}

// Log the test result
Expand Down Expand Up @@ -280,6 +324,42 @@ public void teardown() {
this.tryToLog(MessageType.WARNING, "Failed to cleanup log files because: %s", e.getMessage());
}

cleanUpTest();
}

/**
* Cleanup after a JUnit test.
*/
public void teardownJunit() {
// Log the test result
if (junitTestResult.getStatus() == TestResultType.PASS) {
this.tryToLog(MessageType.SUCCESS, "Test Passed");
} else if (junitTestResult.getStatus() == TestResultType.FAIL) {
this.tryToLog(MessageType.ERROR, "Test Failed");
} else if (junitTestResult.getStatus() == TestResultType.SKIP) {
this.tryToLog(MessageType.INFORMATION, "Test was skipped");
} else {
this.tryToLog(MessageType.WARNING, "Test had an unexpected result.");
}

// Cleanup log files we don't want
try {
if ((this.getLogger() instanceof FileLogger)
&& junitTestResult.getStatus() == TestResultType.PASS
&& this.loggingEnabledSetting == LoggingEnabled.ONFAIL) {
Files.delete(Paths.get(((FileLogger) this.getLogger()).getFilePath()));
}
} catch (Exception e) {
this.tryToLog(MessageType.WARNING, "Failed to cleanup log files because: %s", e.getMessage());
}

cleanUpTest();
}

/**
* Cleans up logs and files after the test.
*/
private void cleanUpTest() {
// Get the Fully Qualified Test Name
String fullyQualifiedTestName = this.fullyQualifiedTestClassName.get();

Expand Down Expand Up @@ -310,6 +390,10 @@ public void setTestResult(ITestResult testResult) {
this.testResult = testResult;
}

public void setTestResult(TestResult testResult) {
this.junitTestResult = testResult;
}

/**
* Steps to take before logging teardown results.
*
Expand All @@ -330,7 +414,8 @@ protected ILogger createLogger() {
if (this.loggingEnabledSetting != LoggingEnabled.NO) {
return LoggerFactory.getLogger(
StringProcessor.safeFormatter("%s - %s", this.fullyQualifiedTestClassName.get(),
DateTimeFormatter.ofPattern(Logger.DEFAULT_DATE_TIME_FORMAT,
DateTimeFormatter.ofPattern(
Logger.DEFAULT_DATE_TIME_FORMAT,
Locale.getDefault()).format(LocalDateTime.now(Clock.systemUTC()))));
} else {
return LoggerFactory.getConsoleLogger();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2022 (C) Cognizant SoftVision, All rights Reserved
*/

package com.cognizantsoftvision.maqs.base.interfaces;

import com.cognizantsoftvision.maqs.utilities.logging.TestResultType;

public class JunitTestResult implements TestResult {
private final TestResultType currentStatus;

public JunitTestResult(TestResultType status) {
this.currentStatus = status;
}

@Override public TestResultType getStatus() {
return this.currentStatus;
}

@Override public boolean isSuccess() {
return currentStatus == TestResultType.PASS;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Copyright 2022 (C) Cognizant SoftVision, All rights Reserved
*/

package com.cognizantsoftvision.maqs.base.interfaces;

import com.cognizantsoftvision.maqs.utilities.logging.TestResultType;

public interface TestResult {
TestResultType getStatus();

boolean isSuccess();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2022 (C) Cognizant SoftVision, All rights Reserved
*/

package com.cognizantsoftvision.maqs.base.watcher;

import com.cognizantsoftvision.maqs.base.BaseTest;
import com.cognizantsoftvision.maqs.base.interfaces.JunitTestResult;
import com.cognizantsoftvision.maqs.utilities.logging.TestResultType;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.TestWatcher;

/**
* The JUnit Test Watcher class.
*/
public class JunitTestWatcher implements TestWatcher {

// Test instance of the test being executed per each thread.
BaseTest testInstance;

public JunitTestWatcher(BaseTest testInstance) {
this.testInstance = testInstance;
}

@Override
public void testSuccessful(ExtensionContext context) {
this.testInstance.setTestResult(new JunitTestResult(TestResultType.PASS));
this.testInstance.teardownJunit();
}

@Override
public void testFailed(ExtensionContext context, Throwable cause) {
this.testInstance.setTestResult(new JunitTestResult(TestResultType.FAIL));
this.testInstance.teardownJunit();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright 2022 (C) Cognizant SoftVision, All rights Reserved
*/

package com.cognizantsoftvision.maqs.base.junit;

import com.cognizantsoftvision.maqs.base.BaseGenericTest;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtensionContext;

class JUnitBaseGenericTestUnitTest extends BaseGenericTest {

@Test
void testCreateTestObject() {
Assertions.assertNotNull(this.getTestObject());
}
}
Loading