Testing class with Protected Constructor w/ PowerMock and Mockito?

You don't need PowerMock for this; a Mockito mock will be just fine. But normally, a mock has no functionality in its methods, which is why toStrings() isn't returning the value that you expect. To change this, you need the CALLS_REAL_METHODS default answer.

Up vote 1 down vote favorite share g+ share fb share tw.

I have the following class to test: public abstract class Challenge { protected int id; protected String name; protected String question; protected Challenge(){} public String toStrings(){ String s = {Integer. ToString(id), name, question}; return s; } ... But using this test: @Test public void testToStrings() throws Exception{ String expectedResult1 = new String{"1", "a", "b"}; String obtainedResult1 = null; Challenge challengeMock = PowerMockito. Mock(Challenge.

Class); challengeMock. Id = 1; challengeMock. Name = "a"; challengeMock.

Question = "b"; obtainedResult1 = challengeMock.toStrings(); Assert. AssertEquals(expectedResult10, obtainedResult10); Assert. AssertEquals(expectedResult11, obtainedResult11); Assert.

AssertEquals(expectedResult12, obtainedResult12); } I get a NullPointerException due to "obtainedResult1 = challengeMock.toStrings();" that returns null. I use PowerMock + Mockito running in Robolectric with rule(becouse its an Android project). @Rule public PowerMockRule rule = new PowerMockRule(); Where is the problem?

Java testing mockito powermock robolectric link|improve this question edited Mar 25 at 16:39Hunter McMillen4,2001311 asked Mar 25 at 16:36DkSw102.

Maybe you want to use PowerMockito.spy() or try to create an (anonymous) implementation of Challenge. – nansen Mar 25 at 17:59 I mocked it only cos was easier to use the private constructor and to set the private data. Any tips for spy use, an example maybe?

– DkSw Mar 26 at 7:56 FYI, although your question seems to have been answered by David already: a spy is similar to a mock but usually is used to mock or inspect certain behaviour of the actual CUT. You can use it like mock (Mockito. Spy(CUT.

Class)) or with annotations. An equivalent method is offered by PowerMockito. I tend not to use powermock at all since I usually find its neccessity a sign of bad design i.e.

Non-testability. But that's personal taste. – nansen Mar 26 at 18:23 Thanks for your suggestion.

I use powermock only becouse I have several static final methods and abstract class, so I can test them too. Btw I have a really near deadline so the solution with real_method is quicker. For the next project I'm surely going to better learn testing and using spy.

– DkSw Mar 27 at 8:21.

You don't need PowerMock for this; a Mockito mock will be just fine. But normally, a mock has no functionality in its methods, which is why toStrings() isn't returning the value that you expect. To change this, you need the CALLS_REAL_METHODS default answer.

So my recommendation would be to change the line where you create the mock (the third non-empty line of testToStrings) to something like this. Challenge challengeMock = Mockito. Mock(Challenge.

Class, Mockito. CALLS_REAL_METHODS ); I have tested this, and your test passes if you make this change.

Thanks! I didn't know propely how mock works, now the test runs fine. – DkSw Mar 26 at 10:55 That question about properties was deleted but... how a question with the c# java php and c can be helpful and clear?

Btw he just gave me a serial 25 downvotes... check this – gdoron Mar 26 at 12:23.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions