2024软件测试面试刷题,这个小程序(永久刷题),靠它快速找到工作了!(刷题APP的天花板)_软件测试刷题小程序-CSDN博客文章浏览阅读2.6k次,点赞85次,收藏12次。你知不知道有这么一个软件测试面试的刷题小程序。里面包含了面试常问的软件测试基础题,web自动化测试、app自动化测试、接口测试、性能测试、自动化测试、安全测试及一些常问到的人力资源题目。最主要的是他还收集了像阿里、华为这样的大厂面试真题,还有互动交流板块……_软件测试刷题小程序https://blog.csdn.net/AI_Green/article/details/134931243?spm=1001.2014.3001.55021.使用start.spring.io创建一个“web”项目。在“依赖项”对话框中搜索并添加“web”依赖项,为了后面的契约文件,再加入“Config Client ”和“Contract Stub Runner依赖项。点击“生成”按钮,下载zip,并将其解压缩到计算机上的文件夹中。
2.pom.xml
encoding="UTF-8"?>
xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3 目录结构如下
功能:验证密码
作为银行储户
我想要在 ATM 上验证密码
以便我可以安全地进行操作
场景:验证密码成功
假如储户拥有一张卡号为"1111222233"的借记卡
并且密码为"123456"
并且储户借记卡账户余额为"100.00"元
当储户将卡插入ATM
并且储户选择查询余额
那么提示储户输入密码
并且输入密码"123456"
那么储户可以看到密码正确的提示
场景:验证密码失败
假如储户拥有一张卡号为"1111222233"的借记卡
并且密码为"123456"
并且储户借记卡账户余额为"100.00"元
当储户将卡插入ATM
并且储户选择查询余额
那么提示储户输入密码
并且输入密码"654321"
那么储户可以看到密码错误的提示
5 先来看看测试文件
MyDemoApplicationTests.java
package com.example.ATMService;
import io.cucumber.junit.platform.engine.Cucumber;
import io.cucumber.spring.CucumberContextConfiguration;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@Cucumber
@CucumberContextConfiguration
@SpringBootTest
class MyDemoApplicationTests {
@Test
void contextLoads() {
}
}
VerifyPINStepDefinitions.java
package com.example.ATMService;
//mvn spring-cloud-contract:convert;mvn spring-cloud-contract:run
//http://localhost:8080/verify_pin/1111222233/123456
import com.example.ATMService.domain.model.Account;
import com.example.ATMService.domain.model.DebitCard;
import com.example.ATMService.performer.ATM;
import com.example.ATMService.performer.Customer;
import io.cucumber.java.zh_cn.假如;
import io.cucumber.java.zh_cn.当;
import io.cucumber.java.zh_cn.那么;
import io.cucumber.junit.platform.engine.Cucumber;
import org.junit.jupiter.api.Assertions;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
@Cucumber
public class VerifyPINStepDefinitions {
private final Customer customer = new Customer();
private final ATM atm = new ATM();
@假如("储户拥有一张卡号为\"{int}\"的借记卡")
public void 储户拥有一张卡号为_的借记卡(Integer cardIdInteger){
Long cardId = cardIdInteger.longValue();
this.customer.haveCard(new DebitCard(cardId));
}
@假如("密码为\"{int}\"")
public void 密码为(Integer PIN){
this.customer.setDebitCardPIN(PIN);
}
@假如("储户借记卡账户余额为\"{double}\"元")
public void 储户借记卡账户余额为_元(Double balance){
this.customer.setCardAccount(new Account(balance));
}
@假如("ATM已经初始化")
public void ATM已经初始化(){
this.atm.init();
}
@当("储户将卡插入ATM")
public void 储户将卡插入atm(){
this.customer.insertCardToATM(atm);
}
@当("储户选择查询余额")
public void 储户选择查询余额(){
this.customer.queryBalanceOn(atm);
}
@那么("提示储户输入密码")
public void 提示储户输入密码(){
Assertions.assertEquals("Please input PIN:", this.atm.getScreenMessage());
}
@那么("输入密码\"{int}\"")
public void 输入密码(Integer pin){
this.customer.enterPIN(this.atm, pin);
}
@那么("储户可以看到密码正确的提示")
public void 储户可以看到密码正确的提示(){
Assertions.assertEquals("your PIN is invalid.", this.atm.getScreenMessage());
}
@那么("储户可以看到密码错误的提示")
public void 储户可以看到密码错误的提示(){
Assertions.assertEquals("your PIN is invalid.", this.atm.getScreenMessage());
}
}
com.example.ATMService.domain.model目录下
Account.java
package com.example.ATMService.domain.model;
public class Account {
private final Double balance;
public Account(Double balance) {
this.balance = balance;
}
public double getBalance() {
return balance;
}
}
com.example.ATMService.domain.model目录下
DebitCard.java
package com.example.ATMService.domain.model;
public class DebitCard {
private Integer PIN= -1;
private final Long cardId;
private Account account;
public DebitCard(Long cardId){
this.cardId = cardId;
}
public void setPIN(Integer pin){
this.PIN = pin.intValue();
}
public void setAccount(Account account) {
this.account = account;
}
public double getBalance() {
return this.account.getBalance();
}
public boolean verifyPIN(Integer pin) {
return this.PIN.intValue()== pin;
}
public Long getCardID() {
return this.cardId;
}
}
com.example.ATMService.domain.service目录下
DebitCardService.java
package com.example.ATMService.domain.service;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.util.Map;
@Service
public class DebitCardService {
@Value("${card-service.host}")
private String cardServiceHost;
public boolean verifyPIN(Long cardID, Integer pin){
RestTemplate template = new RestTemplate();
try{
String requestURL= String.format("http://%s/verify_pin/%d/%d",this.cardServiceHost, cardID,pin);
ResponseEntity
Map body= entity.getBody();
return "OK".equals(body.get("result"))&&(entity.getStatusCode()== HttpStatus.ACCEPTED);
}catch (Exception e) {
return false;
}
}
}
com.example.ATMService.performer目录下
ATM.java
package com.example.ATMService.performer;
import com.example.ATMService.domain.model.DebitCard;
import com.example.ATMService.domain.service.DebitCardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class ATM{
@Autowired
private DebitCardService debitCardService = new DebitCardService();
private DebitCard card;
private String screenMessage;
private boolean verifiedPIN = false;
public void init() {
this.verifiedPIN = false;
this.card = null;
this.screenMessage = null;
}
public void insertCard(DebitCard debitCard) {
this.card = debitCard;
}
public void queryBalance() {
if(this.verifiedPIN){
this.screenMessage = String.format("Your balance is: %f", this.card.getBalance());
}else {
this.screenMessage = String.format("Please input PIN:");
}
}
public String getScreenMessage() {
return this.screenMessage;
}
public void enterPlN(Integer pin) {
this.verifiedPIN = this.debitCardService.verifyPIN(this.card.getCardID(), pin);
if (!this.verifiedPIN) {
this.screenMessage ="your PIN is invalid.";
}else {
this.queryBalance();
}
}
}
com.example.ATMService.performer目录下
Customer.java
package com.example.ATMService.performer;
import com.example.ATMService.domain.model.Account;
import com.example.ATMService.domain.model.DebitCard;
public class Customer {
private DebitCard debitCard;
public void haveCard(DebitCard debitCard) {
this.debitCard = debitCard;
}
public void setDebitCardPIN(Integer pin){
this.debitCard.setPIN(pin);
}
public void setCardAccount(Account account) {
this.debitCard.setAccount(account);
}
public void insertCardToATM(ATM atm){
//atm.reset();
atm.insertCard(this.debitCard);
}
public void queryBalanceOn(ATM atm) {
atm.queryBalance();
}
public void enterPIN(ATM atm, Integer pin) {
atm.enterPlN(pin);
}
}
7 在/ATMService/src/test/resources/contracts目录下建立
verify_pin.yml
request:
method: GET
url: /verify_pin/1111222233/123456
response:
status: 200
headers:
Content-Type: application/json;charset=UTF-8
body:
result: "OK"
verify_pin_fail.yml
reguest:
method: GET
url: /verify_pin/1111222233/654321
respouse:
status: 400
headers:
Contentlype: Appliction/json;chatset=utf-8
body:
result:"Your PlN is apnalnd"
8 运行
mvn spring-cloud-contract:convert&&mvn spring-cloud-contract:run
在浏览器中输入
http://127.0.0.1:8080/verify_pin/1111222233/123456
9.建立另一个Spring Boot card
在/card/target/generated-test-sources/contracts/org/springframework/cloud/contract/verifier/tests/ContractVerifierTest.java中产生
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import io.restassured.module.mockmvc.specification.MockMvcRequestSpecification;
import io.restassured.response.ResponseOptions;
import static org.springframework.cloud.contract.verifier.assertion.SpringCloudContractAssertions.assertThat;
import static org.springframework.cloud.contract.verifier.util.ContractVerifierUtil.*;
import static com.toomuchcoding.jsonassert.JsonAssertion.assertThatJson;
import static io.restassured.module.mockmvc.RestAssuredMockMvc.*;
@SuppressWarnings("rawtypes")
public class ContractVerifierTest {
@Test
public void validate_verify_pin() throws Exception {
// given:
MockMvcRequestSpecification request = given();
// when:
ResponseOptions response = given().spec(request)
.get("/verify_pin/1111222233/123456");
// then:
assertThat(response.statusCode()).isEqualTo(200);
assertThat(response.header("Content-Type")).isEqualTo("application/json;charset=UTF-8");
// and:
DocumentContext parsedJson = JsonPath.parse(response.getBody().asString());
assertThatJson(parsedJson).field("['result']").isEqualTo("OK");
}
}
10. 实现卡片controller
package com.example.card.interfaces;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class CardController {
@GetMapping("/verify_pin/{id}/{pin}")
public ResponseEntityverifyPIN(@PathVariable("id") Long id,
@PathVariable("pin") Integer pin) {
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.set("Content-Type", "Application/json;charset-utf-8");
ResponseEntityresponse;
if(id==1111222233 && pin==123456)
response = new ResponseEntity("{\"result\":\"OK\"}", responseHeaders, HttpStatus.ACCEPTED);
else
response = new ResponseEntity("{\"result\":\"Your PIN is invalid\"}",responseHeaders,HttpStatus.BAD_REQUEST);
return response;
}
}
在卡片项目中运行
mvn spring-boot:run
启动卡片项目
然后运行amt下的mvn test测试通过
11 我的问题,如何测试在浏览器中输入http://127.0.0.1:8080/verify_pin/1111222233/123456显示pass信息,123456为其他字符显示fail信息。
行动吧,在路上总比一直观望的要好,未来的你肯定会感谢现在拼搏的自己!如果想学习提升找不到资料,没人答疑解惑时,请及时加入群: 786229024,里面有各种测试开发资料和技术可以一起交流哦。
最后: 下方这份完整的软件测试视频教程已经整理上传完成,需要的朋友们可以自行领取【保证100%免费】
软件测试面试文档
我们学习必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有字节大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。
精彩链接
大家都在找:
spring boot:springboot项目搭建
后端:后端和前端有什么区别
Java:java启动器下载
职场和发展:职场发展的路径有四种,分别是向上升级
测试用例:测试用例包含哪些内容
自动化测试:自动化测试是什么
学习:学习的励志短句
发表评论
2024-07-02 21:41:06回复