2016-04-20 6 views
0

Ich habe eine Klasse, in der es eine private Variable Connection gibt. Ich möchte Funktion A testen, für die ich Funktion B und Funktion C vortäuschen muss.Test (Mocking) eine Void-Funktion, die intern andere Funktionen aufruft, die eine Verbindung zur Datenbank herstellen

Ich versuchte es mit PowerMock und Mockito, aber nicht in der Lage, es zu tun.

Was genau getan werden sollte, um Funktion A zu testen und Funktion B und Funktion C zu mocksen.

public class ToMock { 

    private Connection connection; 

    private static functionA(String name) {  
     // do something  
     functionB()  
     return xyz; 
    } 

    public static void functionB() { 
     connection = functionC("localhost", 10000);  
    } 

    public static void functionC(String hostName, int port) { 
     //make a connection to db 

     String connectionString = String.format("jdbc:hive2://%s:%d/",emrHost, port); 

     LOGGER.info("Connection string {}", connectionString); 

     try { 

      Class.forName("org.apache.hive.jdbc.HiveDriver");  
      Connection con = DriverManager.getConnection(connectionString, "hadoop", ""); 

      LOGGER.info("Connected successfully"); 

      return con;  
     } catch (ClassNotFoundException e) { 
      throw Throwables.propagate(e); 
     } 
    } 
} 
+1

Sie könnten das Verbindungserstellungsteil in eine neue Fabrikklasse verschieben und dann nur diese Klasse vortäuschen. Auf diese Weise behalten Sie Ihre Logik auch unabhängig von der Erstellung der Datenbankverbindung ... –

Antwort

0

Refactor Ihren Code wie @ neurotic-d beschreibt. So etwas wie das:

public class ToMock { 

    private Connection connection; 

    public ToMock(Connection connection){ 
     this.connection = connection; 
    } 

    private functionA(String name) {  
     // do something  

     return xyz; 
    } 
} 



public class ToMockFactory { 

    public static ToMock toMock(){ 
     return new ToMock(functionB()); 
    } 

    public static Connection functionB() { 
     return functionC("localhost", 10000);  
    } 

    public static Connection functionC(String hostName, int port) { 
     //make a connection to db 

     String connectionString =  String.format("jdbc:hive2://%s:%d/",emrHost, port); 

     LOGGER.info("Connection string {}", connectionString); 

     try { 

      Class.forName("org.apache.hive.jdbc.HiveDriver");  
      Connection con = DriverManager.getConnection(connectionString, "hadoop", ""); 

      LOGGER.info("Connected successfully"); 

      return con;  
     } catch (ClassNotFoundException e) { 
      throw Throwables.propagate(e); 
     } 
    } 
} 
0

Nun, Sie haben static Methoden. Sie können Mockito also nicht nach seinem Design verwenden. Sie könnten PowerMock verwenden. Siehe Verwendung hier: Link.

Siehe hier: link.

Grundsätzlich Code, der wie folgt (Kopiert von PowerMock Beispiele) aussehen wird,

@RunWith(PowerMockRunner.class) 
@PrepareForTest(Static.class) 
public class YourTestCase { 
@Test 
public void testMethodThatCallsStaticMethod() { 
    // mock all the static methods in a class called "Static" 
    PowerMockito.mockStatic(Static.class); 
    // use Mockito to set up your expectation 
    Mockito.when(Static.firstStaticMethod(param)).thenReturn(value); 
    Mockito.when(Static.secondStaticMethod()).thenReturn(123); 

    // execute your test 
    classCallStaticMethodObj.execute(); 

    // Different from Mockito, always use PowerMockito.verifyStatic() first 
    // to start verifying behavior 
    PowerMockito.verifyStatic(Mockito.times(2)); 
    // IMPORTANT: Call the static method you want to verify 
    Static.firstStaticMethod(param); 


    // IMPORTANT: You need to call verifyStatic() per method verification, 
    // so call verifyStatic() again 
    PowerMockito.verifyStatic(); // default times is once 
    // Again call the static method which is being verified 
    Static.secondStaticMethod(); 

    // Again, remember to call verifyStatic() 
    PowerMockito.verifyStatic(Mockito.never()); 
    // And again call the static method. 
    Static.thirdStaticMethod(); 
} 
} 

Hier Static ist die Klasse, zu der die statische Methode (n) gehört.

Verwandte Themen