[英]Unit-testing a rest-API written in xml
我是Web開發和Java的新手。 我已經使用xml開發了該rest-api,並且運行良好(NetBeans)。 但是,我需要對其執行單元測試。 有人可以指導我嗎?
提前致謝
@Path("/users")
public class users{
String host=HelperClass.host;
String uName=HelperClass.uName;
String pass=HelperClass.pass;
Connection con;
/**
* Gets all the users in the database with the password.
* @return
* @throws SQLException
*/
@GET
@Path("/getusers")
@Produces(MediaType.APPLICATION_XML)
public UserList getAllUsers() throws SQLException {
UserList users;
try
{
con=DriverManager.getConnection(host, uName, pass);
Statement stmt=con.createStatement();
String Sql="SELECT * FROM USERS";
ResultSet rs=stmt.executeQuery(Sql);
users=new UserList();
users.setUsers(new ArrayList<Users>());
while(rs.next())
{
Users newuser=new Users();
newuser.setUsername(rs.getString("username"));
newuser.setPassword(rs.getString("password"));
users.getUsers().add(newuser);
}
}
catch(Exception ex)
{
users=new UserList();
}
finally
{
con.close();
}
return users;
}
/**
* Gets the specific user in the database.
* @param userId
* @return
* @throws SQLException
*/
@GET
@Path("/getuser/{userId}")
@Produces(MediaType.APPLICATION_XML)
public Users getUser(@PathParam("userId") String userId) throws SQLException
{
Users user;
try
{
con=DriverManager.getConnection(host, uName, pass);
Statement stmt=con.createStatement();
String Sql="SELECT * FROM USERS where USERNAME='"+userId+"'";
ResultSet rs=stmt.executeQuery(Sql);
user=new Users();
while(rs.next())
{
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
}
}
catch(Exception ex)
{
user=new Users();
}
finally
{
con.close();
}
return user;
}
@POST
@Path("/setuser")
@Consumes({MediaType.APPLICATION_XML})
@Produces({MediaType.TEXT_PLAIN})
public String setUser(Users user) throws SQLException
{
try
{
con=DriverManager.getConnection(host, uName, pass);
Statement stmt=con.createStatement();
String Sql="INSERT INTO USERS VALUES('"+user.getUsername()+"','"+user.getPassword()+"')";
stmt.executeUpdate(Sql);
}
catch(Exception ex)
{
return "error";
}
finally
{
con.close();
}
return "done";
}
@DELETE
@Path("/deleteuser/{userId}")
@Produces({MediaType.TEXT_PLAIN})
public String deleteUser(@PathParam("userId") String userId) throws SQLException
{
try
{
con=DriverManager.getConnection(host, uName, pass);
Statement stmt=con.createStatement();
String Sql="Delete from replies where REPLY_ID='"+userId+"'";
stmt.executeUpdate(Sql);
Sql="Delete from comments where USER_ID='"+userId+"'";
stmt.executeUpdate(Sql);
Sql="Delete from USERINFO where USERINFO_ID='"+userId+"'";
stmt.executeUpdate(Sql);
Sql="Delete from USERS where USERNAME='"+userId+"'";
stmt.executeUpdate(Sql);
}
catch(Exception ex)
{
return "error";
}
finally
{
con.close();
}
return "done";
}
}
有兩種方法可以測試您的其余API:
1>直接調用Junit中的方法並驗證結果
2>使用http GET / POST方法調用您的API並驗證響應。
要使用http get方法調用API,可以參考以下示例:
String url = "http://www.google.com/search?q=httpClient";
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(url);
// add request header
request.addHeader("User-Agent", USER_AGENT);
HttpResponse response = client.execute(request);
System.out.println("Response Code : "
+ response.getStatusLine().getStatusCode());
BufferedReader rd = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
有關其他示例,請訪問http://www.mkyong.com/java/apache-httpclient-examples/
謝謝。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.