修复错误:无法解析com.mockobjects.MockObject类型。它是从所需的.class文件间接引用的

时间:2014-06-27 15:53:40

标签: java eclipse unit-testing mockito

我试图从一个简单的例子中学习一个带有模拟对象的servlet的单元测试。我复制了所有示例代码,并将我认为必要的jar包含在我的项目的eclipse构建路径中。但是,我仍然得到错误:'com.mockobjects.MockObject类型无法解析。它 是从所需的.class文件'

间接引用的

罐子是:

mockito-all-1.9.5-sources.jar
mockito-all-1.9.5.jar
mockobjects-jdk1.3-j2ee1.3-0.09.jar
servlet-api-2.5.jar

温度servlet:

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//Converts farenheit to celcius
public class TemperatureServlet extends HttpServlet {

    public void doGet(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException {
        String str_f = req.getParameter("Fahrenheit");

        res.setContentType("text/html");
        PrintWriter out = res.getWriter();

        try {
            int temp_f = Integer.parseInt(str_f);
            double temp_c = (temp_f - 32) * 5.0 / 9.0;
            out.println("Fahrenheit: " + temp_f + ", Celsius: " + temp_c);
        } catch (NumberFormatException e) {
            out.println("Invalid temperature: " + str_f);
        }
    }
}

测试员类:

import junit.framework.TestCase;

import com.mockobjects.servlet.MockHttpServletRequest;
import com.mockobjects.servlet.MockHttpServletResponse;

public class TestTempServlet extends TestCase {

    public void test_bad_parameter() throws Exception {
        TemperatureServlet s = new TemperatureServlet();
        MockHttpServletRequest request = new MockHttpServletRequest();
        MockHttpServletResponse response = new MockHttpServletResponse();

        request.setupAddParameter("Fahrenheit", "boo!");
        response.setExpectedContentType("text/html");
        s.doGet(request, response);
        response.verify();
        assertEquals("Invalid temperature: boo!\n",
                response.getOutputStreamContents());
    }

    public void test_boil() throws Exception {
        TemperatureServlet s = new TemperatureServlet();
        MockHttpServletRequest request = new MockHttpServletRequest();
        MockHttpServletResponse response = new MockHttpServletResponse();

        request.setupAddParameter("Fahrenheit", "212");
        response.setExpectedContentType("text/html");
        s.doGet(request, response);
        response.verify();
        assertEquals("Fahrenheit: 212, Celsius: 100.0\n",
                response.getOutputStreamContents());
    }

}

3 个答案:

答案 0 :(得分:0)

你错过了mockobjects-core jar。你有mockito-all.jar而不是mockobjects all ver 1.9.5

同时添加mockobjects-jdk1.4-j2ee1.3.jar并删除mockobjects-jdk1.3-j2ee1.3-0.09.jar`

答案 1 :(得分:0)

本书是从2003年开始使用java和junit进行实用的单元测试。因此,很多代码可能无法正常工作。首先,在您的项目中只有以下罐子:

commons-logging-1.1.1.jar
servlet-api-2.5.jar
spring-core-3.1.1.RELEASE.jar
spring-mock-2.08.jar

使用以下给出的clasess的编辑代码。您的代码将通过所有测试。另请注意,api中没有reponse.verify方法,所以注释了它。

的Servlet

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//Converts farenheit to celcius
public class TemperatureServlet extends HttpServlet {

    public void doGet(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException {
        String str_f = req.getParameter("Fahrenheit");

        res.setContentType("text/html");
        PrintWriter out = res.getWriter();

        try {
            int temp_f = Integer.parseInt(str_f);
            double temp_c = (temp_f - 32) * 5.0 / 9.0;
            out.print("Fahrenheit: " + temp_f + ", Celsius: " + temp_c);
        } catch (NumberFormatException e) {
            out.print("Invalid temperature: " + str_f);
        }
    }
}

测试仪:

import junit.framework.TestCase;

import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;

public class TestTempServlet extends TestCase {

    public void test_bad_parameter() throws Exception {
        TemperatureServlet s = new TemperatureServlet();

        MockHttpServletRequest request = new MockHttpServletRequest();
        MockHttpServletResponse response = new MockHttpServletResponse();

        request.addParameter("Fahrenheit", "boo!");
        response.setContentType("text/html");
        s.doGet(request, response);
        //response.verify();//Method not in latest api.
        assertEquals("Invalid temperature: boo!",
                response.getContentAsString());
    }

    public void test_boil() throws Exception {
        TemperatureServlet s = new TemperatureServlet();
        MockHttpServletRequest request = new MockHttpServletRequest();
        MockHttpServletResponse response = new MockHttpServletResponse();

        request.addParameter("Fahrenheit", "212");
        response.setContentType("text/html");
        s.doGet(request, response);
        //response.verify();//Method not in latest api.
        assertEquals("Fahrenheit: 212, Celsius: 100.0",
                response.getContentAsString());
    }

}

答案 2 :(得分:0)

通过从mockito清除spring-boot-test存储库和.m2 repository存储库,然后更新maven项目(Alt + F5),解决了我类似的org.mockito.Answers; cannot be resolved. It is indirectly referenced from required .class files

问题