Merge branch 'master' into BAEL-4783-cucumber-tags-junit5

This commit is contained in:
Loredana Crusoveanu
2021-04-21 11:33:56 +03:00
committed by GitHub
5639 changed files with 73504 additions and 45318 deletions
-3
View File
@@ -1,3 +0,0 @@
## Testing Modules
This is an aggregator module containing multiple modules focused on testing libraries.
+1
View File
@@ -5,3 +5,4 @@
- [Introduction to JUnitParams](http://www.baeldung.com/junit-params)
- [Running JUnit Tests Programmatically, from a Java Application](https://www.baeldung.com/junit-tests-run-programmatically-from-java)
- [Introduction to Lambda Behave](https://www.baeldung.com/lambda-behave)
- [Conditionally Run or Ignore Tests in JUnit 4](https://www.baeldung.com/junit-conditional-assume)
@@ -5,37 +5,59 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assume.assumeFalse;
import static org.junit.Assume.assumeThat;
import static org.junit.Assume.assumeTrue;
import static org.junit.Assume.assumeNotNull;
import static org.junit.Assume.assumeNoException;
import org.junit.Test;
public class ConditionallyIgnoreTestsUnitTest {
@Test
public void whenAssumeThatCodeVersionIsNot2_thenIgnore() {
final int codeVersion = 1;
assumeThat(codeVersion, is(2));
assertEquals("hello", "HELLO".toLowerCase());
public void whenAssumeThatAndOSIsLinux_thenRunTest() {
assumeThat(getOsName(), is("Linux"));
assertEquals("run", "RUN".toLowerCase());
}
@Test
public void whenAssumeTrueOnCondition_thenIgnore() {
final int codeVersion = 1;
assumeTrue(isCodeVersion2(codeVersion));
assertEquals("hello", "HELLO".toLowerCase());
public void whenAssumeTrueAndOSIsLinux_thenRunTest() {
assumeTrue(isExpectedOS(getOsName()));
assertEquals("run", "RUN".toLowerCase());
}
@Test
public void whenAssumeFalseOnCondition_thenIgnore() {
final int codeVersion = 2;
assumeFalse(isCodeVersion2(codeVersion));
assertEquals("hello", "HELLO".toLowerCase());
public void whenAssumeFalseAndOSIsLinux_thenIgnore() {
assumeFalse(isExpectedOS(getOsName()));
assertEquals("run", "RUN".toLowerCase());
}
private boolean isCodeVersion2(final int codeVersion) {
return codeVersion == 2;
@Test
public void whenAssumeNotNullAndNotNullOSVersion_thenRun() {
assumeNotNull(getOsName());
assertEquals("run", "RUN".toLowerCase());
}
}
/**
* Let's use a different example here.
*/
@Test
public void whenAssumeNoExceptionAndExceptionThrown_thenIgnore() {
assertEquals("everything ok", "EVERYTHING OK".toLowerCase());
String t = null;
try {
t.charAt(0);
} catch (NullPointerException npe) {
assumeNoException(npe);
}
assertEquals("run", "RUN".toLowerCase());
}
private boolean isExpectedOS(final String osName) {
return "Linux".equals(osName);
}
// This should use System.getProperty("os.name") in a real test.
private String getOsName() {
return "Linux";
}
}
-10
View File
@@ -35,18 +35,8 @@
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
</plugin>
</plugins>
</build>
<properties>
<junit-jupiter.version>5.4.2</junit-jupiter.version>
<maven-surefire-plugin.version>2.21.0</maven-surefire-plugin.version>
<junit.vintage.version>5.4.2</junit.vintage.version>
</properties>
@@ -46,7 +46,7 @@ public class TestResultLoggerExtension implements TestWatcher, AfterAllCallback
@Override
public void testFailed(ExtensionContext context, Throwable cause) {
LOG.info("Test Aborted for test {}: ", context.getDisplayName());
LOG.info("Test Failed for test {}: ", context.getDisplayName());
testResultsStatus.add(TestResultStatus.FAILED);
}
-1
View File
@@ -152,7 +152,6 @@
<junit-platform.version>1.2.0</junit-platform.version>
<junit-jupiter.version>5.4.2</junit-jupiter.version>
<spring.version>5.0.6.RELEASE</spring.version>
<maven-surefire-plugin.version>2.21.0</maven-surefire-plugin.version>
</properties>
</project>
@@ -28,8 +28,8 @@ public class BeforeAndAfterAnnotationsUnitTest {
}
@After
public void finalize() {
LOG.info("finalize");
public void teardown() {
LOG.info("teardown");
list.clear();
}
@@ -28,8 +28,8 @@ public class BeforeEachAndAfterEachAnnotationsUnitTest {
}
@AfterEach
public void finalize() {
LOG.info("finalize");
public void teardown() {
LOG.info("teardown");
list.clear();
}
+1
View File
@@ -7,3 +7,4 @@
- [Testing an Abstract Class With JUnit](https://www.baeldung.com/junit-test-abstract-class)
- [Guide to Dynamic Tests in JUnit 5](https://www.baeldung.com/junit5-dynamic-tests)
- [Determine the Execution Time of JUnit Tests](https://www.baeldung.com/junit-test-execution-time)
- [@BeforeAll and @AfterAll in Non-Static Methods](https://www.baeldung.com/java-beforeall-afterall-non-static)
-5
View File
@@ -105,10 +105,6 @@
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
@@ -140,7 +136,6 @@
<log4j2.version>2.8.2</log4j2.version>
<powermock.version>2.0.0</powermock.version>
<maven-surefire-plugin.version>2.22.0</maven-surefire-plugin.version>
<exec-maven-plugin.version>1.6.0</exec-maven-plugin.version>
<spring.version>5.0.1.RELEASE</spring.version>
<surefire.report.plugin>3.0.0-M3</surefire.report.plugin>
</properties>
@@ -0,0 +1,27 @@
package com.baeldung.junit5.nonstatic;
import org.junit.jupiter.api.*;
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class BeforeAndAfterAnnotationsUnitTest {
String input;
Long result;
@BeforeAll
public void setup() {
input = "77";
}
@AfterAll
public void teardown() {
input = null;
result = null;
}
@Test
public void whenConvertStringToLong_thenResultShouldBeLong() {
result = Long.valueOf(input);
Assertions.assertEquals(77l, result);
}
}
+2 -2
View File
@@ -55,8 +55,8 @@
</dependencies>
<properties>
<junit.jupiter.version>5.6.2</junit.jupiter.version>
<junit.platform.version>1.6.0</junit.platform.version>
<junit.jupiter.version>5.7.0</junit.jupiter.version>
<junit.platform.version>1.7.0</junit.platform.version>
<log4j2.version>2.8.2</log4j2.version>
<assertj-core.version>3.11.1</assertj-core.version>
</properties>
+3 -32
View File
@@ -18,12 +18,12 @@
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-engine</artifactId>
<version>${junit.platform.version}</version>
<version>${junit-platform.version}</version>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>${junit.platform.version}</version>
<version>${junit-platform.version}</version>
<scope>test</scope>
</dependency>
<dependency>
@@ -47,42 +47,13 @@
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>${junit.platform.version}</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>${exec-maven-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.baeldung.TestLauncher</mainClass>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<junit.jupiter.version>5.2.0</junit.jupiter.version>
<junit.platform.version>1.2.0</junit.platform.version>
<junit-platform.version>1.2.0</junit-platform.version>
<junit.vintage.version>5.2.0</junit.vintage.version>
<maven-surefire-plugin.version>2.21.0</maven-surefire-plugin.version>
<exec-maven-plugin.version>1.6.0</exec-maven-plugin.version>
</properties>
</project>
@@ -37,7 +37,6 @@
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
@@ -55,11 +54,6 @@
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
@@ -77,7 +71,6 @@
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.0.5.RELEASE</version>
</plugin>
<!--<plugin>-->
<!--<groupId>net.alchim31.maven</groupId>-->
@@ -122,10 +115,10 @@
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<encoding>UTF-8</encoding>
<scala.version>2.11.12</scala.version> <!--2.11.12 --> <!--2.12.6 -->
<gatling.version>2.2.5</gatling.version> <!--2.2.5 --> <!--2.3.1 -->
<scala-maven-plugin.version>3.2.2</scala-maven-plugin.version> <!--3.2.2 --> <!--3.3.2 -->
<gatling-maven-plugin.version>2.2.1</gatling-maven-plugin.version> <!--2.2.1 --> <!--2.2.4 -->
<scala.version>2.12.12</scala.version> <!--2.11.12 --> <!--2.12.6 -->
<gatling.version>3.4.0</gatling.version> <!--2.2.5 --> <!--2.3.1 -->
<scala-maven-plugin.version>4.4.0</scala-maven-plugin.version> <!--3.2.2 --> <!--3.3.2 -->
<gatling-maven-plugin.version>3.1.0</gatling-maven-plugin.version> <!--2.2.1 --> <!--2.2.4 -->
<jmeter.version>5.0</jmeter.version>
</properties>
@@ -1,26 +0,0 @@
package com.baeldung.loadtesting;
import com.baeldung.loadtesting.model.Transaction;
import com.baeldung.loadtesting.repository.TransactionRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@Deprecated
public class TransactionController {
@Autowired
private TransactionRepository transactionRepository;
@PostMapping(path="/addTransaction")
public @ResponseBody
String saveTransactions(@RequestBody Transaction trnsctn){
transactionRepository.save(trnsctn);
return "Saved Transaction.";
}
@GetMapping(path="/findAll/{rewardId}")
public @ResponseBody Iterable<Transaction> getTransactions(@RequestParam Integer id){
return transactionRepository.findByCustomerRewardsId(id);
}
}
@@ -4,11 +4,11 @@ import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import lombok.Data;
import javax.persistence.Index;
import javax.persistence.Table;
@Entity
@Data
@Table(indexes = {@Index(columnList="customerId")})
public class CustomerRewardsAccount {
@Id
@@ -19,4 +19,18 @@ public class CustomerRewardsAccount {
public Integer getCustomerId(){
return this.customerId;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public void setCustomerId(Integer customerId) {
this.customerId = customerId;
}
}
@@ -1,16 +1,17 @@
package com.baeldung.loadtesting.model;
import lombok.Data;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Index;
import javax.persistence.Table;
import java.util.Date;
import java.util.Calendar;
@Entity
@Data
@Table(indexes = {@Index(columnList="customerRewardsId")})
public class Transaction {
@Id
@@ -27,4 +28,34 @@ public class Transaction {
public void setTransactionDate(Date transactionDate){
this.transactionDate = transactionDate;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getCustomerRewardsId() {
return customerRewardsId;
}
public void setCustomerRewardsId(Integer customerRewardsId) {
this.customerRewardsId = customerRewardsId;
}
public Integer getCustomerId() {
return customerId;
}
public void setCustomerId(Integer customerId) {
this.customerId = customerId;
}
public Date getTransactionDate() {
return transactionDate;
}
}
@@ -0,0 +1,7 @@
spring.h2.console.enabled=true
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
@@ -7,46 +7,41 @@ import scala.concurrent.duration._
class RewardsScenario extends Simulation {
def randCustId() = Random.nextInt(99)
def randCustId() = java.util.concurrent.ThreadLocalRandom.current().nextInt()
val httpProtocol = http.baseUrl("http://localhost:8080")
.acceptHeader("text/html,application/json;q=0.9,*/*;q=0.8")
.doNotTrackHeader("1")
.acceptLanguageHeader("en-US,en;q=0.5")
.acceptEncodingHeader("gzip, deflate")
.userAgentHeader("Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0")
val scn = scenario("RewardsScenario")
.repeat(10){
.repeat(1000){
exec(http("transactions_add")
.post("/transactions/add/")
.body(StringBody("""{ "customerRewardsId":null,"customerId":""""+ randCustId() + """","transactionDate":null }""")).asJson
.body(StringBody(_ => s"""{"customerRewardsId":null,"customerId":${randCustId()},"transactionDate":null}""")).asJson
.check(jsonPath("$.id").saveAs("txnId"))
.check(jsonPath("$.transactionDate").saveAs("txtDate"))
.check(jsonPath("$.customerId").saveAs("custId")))
.pause(1)
.exec(http("get_reward")
.get("/rewards/find/${custId}")
.check(jsonPath("$.id").saveAs("rwdId")))
.pause(1)
.check(jsonPath("$.id").optional.saveAs("rwdId")))
.doIf("${rwdId.isUndefined()}"){
exec(http("rewards_add")
.post("/rewards/add")
.body(StringBody("""{ "customerId": "${custId}" }""")).asJson
.body(StringBody("""{"customerId":${custId}}""")).asJson
.check(jsonPath("$.id").saveAs("rwdId")))
}
.exec(http("transactions_add")
.exec(http("transactions_update")
.post("/transactions/add/")
.body(StringBody("""{ "customerRewardsId":"${rwdId}","customerId":"${custId}","transactionDate":"${txtDate}" }""")).asJson)
.pause(1)
.body(StringBody("""{"customerRewardsId":${rwdId},"customerId":${custId},"transactionDate":"${txtDate}" }""")).asJson)
.exec(http("get_reward")
.exec(http("get_transactions")
.get("/transactions/findAll/${rwdId}"))
.exec(_.removeAll("txnId", "txtDate", "custId", "rwdId"))
}
setUp(
scn.inject(atOnceUsers(100))
).protocols(httpProtocol)
}
}
@@ -16,7 +16,7 @@
<stringProp name="ThreadGroup.on_sample_error">continue</stringProp>
<elementProp name="ThreadGroup.main_controller" elementType="LoopController" guiclass="LoopControlPanel" testclass="LoopController" testname="Loop Controller" enabled="true">
<boolProp name="LoopController.continue_forever">false</boolProp>
<stringProp name="LoopController.loops">10</stringProp>
<stringProp name="LoopController.loops">1000</stringProp>
</elementProp>
<stringProp name="ThreadGroup.num_threads">100</stringProp>
<stringProp name="ThreadGroup.ramp_time">0</stringProp>
@@ -200,7 +200,7 @@
</HTTPSamplerProxy>
<hashTree/>
<RandomVariableConfig guiclass="TestBeanGUI" testclass="RandomVariableConfig" testname="Random Variable" enabled="true">
<stringProp name="maximumValue">10000</stringProp>
<stringProp name="maximumValue">9223372036854775806</stringProp>
<stringProp name="minimumValue">1</stringProp>
<stringProp name="outputFormat"></stringProp>
<boolProp name="perThread">false</boolProp>
@@ -1,5 +1,5 @@
grinder.script = grinder.py
grinder.threads = 100
grinder.processes = 1
grinder.runs = 10
grinder.runs = 1000
grinder.logDirectory = /logs
@@ -24,6 +24,7 @@ random=java.util.Random()
class TestRunner:
def __call__(self):
customerId = str(random.nextInt());
result = request1.POST("http://localhost:8080/transactions/add", "{"'"customerRewardsId"'":null,"'"customerId"'":"+ customerId + ","'"transactionDate"'":null}")
@@ -37,4 +38,5 @@ class TestRunner:
rwdId = parseJsonString(result.getText(), "id")
result = request1.POST("http://localhost:8080/transactions/add", "{"'"id"'":" + txnId + ","'"customerRewardsId"'":" + rwdId + ","'"customerId"'":"+ customerId + ","'"transactionDate"'":null}")
result = request1.GET("http://localhost:8080/transactions/findAll/" + rwdId)
result = request1.GET("http://localhost:8080/transactions/findAll/" + rwdId)
+5 -1
View File
@@ -5,4 +5,8 @@
- [Mockito Strict Stubbing and The UnnecessaryStubbingException](https://www.baeldung.com/mockito-unnecessary-stubbing-exception)
- [Mockito and Fluent APIs](https://www.baeldung.com/mockito-fluent-apis)
- [Mocking the ObjectMapper readValue() Method](https://www.baeldung.com/mockito-mock-jackson-read-value)
- [Introduction to Mockito’s AdditionalAnswers](https://www.baeldung.com/mockito-additionalanswers)
- [Introduction to Mockito’s AdditionalAnswers](https://www.baeldung.com/mockito-additionalanswers)
- [Mockito – Using Spies](https://www.baeldung.com/mockito-spy)
- [Using Mockito ArgumentCaptor](https://www.baeldung.com/mockito-argumentcaptor)
- [Difference Between when() and doXxx() Methods in Mockito](https://www.baeldung.com/java-mockito-when-vs-do)
- [Overview of Mockito MockSettings](https://www.baeldung.com/mockito-mocksettings)
-1
View File
@@ -30,7 +30,6 @@
<properties>
<mockito.version>2.21.0</mockito.version>
<jackson.version>2.10.3</jackson.version>
</properties>
</project>
@@ -0,0 +1,7 @@
package com.baeldung.mockito.argumentcaptor;
public enum AuthenticationStatus {
AUTHENTICATED,
NOT_AUTHENTICATED,
ERROR
}
@@ -0,0 +1,14 @@
package com.baeldung.mockito.argumentcaptor;
public class Credentials {
private final String name;
private final String password;
private final String key;
public Credentials(String name, String password, String key) {
this.name = name;
this.password = password;
this.key = key;
}
}
@@ -0,0 +1,10 @@
package com.baeldung.mockito.argumentcaptor;
public interface DeliveryPlatform {
void deliver(Email email);
String getServiceStatus();
AuthenticationStatus authenticate(Credentials credentials);
}
@@ -0,0 +1,47 @@
package com.baeldung.mockito.argumentcaptor;
public class Email {
private String address;
private String subject;
private String body;
private Format format;
public Email(String address, String subject, String body) {
this.address = address;
this.subject = subject;
this.body = body;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public Format getFormat() {
return format;
}
public void setFormat(Format format) {
this.format = format;
}
}
@@ -0,0 +1,36 @@
package com.baeldung.mockito.argumentcaptor;
public class EmailService {
private DeliveryPlatform platform;
public EmailService(DeliveryPlatform platform) {
this.platform = platform;
}
public void send(String to, String subject, String body, boolean html) {
Format format = Format.TEXT_ONLY;
if (html) {
format = Format.HTML;
}
Email email = new Email(to, subject, body);
email.setFormat(format);
platform.deliver(email);
}
public ServiceStatus checkServiceStatus() {
if (platform.getServiceStatus().equals("OK")) {
return ServiceStatus.UP;
} else {
return ServiceStatus.DOWN;
}
}
public boolean authenticatedSuccessfully(Credentials credentials) {
if (platform.authenticate(credentials).equals(AuthenticationStatus.AUTHENTICATED)) {
return true;
} else {
return false;
}
}
}
@@ -0,0 +1,6 @@
package com.baeldung.mockito.argumentcaptor;
public enum Format {
TEXT_ONLY,
HTML
}

Some files were not shown because too many files have changed in this diff Show More