我使用"Jersey测试框架"对我的webservice进行单元测试.
这是我的资源类:
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
// The Java class will be hosted at the URI path "/helloworld"
@Path("/helloworld")
public class class HelloWorldResource {
private SomeService service;
@GET
@Produces("text/plain")
public String getClichedMessage() {
// Return some cliched textual content
String responseFromSomeService = service.getSomething();
return responseFromSomeService;
}
}
Run Code Online (Sandbox Code Playgroud)
如何在单元测试中模拟SomeService?
我Junit用来测试我的球衣api.我想在没有数据库的情况下测试DAO.我尝试使用Mockito,但仍然无法使用模拟对象来测试包含Hibernate调用DB的DAO.我想写Junit一个调用DAO的Helper类.任何人都可以提供一些解决方案,其中包含一些示例代码来模拟DAO中的DB Connections.
编辑:
Status.java
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getDBValue() throws SQLException {
DatabaseConnectionDAO dbConnectiondao = new DatabaseConnectionDAO();
String dbValue = dbConnectiondao.dbConnection();
return dbValue;
}
Run Code Online (Sandbox Code Playgroud)
DatabaseConnectionDAO.java
private Connection con;
private Statement stmt;
private ResultSet rs;
private String username;
public String dbConnection() throws SQLException{
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test", "root", "root");
stmt = con.createStatement();
rs =stmt.executeQuery("select * from test");
while(rs.next()){
username = rs.getString(1);
}
}catch(Exception e){
e.printStackTrace();
}finally{
con.close();
}
return username;
}
Run Code Online (Sandbox Code Playgroud)
TestDatabase.java
@Test
public void testMockDB() …Run Code Online (Sandbox Code Playgroud) jersey ×2
java ×1
jax-rs ×1
junit ×1
mockito ×1
rest-assured ×1
unit-testing ×1
web-services ×1