2012-10-18 22 views
20

मैं एक समारोह है कि एक क्वेरी पैरामीटर के लिए लग रहा है और एक बूलियन देता है:HttpServletRequest का नकल कैसे करें?

public static Boolean getBooleanFromRequest(HttpServletRequest request, String key) { 
     Boolean keyValue = false; 
     if(request.getParameter(key) != null) { 
      String value = request.getParameter(key); 
      if(keyValue == null) { 
       keyValue = false; 
      } 
      else { 
       if(value.equalsIgnoreCase("true") || value.equalsIgnoreCase("1")) { 
        keyValue = true; 
       } 
      } 
     } 
     return keyValue; 
    } 

मैं दोनों JUnit और मेरे pom.xml में easymock है, मैं मजाक HttpServletRequest के बारे में कैसे जाते हैं?

+3

नीचे इस सवाल का जवाब इस सवाल है, लेकिन मैं सामान्य रूप से कोड पुनर्रचना के लिए पैरवी करेंगे ताकि गैर तुच्छ के सबसे कि तर्क अबास्ट्रक्शन के एक और उचित स्तर पर है। यदि यह संभव है, तो HttpServletRequest का मज़ाक उड़ाना अनावश्यक हो जाता है। –

उत्तर

8

HttpServletRequest, ताकि आप इसे EasyMock Readme

यहाँ का पालन करते हुए नकली कर सकते हैं कि किस इकाई का परीक्षण करने के अपने getBooleanFromRequest विधि

// static import allows for more concise code (createMock etc.) 
import static org.easymock.EasyMock.*; 

// other imports omitted 

public class MyServletMock 
{ 
    @Test 
    public void test1() 
    { 
     // Step 1 - create the mock object 
     HttpServletRequest req = createMock(HttpServletRequest.class); 

     // Step 2 - record the expected behavior 

     // to test true, expect to be called with "param1" and if so return true 
     // Note that the method under test calls getParameter twice (really 
     // necessary?) so we must relax the restriction and program the mock 
     // to allow this call either once or twice 
     expect(req.getParameter("param1")).andReturn("true").times(1, 2); 

     // program the mock to return false for param2 
     expect(req.getParameter("param2")).andReturn("false").times(1, 2); 

     // switch the mock to replay state 
     replay(req); 

     // now run the test. The method will call getParameter twice 
     Boolean bool1 = getBooleanFromRequest(req, "param1"); 
     assertTrue(bool1); 
     Boolean bool2 = getBooleanFromRequest(req, "param2"); 
     assertFalse(bool2); 

     // call one more time to watch test fail, just to liven things up 
     // call was not programmed in the record phase so test blows up 
     getBooleanFromRequest(req, "bogus"); 

    } 
} 
12

कुछ मॉकिंग फ्रेमवर्क का प्रयोग करें उदा। Mockito या JMock जो ऐसी वस्तुओं की नकली क्षमता के साथ आता है।

HttpServletRequest mockedRequest = Mockito.mock(HttpServletRequest.class); 

Mockito पर जानकारी के लिए देखें:: How do I drink it? Mockito साइट पर

Mockito में, आप के रूप में मजाक कर सकते हैं।

JMock में, आप मजाक कर सकते हैं के रूप में:

Mockery context = new Mockery(); 
HttpServletRequest mockedRequest = context.mock(HttpServletRequest.class); 

jMock पर जानकारी के लिए देखें: jMock - Getting Started

+0

क्यों आवश्यक नहीं हैं? –

+0

क्योंकि वे किसी भी अच्छी आधुनिक पुस्तकालय की तरह प्रकार अनुमान के साथ जेनरिक का उपयोग करते हैं। – Hiro2k

+0

आप नकली विधियों के लिए तर्क के रूप में '.class' पास कर रहे हैं। –

2

मैं easymock के बारे में पता नहीं है, लेकिन पुस्तक 'Unit Testing in Java: How Tests Drive the Code' by Johannes Link निहित के स्पष्टीकरण के लिए कैसे लाइब्रेरी का उपयोग करके सर्विसेज परीक्षण करें जो वह डमी ऑब्जेक्ट्स का निर्माण करेगा।

पुस्तक के लिए साथी साइट अब लेकिन the companion site from the original german publication is still up (कुछ ... की कंपनी के प्रकाशन में परिवर्तन) चला गया है। इससे, you can download the definitions of all the dummy objects

0

Mockrunner पर एक नज़र डालें का एक उदाहरण किसी अन्य इंटरफ़ेस की तरह ज्यादा है: http://mockrunner.sourceforge.net/

जावा ईई मैक्स का उपयोग करना बहुत आसान है, जिसमें HttpServletRequest और HttpServletResponse शामिल हैं।

7

यह एक पुराना धागा है ... लेकिन सवाल अभी भी प्रासंगिक है।

एक और अच्छा विकल्प स्प्रिंग ढांचे में MockServiceRequest और MockServiceResponse है: रूप में अच्छी तरह से लिखा

http://docs.spring.io/spring/docs/2.0.x/api/org/springframework/mock/web/package-summary.html

 संबंधित मुद्दे

  • कोई संबंधित समस्या नहीं^_^