
正文
java用例代码 java用例图详解
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
java spring项目的controller层的代码怎么用junit写单元测试用例
springboot与Junit有整合的方式,你可以模拟http请求从你的测试类发送请求到Controller,就像中描述的一样。下面我列出部分代码:
package com.youyanpai;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class SimpleTest {
private MockMvc mockMvc;
@Autowired
private WebApplicationContext webApplicationContext;
/**
* 前置处理
* @author 有言派
* @author
*/
@Before
public void before() throws Exception {
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}
/**
* 测试方法
* @author 有言派
* @author
*/
@Test
public void testYourMethod() throws Exception {
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/login")
.param("username", "youyanpai.com")
.param("password", "youyanpai.com")
.session(session))
.andReturn();
int status = mvcResult.getResponse().getStatus();
String responseString = mvcResult.getResponse().getContentAsString(); //请求是否正确
Assert.assertEquals("请求错误", 200, status);
//输出返回值
System.out.println("有言派提示您,测试返回结果:"+responseString);
}
}
希望能够帮到你!
相关问答
Q1: 谁能给一个Java程序代码我,要50行到100行就可以啦。最好有几行解释
给你一个前几天才帮人写的
“计算整钱兑零”。程序要求用户输入一个双精度数代表总元数java用例代码,就会列出总值与其等价的1元币、二角五分币、5分币和1分币的数目。程序报告的数目是1元币的最大数、其次是二角五分币的最大数java用例代码,等等java用例代码,依此类推。只显示非零的单位。对单个单位显示单数单词java用例代码,对多于一个单位的显示复数单词
import java.util.Scanner;
public class MoneyCalculate {
public static void main(String[] args) {
int max100 = 0;
int max25 = 0;
int max5 = 0;
int max1 = 0;
double money = getMoneyFromInput();
String str = String.valueOf(money).trim();
String[] ary = str.split("\\.");
max100 = Integer.parseInt(ary[0]);
if(ary.length == 2){
int fen = Integer.parseInt(ary[1]);
if(ary[1].trim().length() == 1){
fen = Integer.parseInt(ary[1]) * 10;
}
max25 = fen / 25;
if(fen % 25 != 0){
fen = fen % 25;
}else{
fen = 0;
}
max5 = fen / 5;
max1 = fen % 5;
}
StringBuilder sb = new StringBuilder(money + " = ");
if(max100 != 0){
sb.append(max100);
sb.append("*1 ");
}
if(max25 != 0){
sb.append(max25);
sb.append("*0.25 ");
}
if(max5 != 0){
sb.append(max5);
sb.append("*0.05 ");
}
if(max1 != 0){
sb.append(max1);
sb.append("*0.01 ");
}
System.out.println(sb.toString());
}
private static double getMoneyFromInput() {
Scanner scanner = new Scanner(System.in);
return scanner.nextDouble();
}
}
-----------
2.49
2.49 = 2*1 1*0.25 4*0.05 4*0.01
-----------
2.5
2.5 = 2*1 2*0.25
-----------
37.23
37.23 = 37*1 4*0.05 3*0.01
-----------------
123.569
123.569 = 123*1 22*0.25 3*0.05 4*0.01
Q2: selenium 怎么使用java编写测试用例
control-1.0.1-dist.zip。 解压。
2. 用命令行来到解压的文件夹下: \selenium-remote-control-0.9.2\selenium-server-0.9.2
3. 运行: java -jar selenium-server.jar 启动selenium server (务必启动!!)
4. 在Eclipse创建一个项目,在项目的build path里面加上junit.jar和selenium-java-client-driver.jar(这个在刚解压的包里面)
5. 先利用firefox selenium IDE来录制检测页面检测功能用的junit代码。
6. 在项目里面新建一个class(junit用例):将上面的junit代码帖于此。
7. 根据eclipse的错误提示来增加相应要import的类
8. 在进行测试前,最好将对应浏览器关闭,否则容易出错。
9. 然后在Eclipse里运行 “Run As - unit Test”即可看到自动化的范例.
10.运行期间,会弹出ie窗口,自动进 行操作测试。检测完后,若junit显示为“绿色”则表示成功。
下面粘贴一下那个测试小程序
import com.thoughtworks.selenium.SeleneseTestCase;public class Untitled extends SeleneseTestCase {
public void setUp() throws Exception {
//由于selenium 对*firefox不支持3.6版本的.只能支持3.0版本.所以,最好将selenium IDE录制的代码中的firefox改为ie进行测试。
//setUp("", "*firefox");
setUp("", "*iexplore");
}
public void testUntitled() throws Exception {
selenium.open("/");
selenium.type("q", "baidu");
selenium.click("btnG");
selenium.waitForPageToLoad("30000");
selenium.click("link= 百度一下,你就知道");
//添加断言进行测试:
// assertTrue(selenium.isTextPresent("OpenQA: Selenium")); //测试出错,程序退出
assertTrue(selenium.isTextPresent("百度一 下,你就知道")); //测试成功,程序继续
}
//用于让测试的页面关闭.若不写,则页面不会关闭
public void tearDown() throws Exception {
selenium.stop();
}
}
(7)
7.1
selenium 常用操作有:open,type,click,select,selectFrame:
1. open("/")打开的是当前的网址;selenium.open("/dmmc/"):在当前的网址后面追回/dmmc/;
2. type,click,select,selectFrame各方法使用时,对元素的定位都可采用元素ID 或 xpath方式;
3. type,click,select,selectFrame去选择元素时,可以直接用元素的ID作为标 记.
4. 如:selenium.type("loginName", "coship");;采用xpath方式时,则格式如://元素名1[元素属性名1='元素属性值1']/元素名2[元素属性名2='元素 属 性值2']/....
如:selenium.type("//input[@name='admin.password']", "coship")7.2
常用命令用法:
1)
type的两种不同定位方式:
selenium.type("loginName", "coship");
//以下语句的"xpath="可以省略
selenium.type("xpath=//input[@name='admin.password']", "coship");
2)
click的两种不同定位方式:
selenium.click("imageField"); 即是通过ID定位:input type="submit" value=" " id="imageField"
selenium.click("//input[@type='submit']"); (通过属性input-type)
selenium.click("//input[@value='确定']"); (通过属性input-value)
selenium.click("//input[@name='devTypeIds' and @value='000002']") (还可通过属性@id)
3)
点击链接方式:
对于动态内容的获取,尽量避 免采用第一种方式(若内容变了,则出错),而采用第二种方式.
实现方式一:
点击链接:a href=..801830456628/a
selenium.click("link=801830456628");
实现方式二:
获取id=adminList的table中的tbody下的第三行,第二列中的a href元素。
selenium.click("//table[@id='adminsList']/tbody/tr[3]/td[2]/a");
4)
选 择下拉框:
实现方式一:
selenium.select("status", "label=启用");
即 是:select id="status"option value="1"启用/option/select
实现方式二:
selenium.select("xpath=//SELECT[@id='status']", "index=1");
具体应用,请见以下实例。7.3
实例:
用于检测abmc系统各模块功能是否正常。
方式:
用selenium IDE录制abmc系统各模块功能操作.(前提是:这些操作,这些功能都是正确成功),以后当abmc系统升级,更改后,即可运行此脚本,来检查升级是否 影响系统功能实现。若系统更改有错,则selenium中运行中某一步骤时,会出错退出。
如:
系统更改后导致某一页面打不开,这时 selenium运行到此页面时,就不能继续往下操作,就会出错而退出。注意:
1.同时,也可在测试代码中添加一些断言判断来判断成功,失败。
2.
对于firefox selenium IDE录制的脚本要进行适当的修改,尽量让selenium用元素ID来定位操作元素,而不通过元素名(元素名易变化)。
3.
若selenium RC检测代码出错,也不一定是系统升级有问题,可能是系统升级后,有些数据删除,修改了,selenium RC在回放操作时,找到原来录制时对应的数据而出错。具体代码如下:
//对于click,select,selectFrame去选择元素时,可以直接用元素的ID作为标记.// 如:selenium.click("元素ID");public class AbmcSeleniumTest extends SeleneseTestCase {
public void setUp() throws Exception {
setUp("", "*iexplore");
}
public void testUntitled() throws Exception {
selenium.open("/abmc/");
//type的两种不同定位方式
selenium.type("loginName", "coship");
//以下语句 的"xpath="可以省略
selenium.type("xpath=//input[@name='admin.password']", "coship");
// selenium.click("imageField"); 即是通过ID 定位:input type="submit" value=" " id="imageField"
selenium.click("//input[@type='submit']");
//等待一个新的页面加载。 以毫秒为单位,超过后该命令将返回错误。
selenium.waitForPageToLoad("30000");
//即选择frame src="device/index.jsp" id="mainFrame"
selenium.selectFrame("mainFrame");
//对于动态内容的获取,尽量避免采用第一种方式 (若内容变了,则出错),而采用第二种方式
//点击链接:a href=..801830456628/a
// selenium.click("link=801830456628");
//实现方式二:获取id=adminList的table中的tbody下的第三行,第二列中的a href元素。
selenium.click("//table[@id='adminsList']/tbody/tr[3]/td[2]/a");
selenium.waitForPageToLoad("30000");
selenium.click("//input[@value=' 返回']");
selenium.waitForPageToLoad("30000");
//因为有多个“查看应用列表”,若不指定,默认获取第一个
selenium.click("link=查看应用列表");
selenium.click("btn_dsure");
// 方式一:
//selenium.click(" //a[@onclick=\"showPage('应用列表','deviceAppList.action?device.swType=2device.deviceId=0000257device.deviceName=801830456628device.specName=DevTyp',750,400)\"]");
//方式二:
selenium.click("//table[@id='adminsList']/tbody/tr[3]/td[5]/span[1]/a");
selenium.click("btn_dsure");
selenium.selectFrame("relative=up");
selenium.selectFrame("leftFrame");
selenium.click("link=应用文件管理");
selenium.click("link=应用文件信息");
selenium.selectFrame("relative=up");
selenium.selectFrame("mainFrame");
selenium.click("//a[@onclick=\"showPage('匹配终端类型','appTypeList.action?application.appId=01application.appName=maliao',750,400)\"]");
selenium.click("btn_dsure");
selenium.click("//table[@id='adminsList']/tbody/tr[7]/td[8]/span[2]/a");
selenium.waitForPageToLoad("30000");
selenium.click("//input[@name='devTypeIds' and @value='000002']");
selenium.click("//input[@value='确定']");
selenium.waitForPageToLoad("30000");
selenium.click("//a[@onclick=\"showPage('匹配终端类型','appTypeList.action?application.appId=01application.appName=maliao',750,400)\"]");
selenium.click("btn_dsure");
selenium.selectFrame("relative=up");
selenium.selectFrame("leftFrame");
selenium.click("link=终端应用管理");
selenium.click("link=终端应用许可");
selenium.selectFrame("relative=up");
selenium.selectFrame("mainFrame");
// selenium.select("status", "label=启用"); 即是:select id="status"option value="1"启 用/option/select
selenium.select("xpath=//SELECT[@id='status']", "index=1");
selenium.click("//input[@type='image']");
selenium.waitForPageToLoad("30000");
selenium.click("//input[@type='image']");
selenium.waitForPageToLoad("30000");
selenium.selectFrame("relative=up");
//即 选择frame src="device/index.jsp" id="mainFrame"
selenium.selectFrame("topFrame");
selenium.click("link=注销");
//若要测试其 它的网页,可以继续selenium.open(..)
}
}
#web测试技术
关于java用例代码和java用例图详解的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。








