Skip to content

Commit 8598f60

Browse files
BAEL-7738 (#17963)
* init testng with spring boot * remove unnecessary comment * update unit test naming and fix formatting * update spring app name to testng-2 since this is a new module/project * update test methods naming to use given_when_then * add readme for new module * remove service package because its unecessary * update BeforeMethodTest file name and tests name as well according to baeldung guides * rename file to make the file naming clearer * update file name and tests for before test annotation unit test * remove relevant article from the readme file * remove spring boot from this module since its not needed
1 parent b4a4e95 commit 8598f60

File tree

7 files changed

+152
-0
lines changed

7 files changed

+152
-0
lines changed

testing-modules/pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
<module>testing-libraries-2</module>
6262
<module>testing-libraries-3</module>
6363
<module>testng</module>
64+
<module>testng-2</module>
6465
<module>testng-command-line</module>
6566
<module>xmlunit-2</module>
6667
<module>zerocode</module>

testing-modules/testng-2/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
### Relevant articles

testing-modules/testng-2/pom.xml

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>com.baeldung</groupId>
7+
<artifactId>testing-modules</artifactId>
8+
<version>1.0.0-SNAPSHOT</version>
9+
</parent>
10+
11+
<artifactId>testng-2</artifactId>
12+
<version>0.0.1-SNAPSHOT</version>
13+
<name>testng-2</name>
14+
<description>Demo project for Baeldung Spring Boot TestNG</description>
15+
<url/>
16+
<licenses>
17+
<license/>
18+
</licenses>
19+
<developers>
20+
<developer/>
21+
</developers>
22+
<scm>
23+
<connection/>
24+
<developerConnection/>
25+
<tag/>
26+
<url/>
27+
</scm>
28+
<properties>
29+
<java.version>17</java.version>
30+
<testng.version>7.10.2</testng.version>
31+
<slf4j.version>2.0.16</slf4j.version>
32+
</properties>
33+
<dependencies>
34+
<dependency>
35+
<groupId>org.testng</groupId>
36+
<artifactId>testng</artifactId>
37+
<version>${testng.version}</version>
38+
<scope>test</scope>
39+
</dependency>
40+
<dependency>
41+
<groupId>org.slf4j</groupId>
42+
<artifactId>slf4j-api</artifactId>
43+
<version>${slf4j.version}</version>
44+
</dependency>
45+
</dependencies>
46+
47+
<build>
48+
<plugins>
49+
</plugins>
50+
</build>
51+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.testng.utils;
2+
3+
public class Counter {
4+
private int totalCount;
5+
6+
public Counter(int totalCount) {
7+
this.totalCount = totalCount;
8+
}
9+
10+
public int getTotalCount() {
11+
return totalCount;
12+
}
13+
14+
public void addCounter(int number) {
15+
this.totalCount = totalCount + number;
16+
}
17+
18+
public void subtractCounter(int number) {
19+
this.totalCount = totalCount - number;
20+
}
21+
22+
public void resetTotalCount() {
23+
this.totalCount = 0;
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
spring.application.name=testng-2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.baeldung.testng;
2+
3+
import com.baeldung.testng.utils.Counter;
4+
import org.testng.annotations.BeforeMethod;
5+
import org.testng.annotations.BeforeTest;
6+
import org.testng.annotations.Test;
7+
import org.slf4j.Logger;
8+
import org.slf4j.LoggerFactory;
9+
10+
public class BeforeMethodAnnotationTest {
11+
private static final Logger log = LoggerFactory.getLogger(BeforeMethodAnnotationTest.class);
12+
13+
Counter counter;
14+
15+
@BeforeTest
16+
public void init() {
17+
log.info("Initializing ...");
18+
counter = new Counter(0);
19+
}
20+
21+
@BeforeMethod
22+
public void givenCounterInitialized_whenResetTotalCount_thenTotalCountValueReset() {
23+
log.info("resetting total counter value ...");
24+
counter.resetTotalCount();
25+
}
26+
27+
@Test
28+
public void givenCounterInitialized_whenAddingValue_thenTotalCountIncreased() {
29+
log.info("total counter before added: {}", counter.getTotalCount());
30+
counter.addCounter(2);
31+
log.info("total counter after added: {}", counter.getTotalCount());
32+
}
33+
34+
@Test
35+
public void givenCounterInitialized_whenSubtractingValue_thenTotalCountDecreased() {
36+
log.info("total counter before subtracted: {}", counter.getTotalCount());
37+
counter.subtractCounter(2);
38+
log.info("total counter after subtracted: {}", counter.getTotalCount());
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.testng;
2+
3+
import com.baeldung.testng.utils.Counter;
4+
import org.testng.annotations.BeforeTest;
5+
import org.testng.annotations.Test;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
9+
public class BeforeTestAnnotationTest {
10+
private static final Logger log = LoggerFactory.getLogger(BeforeTestAnnotationTest.class);
11+
12+
Counter counter;
13+
14+
@BeforeTest
15+
public void init() {
16+
log.info("Initializing ...");
17+
counter = new Counter(0);
18+
}
19+
20+
@Test
21+
public void givenCounterInitialized_whenAddingValue_thenTotalCountIncreased() {
22+
log.info("total counter before added: {}", counter.getTotalCount());
23+
counter.addCounter(2);
24+
log.info("total counter after added: {}", counter.getTotalCount());
25+
}
26+
27+
@Test
28+
public void givenCounterInitialized_whenSubtractingValue_TotalCountDecreased() {
29+
log.info("total counter before subtracted: {}", counter.getTotalCount());
30+
counter.subtractCounter(1);
31+
log.info("total counter after subtracted: {}", counter.getTotalCount());
32+
}
33+
}

0 commit comments

Comments
 (0)