Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7718519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:11:11+00:00 2026-06-01T03:11:11+00:00

I have the following class to test: public abstract class Challenge { protected int

  • 0

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(expectedResult1[0], obtainedResult1[0]);
    Assert.assertEquals(expectedResult1[1], obtainedResult1[1]);
    Assert.assertEquals(expectedResult1[2], obtainedResult1[2]);
}

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T03:11:12+00:00Added an answer on June 1, 2026 at 3:11 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following class hierarchy class Test { public string Name { get;
I have the following simple code abstract class A { public abstract void Test(Int32
I have following code public class TEST { public static void main(String arg[]){ try
I have a question about .net generics. Consider the following code: public abstract class
I have the following class: public class Test { public static int a =
I have the following class: public class Test { public string Text { get;
I have the following code: public class MyElement { String name; String type; MyElement(String
I have following piece of code: class Test{ private: int id; public: Test(int v):id(v)
I have the following class: public abstract class AbstractParent { static String method() {
If have following class public abstract class MyBaseClass : BaseClass { public override string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.