Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I test final and static methods of a utility project?

Tags:

java

junit

I'm trying to implement unit testing for aproject, it uses a legacy "utility" project that is littered with static methods and many of the classes are final or their methods are final. I'm not able to update the legacy project at all.

JMock and EasyMock both choke on final methods, and I don't see a nice way to test the static calls. What techniques are there to test these?

like image 435
talk to frank Avatar asked Aug 18 '09 11:08

talk to frank


People also ask

How do you test the static method?

All you need to do is wrap the static method call inside an instance method and then use dependency injection to inject an instance of the wrapper class to the class under test.

How do you write a test case for a static method?

Note that you should add the class that contains static methods in two places in your unit tests: On top of the unit test class using @PrepareForTest annotation. In your test setup by calling the PowerMockito. mockStatic to do the necessary initialization step before trying to mock any of its methods.

What is a static utility method?

Static methods are utility methods defined in a class that do not fit the tradition method- receiver pattern. Static methods are used where there is not a natural choice of a receiver object for the problem the method solves.

Can we write unit test for static class?

This will allow you to send in different values for testing different scenarios. If however the entire class as it is cannot appear in the UT (due to environment constraints on initialization, for example), consider having just the business logic exported to a seperate non static class and write your UT for that.


2 Answers

If you're able to refactor your code, you can wrap your calls to the final/static methods in simple instance methods, for example:

protected Foo doBar(String name) {     return Utility.doBar(name); } 

This allows you to override your wrapper method in the unit test to return a mock instance of Foo.

Alternatively you can use Powermock, which extends Easymock (and Mockito) to allow mocking of final and static methods:

PowerMock is a framework that extend other mock libraries such as EasyMock with more powerful capabilities. PowerMock uses a custom classloader and bytecode manipulation to enable mocking of static methods, constructors, final classes and methods, private methods, removal of static initializers and more.

Here's an example test mocking a static final method, the example shows how to mock some other types too:

@Test public void testMockStaticFinal() throws Exception {     mockStatic(StaticService.class);     String expected = "Hello altered World";     expect(StaticService.sayFinal("hello")).andReturn("Hello altered World");     replay(StaticService.class);      String actual = StaticService.sayFinal("hello");      verify(StaticService.class);     assertEquals("Expected and actual did not match", expected, actual);      // Singleton still be mocked by now.     try {         StaticService.sayFinal("world");             fail("Should throw AssertionError!");     } catch (AssertionError e) {         assertEquals("\n  Unexpected method call sayFinal(\"world\"):",              e.getMessage());     } } 
like image 160
Rich Seller Avatar answered Sep 20 '22 01:09

Rich Seller


How about a level of indirection / Dependency Injection?

Since the legacy utility project is your dependency, create an interface to separate it out from your code. Now your real/production implementation of this interface delegates to the legacy utility methods.

public LegacyActions : ILegacyActions {   public void SomeMethod() { // delegates to final/static legacy utility method } } 

For your tests, you can create a mock of this interface and avoid interacting with the legacy utility thingie.

like image 21
Gishu Avatar answered Sep 20 '22 01:09

Gishu