Bael 7317 (#15415)
* BAEL-7226: Code for the improvements of the Percentages article. * BAEL-7226: Rename Test to follow the convention * BAEL-7317: Update the code to the latest versions of Spock, Groovy and the maven matching configuration * BAEL-7317: Add code for the article improvements
This commit is contained in:
parent
f471384958
commit
164ef67325
|
@ -15,6 +15,12 @@
|
|||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<spock-core.version>2.4-M1-groovy-4.0</spock-core.version>
|
||||
<groovy-all.version>4.0.16</groovy-all.version>
|
||||
<gmavenplus-plugin.version>3.0.2</gmavenplus-plugin.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.spockframework</groupId>
|
||||
|
@ -23,9 +29,10 @@
|
|||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.codehaus.groovy</groupId>
|
||||
<groupId>org.apache.groovy</groupId>
|
||||
<artifactId>groovy-all</artifactId>
|
||||
<version>${groovy-all.version}</version>
|
||||
<type>pom</type>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
@ -39,18 +46,34 @@
|
|||
<execution>
|
||||
<goals>
|
||||
<goal>compile</goal>
|
||||
<goal>testCompile</goal>
|
||||
<goal>compileTests</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<testSources>
|
||||
<testSource>
|
||||
<directory>${project.basedir}/src/test/groovy</directory>
|
||||
<includes>
|
||||
<include>**/*.groovy</include>
|
||||
</includes>
|
||||
</testSource>
|
||||
</testSources>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>3.2.2</version>
|
||||
<configuration>
|
||||
<testSourceDirectory>src/test/groovy</testSourceDirectory>
|
||||
<includes>
|
||||
<include>**/*Specification.groovy</include>
|
||||
<include>**/*Test.groovy</include>
|
||||
</includes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<spock-core.version>1.3-groovy-2.4</spock-core.version>
|
||||
<groovy-all.version>2.4.7</groovy-all.version>
|
||||
<gmavenplus-plugin.version>1.5</gmavenplus-plugin.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,11 @@
|
|||
package mocks;
|
||||
|
||||
public class Book {
|
||||
private String title;
|
||||
private String author;
|
||||
|
||||
Book(String title, String author) {
|
||||
this.title = title;
|
||||
this.author = author;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
package mocks;
|
||||
|
||||
public interface BookRepository {
|
||||
Book findById(Long id);
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package mocks;
|
||||
|
||||
public class BookService {
|
||||
private final BookRepository repository;
|
||||
|
||||
public BookService(BookRepository repository) {
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
public Book getBookDetails(Long id) {
|
||||
return repository.findById(id);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package mocks;
|
||||
|
||||
public class MessageService {
|
||||
public String fetchMessage() {
|
||||
return UtilityClass.getMessage();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package mocks;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class ShoppingCart {
|
||||
private final Map<String, Integer> items = new HashMap<>();
|
||||
private double totalPrice = 0.0;
|
||||
|
||||
public void addItem(String item, int quantity) {
|
||||
items.put(item, items.getOrDefault(item, 0) + quantity);
|
||||
totalPrice += quantity * 2.00; // Example pricing
|
||||
}
|
||||
|
||||
public int getTotalItems() {
|
||||
return items.values().stream().mapToInt(Integer::intValue).sum();
|
||||
}
|
||||
|
||||
public double getTotalPrice() {
|
||||
return totalPrice;
|
||||
}
|
||||
|
||||
public boolean containsItem(String item) {
|
||||
return items.containsKey(item);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package mocks;
|
||||
|
||||
public class UtilityClass {
|
||||
public static String getMessage() {
|
||||
return "Original Message";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package extensions
|
||||
|
||||
import mocks.ShoppingCart
|
||||
import spock.lang.Specification
|
||||
|
||||
class ShoppingCartTest extends Specification {
|
||||
def "verify multiple properties of a ShoppingCart"() {
|
||||
given:
|
||||
ShoppingCart cart = new ShoppingCart()
|
||||
cart.addItem("Apple", 3)
|
||||
cart.addItem("Banana", 2)
|
||||
|
||||
expect:
|
||||
with(cart) {
|
||||
totalItems == 5
|
||||
totalPrice == 10.00
|
||||
items.contains("Apple")
|
||||
items.contains("Banana")
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package mocks
|
||||
|
||||
import spock.lang.Specification
|
||||
|
||||
|
||||
class BookServiceTest extends Specification {
|
||||
def "should retrieve book details and verify method calls"() {
|
||||
given:
|
||||
def bookRepository = Mock(BookRepository) {
|
||||
findById(1L) >> new Book("Effective Java", "Joshua Bloch")
|
||||
findById(2L) >> null
|
||||
}
|
||||
def bookService = new BookService(bookRepository)
|
||||
|
||||
when:
|
||||
Book effectiveJava = bookService.getBookDetails(1L)
|
||||
Book unknownBook = bookService.getBookDetails(2L)
|
||||
|
||||
then:
|
||||
1 * bookRepository.findById(1L)
|
||||
1 * bookRepository.findById(2L)
|
||||
effectiveJava.title == "Effective Java"
|
||||
unknownBook == null
|
||||
}
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package mocks
|
||||
|
||||
import spock.lang.Specification
|
||||
|
||||
|
||||
class MessageServiceTest extends Specification {
|
||||
def "should use global mock for UtilityClass"() {
|
||||
given:
|
||||
def utilityMock = GroovySpy(UtilityClass, global: true)
|
||||
utilityMock.getMessage() >> "Mocked Message"
|
||||
|
||||
when:
|
||||
MessageService service = new MessageService()
|
||||
String message = service.fetchMessage()
|
||||
|
||||
then:
|
||||
1 * utilityMock.getMessage()
|
||||
message == "Mocked Message"
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue