2012-10-18 18 views
20

Ho una funzione che cerca un parametro di query e restituisce un valore booleano:Come si deride 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; 
    } 

Ho sia JUnit e EasyMock nel mio pom.xml, come posso fare per il beffardo HttpServletRequest?

+3

La risposta di sotto di questa domanda come scritto bene, ma mi sarebbe normalmente sostenere per il refactoring del codice in modo che la maggior parte dei non banale la logica è in un livello più appropriato di astrazione. Se ciò è possibile, allora la derisione di HttpServletRequest diventa inutile. –

risposta

8

HttpServletRequest è molto simile a qualsiasi altra interfaccia, in modo da poter prendere in giro seguendo la EasyMock Readme

Ecco un esempio di come unità di prova il metodo di 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

Utilizzare una struttura di derisione, ad es. Mockito o JMock che viene fornito con la capacità di derisione di tali oggetti.

In Mockito, si può fare beffardo come:

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

Per i dettagli su Mockito, si veda: How do I drink it? sul sito Mockito.

In JMock, si può fare beffardo come:

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

Per i dettagli su jMock, si prega di fare riferimento: jMock - Getting Started

+0

Perché non sono richiesti i cast? –

+0

Perché usano i generici con l'inferenza di tipo come dovrebbe ogni buona libreria moderna. – Hiro2k

+0

Stai passando '.class' come argomento per simulare i metodi. –

Problemi correlati