The meaning of the final method with PowerMock + EasyMock

I am trying to make fun of calling the final ResourceBundle.getString() method. With PowerMock 1.4.12 and EasyMock 3.1, the call is not mocked; instead, the "real" method is called.

My test class:

 @RunWith(PowerMockRunner.class) @PrepareForTest(ResourceBundle.class) public class TestSuite { @Before public void setUp() throws Exception { ResourceBundle resourceBundleMock = PowerMock.createNiceMock(ResourceBundle.class); expect(resourceBundleMock.getString(BundleConstants.QUEUE)).andReturn("Queue"); PowerMock.replay(resourceBundleMock); beanBeingTested.setMessages(resourceBundleMock); } ... } 

Code in BeanBeingTested:

 private ResourceBundle messages; ... String label = messages.getString(BundleConstants.QUEUE); 

Error message:

 java.util.MissingResourceException: Can't find resource for bundle $java.util.ResourceBundle$$EnhancerByCGLIB$$e4a02557, key Queue at java.util.ResourceBundle.getObject(ResourceBundle.java:384) at java.util.ResourceBundle.getString(ResourceBundle.java:344) at com.yoyodyne.BeanBeingTested.setUpMenus(BeanBeingTested.java:87) 

When I move on to the test case, the debugger shows the beanBeingTested.messages type as "EasyMock for the java.util.ResourceBundle class", so the layout is entered correctly. (In addition, there is no error during getString() in the call to expect() during setup).

With a simple mock instead of a nice layout, I get the following error:

 java.lang.AssertionError: Unexpected method call handleGetObject("Queue"): getString("Queue"): expected: 1, actual: 0 

Any idea what I'm doing wrong?

Thanks.

+6
source share
3 answers

An instance is created using EasyMock. Instead, when working with static methods, you should mock the class (using PowerMock).

It should work like this (testing with EasyMock 3.0 and PowerMock 1.5):

 @RunWith(PowerMockRunner.class) @PrepareForTest(ResourceBundle.class) public class TestSuite { @Before public void setUp() throws Exception { // mock the class for one method only PowerMock.mockStaticNice(ResourceBundle.class, "getString"); // define mock-behaviour on the class, when calling the static method expect(ResourceBundle.getString(BundleConstants.QUEUE)).andReturn("Queue"); // start the engine PowerMock.replayAll(); } } 

(I know this question is several months old, but it can help others though)

+3
source

Try using:

 @PrepareForTest({ResourceBundle.class, BeanBeingTested.class}) 

Only using the ResourceBundle in PrepareForTest will the layout work when called directly from your unit test method, but when called from BeanBeingTested you will get a real method.

Powermock documentation is not available in this area.

+1
source

Why bother mocking a resource package call? As a rule, I try to avoid ridicule of java nuts and bolts such as ArrayList, Date, etc. Resource packages (and MessageFormat.format ()) more or less fall into the same category for me. They usually work with strings that are fundamental, and if these things are broken or change their behavior enough to break the test, this is definitely what I want to know :)

Just let them grab the line (which supposedly should be installed in the user interface, perhaps after it. Do not bother with the return value, since you do not want the changes in the package to interrupt your test. The line is set to the layout of the user interface component This is a good place for anyObject (String.class), which correctly expresses the fact that you (probably) really don't care about the particular string being displayed.

I also find this useful when the test fails due to a missing message key. WHAT I want to know.

0
source

Source: https://habr.com/ru/post/923574/


All Articles