Uploaded image for project: 'Unitils'
  1. Unitils
  2. UNI-246

Allowing to create mock on private methods, static fields and mock construction of new object

    Details

    • Type: Improvement
    • Status: Open
    • Priority: Minor
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: None
    • Labels:
      None

      Description

      I'm using Unitils on my projects, but also another library that has some wonderful features : powermock.
      Unfortunately, we can not use two runners (powermock + unitils) with junit.
      It would be great if unitils could include some useful features such as :

      - Mocking private methods :

      DataService tested = PowerMock.createMock(DataService.class);
      PowerMock.expectPrivate(tested, "privateMethod", param1).andReturn(true);

      - Mocking static methods :

      mockStatic(StaticService.class);
      String expected = "Hello altered World";
      expect(StaticService.say("hello")).andReturn("Hello altered World");

      - Mocking new objects created inside a method :

      powermock uses its own custom class loader in order to alter classes and to be able to return a mock instead of the real class :

      For example we have the following method :

      public void execute() {
      [...]
      HttpURLConnection connection = new HttpURLConnection(url) ;
      connection.connect;
      [...]
      }

      With EasyMock and Unitils, we cannot create a mock for the HttpURLConnection because the object is created and used inside the same method.
      But with powermock we can alter the HttpURLConnection in order to replace it by a mock :

      HttpURLConnection httpURLConnectionMock = PowerMock.createMock(HttpURLConnection.class);
      PowerMock.expectNew(HttpURLConnection.class, EXPECTED_CALLED_URL).andReturn(httpURLConnectionMock );
      // Expect
      httpURLConnectionMock.connect();

      replayAll();
      myService.execute();
      verifyAll();

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              tony75 tony75
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated: