资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

java测试开发代码题 java 测试开发

java web 应用开发测试题?

在开发Java Web应用程序的时候,HTTP请求消息使用Get或POET方法以便在WEB上传输数据

创新互联公司是专业的昌图网站建设公司,昌图接单;提供做网站、成都网站制作,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行昌图网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

怎么写Java测试代码?

可以用main函数和JUnit来写测试代码。main是最早使用的,但是现在更流行的测试工具是JUnit。

JUnit是一个Java语言的单元测试框架。它由Kent Beck和Erich Gamma建立,逐渐成为源于Kent Beck的sUnit的xUnit家族中最为成功的一个。 JUnit有它自己的JUnit扩展生态圈。多数Java的开发环境都已经集成了JUnit作为单元测试的工具。

下面是一些具体的编写测试代码的技巧或较好的实践方法:

1. 不要用TestCase的构造函数初始化Fixture,而要用setUp()和tearDown()方法。

2. 不要依赖或假定测试运行的顺序,因为JUnit利用Vector保存测试方法。所以不同的平台会按不同的顺序从Vector中取出测试方法。

3. 避免编写有副作用的TestCase。例如:如果随后的测试依赖于某些特定的交易数据,就不要提交交易数据。简单的回滚就可以了。

4. 当继承一个测试类时,记得调用父类的setUp()和tearDown()方法。

5. 将测试代码和工作代码放在一起,一边同步编译和更新。(使用Ant中有支持junit的task.)

6. 测试类和测试方法应该有一致的命名方案。如在工作类名前加上test从而形成测试类名。

7. 确保测试与时间无关,不要依赖使用过期的数据进行测试。导致在随后的维护过程中很难重现测试。

8. 如果你编写的软件面向国际市场,编写测试时要考虑国际化的因素。不要仅用母语的Locale进行测试。

9. 尽可能地利用JUnit提供地assert/fail方法以及异常处理的方法,可以使代码更为简洁。

10.测试要尽可能地小,执行速度快。

11.不要硬性规定数据文件的路径。

12.利用Junit 的自动异常处理书写简洁的测试代码

事实上在Junit 中使用try-catch 来捕获异常是没有必要的,Junit 会自动捕获异常。那些没有被捕获的异常就被当成错误处理。

13. 充分利用Junit 的assert/fail 方法

assertSame()用来测试两个引用是否指向同一个对象

assertEquals()用来测试两个对象是否相等

14. 确保测试代码与时间无关

15. 使用文档生成器做测试文档。

JAVA实现面对对象编程,这道题怎么做?

Index.java

import java.io.BufferedWriter;

import java.io.FileWriter;

import java.io.IOException;

import java.util.HashMap;

import java.util.Iterator;

import java.util.Map;

import java.util.Scanner;

import java.util.Set;

public class Index {

// 创建全局键盘输入对象.

static Scanner sc;

public static void main(String[] args) throws IOException {

sc = new Scanner(System.in);

// 创建Map映射,键为球队编号,值为球队类.

HashMapString, Team teams = new HashMapString, Team();

// 循环命名,用于指定退出和继续.

Loop: while (true) {

// 打印选项

System.out.println("1.添加球队 2.查看球队 3.删除球员信息 4.导出球队信息 5.退出");

String option = sc.nextLine();

switch (option) {

case "1":  // 1.添加球队

addTeam(teams);

break;

case "2":  // 2.查看球队

viewTeam(teams);

break;

case "3": // 3.删除球员

deletePlayer(teams);

break;

case "4":  // 4.导出信息

exportInfo(teams);

break;

case "5":  // 5.退出系统

exit();

sc.close(); // 退出此系统之前关闭输入流.

break Loop;

default:  // 其他情况

System.out.println("错误输入,请重新输入:");

continue Loop;

}

}

}

// 根据球队编号添加队伍,添加队伍的同时在球队中添加球员对象.

public static void addTeam(MapString, Team teams) {

System.out.println("请输入球队编号:");

String teamID = sc.nextLine();

// 创建新球队对象.

Team team = new Team();

team.setTeamID(teamID);

// 把创建的球队对象加入map映射中.

teams.put(teamID, team);

System.out.println("请输入球队名字:");

String teamName = sc.nextLine();

team.setTeamName(teamName);

System.out.println("请输入球队所在城市:");

String teamCity = sc.nextLine();

team.setTeamCity(teamCity);

// 新建一个布尔变量用于判断是否继续添加.

boolean flag = true;

while (flag) {

System.out.println("请输入球员名字:");

String playerName = sc.nextLine();

System.out.println("请输入球员场均得分:");

double pointsPerGame = sc.nextDouble();

sc.nextLine();

System.out.println("请选择球员类型: 1.前锋 2.中锋 3.后卫");

// 新建一个position变量,判断添加的球员类型.

int position = sc.nextInt();

sc.nextLine();

if (position == 1) {

Forward player = new Forward();

team.setPlayers(player);

player.setPlayerPosition("前锋");

System.out.println("请输入场均篮板:");

double reboundsPerGame = sc.nextDouble();

sc.nextLine();

player.setPlayerName(playerName);

player.setPointsPerGame(pointsPerGame);

player.setReboundsPerGame(reboundsPerGame);

} else if (position == 2) {

Center player = new Center();

team.setPlayers(player);

player.setPlayerPosition("中锋");

System.out.println("请输入场均盖帽:");

double blocksPerGame = sc.nextDouble();

sc.nextLine();

player.setPlayerName(playerName);

player.setPointsPerGame(pointsPerGame);

player.setBlocksPerGame(blocksPerGame);

} else {

Guard player = new Guard();

team.setPlayers(player);

player.setPlayerPosition("后卫");

System.out.println("请输入场均助攻:");

double assistsPerGame = sc.nextDouble();

sc.nextLine();

player.setPlayerName(playerName);

player.setPointsPerGame(pointsPerGame);

player.setAssistsPerGame(assistsPerGame);

}

System.out.println("是否继续添加:(Y/N)");

String isAdd = sc.nextLine();

if (isAdd.equalsIgnoreCase("y"))

flag = true;

else

flag = false;

}

System.out.println("球队编号\t球队名称\t球队所在城市");

System.out.println(team.getTeamID() + "\t\t" + team.getTeamName() + "\t\t" + team.getTeamCity());

}

// 根据队伍,输出球员信息.

public static void viewTeam(MapString, Team teams) {

// 用keySet方法取出map映射中的键.用于迭代取Team对象.

SetString keyset = teams.keySet();

for (IteratorString itt = keyset.iterator(); itt.hasNext();) {

String tmp = itt.next();

Team team = teams.get(tmp);

System.out.println("球队编号\t球队名称\t球队所在城市");

System.out.println(team.getTeamID() + "\t\t" + team.getTeamName() + "\t\t" + team.getTeamCity());

// 得到Team对象之后,迭代其中的ArrayList,其中存储了球员对象.

System.out.println("球员名字\t场均得分\t球员类型");

for (IteratorPlayer itp = team.getPlayers().iterator(); itp.hasNext();) {

Player player = itp.next();

if (player.getPlayerPosition().equals("前锋"))

player = (Forward) player;

else if (player.getPlayerPosition().equals("中锋"))

player = (Center) player;

else

player = (Guard) player;

System.out.println(player);

}

}

}

// 先根据球队编号,再删除球员,如没有球队/员则会提示.

public static void deletePlayer(MapString, Team teams) {

System.out.println("请输入要删除的球队编号");

String teamID = sc.nextLine();

// 判断map映射中是否存在输入的球队编号,无则跳回选择界面.

if (teams.containsKey(teamID)) {

Team team = teams.get(teamID);

System.out.println("请输入球员姓名:");

String playerName = sc.nextLine();

boolean flag = false;

for (IteratorPlayer it = team.getPlayers().iterator(); it.hasNext();) {

Player tmp = it.next();

if (playerName.equals(tmp.getPlayerName())) {

flag = true;

it.remove();

System.out.println("删除成功");

break;

}

}

if (!flag)

System.out.println("删除失败,无此球员");

} else

System.out.println("无此球队,请重新选择功能.");

}

// 导出球队信息到当前目录.

public static void exportInfo(MapString, Team teams) throws IOException {

SetString keyset = teams.keySet();

// 通过判断keySet的长度判断是否输入过球队信息.

if (keyset.size() != 0) {

BufferedWriter bfw = new BufferedWriter(new FileWriter("Teams.txt"));

for (IteratorString it = keyset.iterator(); it.hasNext();) {

String tmp = it.next();

Team team = teams.get(tmp);

bfw.write("球队编号\t球队名称\t球队所在城市");

bfw.newLine();

bfw.write(team.getTeamID() + "\t\t" + team.getTeamName() + "\t\t" + team.getTeamCity());

bfw.newLine();

}

bfw.close();

} else {

System.out.println("请先输入球队信息,再导出.");

}

}

// 退出系统.

public static void exit() {

System.out.println("欢迎下次再来.");

}

}

Team.java

import java.util.ArrayList;

public class Team {

private String teamID; // 球队编号

private String teamName; // 球队名字

private String teamCity; // 球队所在城市

private ArrayListPlayer players; // 球员集合

public Team() { // 实例化Team的同时,实例化ArrayList集合.

players = new ArrayList();

}

// 各属性get和set方法

public String getTeamID() {

return teamID;

}

public void setTeamID(String teamID) {

this.teamID = teamID;

}

public String getTeamName() {

return teamName;

}

public void setTeamName(String teamName) {

this.teamName = teamName;

}

public String getTeamCity() {

return teamCity;

}

public void setTeamCity(String teamCity) {

this.teamCity = teamCity;

}

public ArrayListPlayer getPlayers() {

return players;

}

// players的set方法,直接将球员添加到ArrayList中.

public void setPlayers(Player player) {

players.add(player);

}

}

Player.java

public class Player {

protected String playerName; // 球员姓名

protected double pointsPerGame; // 场均得分

protected String playerPosition; // 球员类型

// 各个属性的get,set方法

public String getPlayerName() {

return playerName;

}

public void setPlayerName(String playerName) {

this.playerName = playerName;

}

public double getPointsPerGame() {

return pointsPerGame;

}

public void setPointsPerGame(double pointsPerGame) {

this.pointsPerGame = pointsPerGame;

}

public String getPlayerPosition() {

return playerPosition;

}

public void setPlayerPosition(String playerPosition) {

this.playerPosition = playerPosition;

}

// 重写toString方法,方便输出

public String toString() {

return playerName + "\t\t" + pointsPerGame + "\t\t" + playerPosition;

}

}

Forward.java

public class Forward extends Player {

private double reboundsPerGame; // 场均篮板

// get,set方法

public double getReboundsPerGame() {

return reboundsPerGame;

}

public void setReboundsPerGame(double reboundsPerGame) {

this.reboundsPerGame = reboundsPerGame;

}

// 重写toString方法,方便输出

public String toString() {

return playerName + "\t\t" + pointsPerGame + "\t\t" + playerPosition + "\t\t" + "场均篮板\t\t" + reboundsPerGame;

}

}

Center.java

public class Center extends Player {

private double blocksPerGame; // 场均盖帽

// get,set方法

public double getBlocksPerGame() {

return blocksPerGame;

}

public void setBlocksPerGame(double blocksPerGame) {

this.blocksPerGame = blocksPerGame;

}

// 重写toString方法,方便输出

public String toString() {

return playerName + "\t\t" + pointsPerGame + "\t\t" + playerPosition + "\t\t" + "场均盖帽\t\t" + blocksPerGame;

}

}

Guard.java

public class Guard extends Player {

private double assistsPerGame; // 场均助攻

// get,set方法

public double getAssistsPerGame() {

return assistsPerGame;

}

public void setAssistsPerGame(double assistsPerGame) {

this.assistsPerGame = assistsPerGame;

}

// 重写toString方法,方便输出

public String toString() {

return playerName + "\t\t" + pointsPerGame + "\t\t" + playerPosition + "\t\t" + "场均助攻\t\t" + assistsPerGame;

}

}

除了注释外有不懂的追问,功能都测试过可以使用,求采纳.


分享标题:java测试开发代码题 java 测试开发
标题路径:http://cdkjz.cn/article/dddjhsd.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220