BAEL-7735: Add test case demonstrating TestNG SoftAssert (#16443)

* BAEL-7735: Add test case demonstrating TestNG SoftAssert

* Update package

* Move Book and BookService to separate classes
This commit is contained in:
Lucian Snare 2024-04-26 23:31:20 -04:00 committed by GitHub
parent a393395bf1
commit 267bd54092
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 82 additions and 0 deletions

View File

@ -0,0 +1,34 @@
package com.baeldung.testng.softassert;
public class Book {
private String isbn;
private String title;
private String author;
public Book() {
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}

View File

@ -0,0 +1,5 @@
package com.baeldung.testng.softassert;
public interface BookService {
Book getBook(String isbn);
}

View File

@ -0,0 +1,43 @@
package com.baeldung.testng.softassert;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;
public class SoftAssertionUnitTest {
private BookService bookService;
@BeforeMethod
void setup() {
bookService = mock(BookService.class);
Book book = new Book();
when(bookService.getBook(any()))
.thenReturn(book);
}
@Test
void givenBookWithNullFields_whenUsingMultipleAssertions_thenUseSoftAssert() {
Book gatsby = bookService.getBook("9780743273565");
// Traditional assertions - uncomment to see the test fail, only running the first assertion
// assertNotNull(gatsby.isbn, "ISBN");
// assertNotNull(gatsby.title, "title");
// assertNotNull(gatsby.author, "author");
// SoftAssert - uncomment to see the test fail, but with all assertions run
// SoftAssert softAssert = new SoftAssert();
// softAssert.assertNotNull(gatsby.isbn, "ISBN");
// softAssert.assertNotNull(gatsby.title, "title");
// softAssert.assertNotNull(gatsby.author, "author");
// softAssert.assertAll();
}
}