<dependency> <groupId>org.scalatest</groupId> <artifactId>scalatest_2.13</artifactId> <version>3.2.9</version> <scope>test</scope> </dependency> import org.scalatest.BeforeAndAfterEach; import org.scalatest.FeatureSpec; import org.scalatest.GivenWhenThen; import org.junit.runner.RunWith; import org.scalatest.junit.JUnitRunner; import java.util.NoSuchElementException; @RunWith(JUnitRunner.class) public class LinkedListSpec extends FeatureSpec with GivenWhenThen with BeforeAndAfterEach { private LinkedList<String> linkedList; @Override public void beforeEach() { linkedList = new LinkedList<>(); } feature("LinkedList add and get methods") { scenario("Adding elements to the list") { Given("an empty linked list") assert(linkedList.size() == 0); When("adding an element to the list") linkedList.add("element"); Then("the size of the list should increase") assert(linkedList.size() == 1); And("the added element should be the first element") assert(linkedList.get(0).equals("element")); } scenario("Getting elements from the list") { Given("a linked list with elements") linkedList.add("element1"); linkedList.add("element2"); linkedList.add("element3"); When("getting an element from the list") String element = linkedList.get(1); Then("the retrieved element should match the expected element") assert(element.equals("element2")); } // More scenarios for other methods can be added here } } <build> <plugins> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.12.4</version> <configuration> <includes> <include>**/*Spec.class</include> </includes> </configuration> </plugin> </plugins> </build>


上一篇:
下一篇:
切换中文