For what reason should I make fun of?

I am new to Mockito and PowerMockito. I found out that I cannot test static methods using pure Mockito, so I need a PowerMockito user (right?).

I have a very simple class called Validate using this very simple method

public class Validate { public final static void stateNotNull( final Object object, final String message) { if (message == null) { throw new IllegalArgumentException("Exception message is a null object!"); } if (object == null) { throw new IllegalStateException(message); } } 

Therefore, I need to verify that:

1) When I call this static method in the argument of the null message, IllegalArgumentException is called 2) When I call this static method on the argument of the null object, IllegalStateException is called

From what I got so far, I wrote this test:

 import static org.mockito.Matchers.anyString; import static org.mockito.Matchers.isNull; import org.junit.Before; import org.junit.runner.RunWith; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import org.testng.annotations.Test; @RunWith(PowerMockRunner.class) @PrepareForTest(Validate.class) public class ValidateTestCase { @Test(expectedExceptions = { IllegalStateException.class }) public void stateNotNullTest() throws Exception { PowerMockito.mockStatic(Validate.class); Validate mock = PowerMockito.mock(Validate.class); PowerMockito.doThrow(new IllegalStateException()) .when(mock) .stateNotNull(isNull(), anyString()); Validate.stateNotNull(null, null); } } 

So, this suggests that I am mocking the Validate class, and I verify that when mock is called in this method with a null argument as an object and any string as a message, an IllegalStateException is thrown.

Now, I really do not understand. Why can't I just call this method directly, dropping all the voodoo magic around mocking this static class? It seems to me that if I do not call Validate.stateNotNull, that the test will pass anyway ... For what reason should I mock this?

+6
source share
2 answers

First decide what your goal is and what you want to test. Your test does not test your method of the Validate class, it creates a layout that behaves like this method, as Fortega points out . Determine what you are testing (the test object) and what you need to conduct the test (co-authors), then look at the employees and determine if they are easy to create or if you need to mock them.

For something like this class, which has nothing to do with anything, I would recommend doing the whole without layouts. There is nothing to scoff at, the test can be written as follows:

 import static org.junit.Assert.*; public class ValidateTestCase { @Test public void testHappyPath() throws Exception { Validate.stateNotNull("", ""); } @Test public void testNullMessage() throws Exception { try { Validate.stateNotNull(null, null); fail(); } catch (IllegalStateException e) { String expected = "Exception message is a null object!" assertEquals(expected, e.getMessage()); } } @Test(expected=IllegalStateException.class) public void testNullObject() throws Exception { Validate.stateNotNull(null, "test"); } } 

and that tells you if the code is doing what you want.

Do not make fun if there is no dependency that you want to avoid when entering the test because it is an external resource (for example, a file system or database) or some complex subsystem. Layout frames can be very useful, but they add complexity, they can override the behavior of the things being tested, making tests fragile, and they can make tests difficult to read. If possible, do it without them.

+8
source

You should not scoff at the classes and methods that you are testing. You should only simulate the methods needed to complete the test itself.

For example, if you need some objects from a web service to run a test, you can make fun of web service calls, so you don’t really need to call a web service.

+11
source

All Articles