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 7698657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:17:35+00:00 2026-05-31T22:17:35+00:00

I have written a JUnit test for a private function which is returning String.

  • 0

I have written a JUnit test for a private function which is returning String. And it is working fine.

public void test2() throws Exception
{
    MyHandler handler = new MyHandler();
    Method privateStringMethod = MyHandler.class.getDeclaredMethod("getName", String.class);
    privateStringMethod.setAccessible(true);
    String s = (String) privateStringMethod.invoke(handler, 852l);
    assertNotNull(s);
}

I have one more function which returns boolean but this is not working.
But in that I am getting a compile time error saying Cannot cast from Object to boolean.

public void test1() throws Exception
{
    MyHandler handler = new MyHandler();
    Method privateStringMethod = MyHandler.class.getDeclaredMethod("isvalid", Long.class);
    privateStringMethod.setAccessible(true);
    boolean s = (boolean) privateStringMethod.invoke(handler, 852l);
    assertNotNull(s);
}

How can I run?

  • 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-05-31T22:17:37+00:00Added an answer on May 31, 2026 at 10:17 pm

    The returnvalue will be ‘autoboxed’ to an Boolean object. Since a primitive can’t be null, you mustn’t test against null. Even .booleanValue() mustn’t be called, because of Autoboxing.

    But I’m of the same opinion as @alex.p, regarding testing private methods.

    public class Snippet {
    
        @Test
        public void test1() throws Exception {
            final MyHandler handler = new MyHandler();
            final Method privateStringMethod = MyHandler.class.getDeclaredMethod("isvalid");
            privateStringMethod.setAccessible(true);
            final Boolean s = (Boolean) privateStringMethod.invoke(handler);
            Assert.assertTrue(s.booleanValue());
        }
    
        class MyHandler {
            private boolean isvalid() {
                return false;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written few test scripts using JUnit 4 and Selenium. I have added
I have written JUnit tests for my class, and would like it to tell
We have a lot of integration tests written using JUnit 3 , though we're
I have written a PHP application which requires storage of millions of integers between
I've been working on a Java application where I have to use JUnit for
What is the best way to learn RSpec and Rails3? I have written JUnit/NUnit
I'm converting a bunch of test cases written for use with JUnit 4+ to
I have a test class, written in JUnit4 syntax, that can be run in
We have test classes which are built on Spring 2.0.8's AbstractTransactionalDataSourceSpringContextTests. There are a
I'm working on a project at the moment that we're using junit to test,

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.