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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:10:23+00:00 2026-06-10T13:10:23+00:00

Say I have the following code: public class ClassToTest { AnotherClass anotherClass; public void

  • 0

Say I have the following code:

public class ClassToTest
{
    AnotherClass anotherClass;

    public void methodToTest( int x, int y )
    {
        int z = x + y
        anotherClass.receiveSomething( z );
    }
}

public class AnotherClass
{
    public void receiveSomething( int z ) {.. do something.. }  
}

I want to make assertion on the value of the variable z. How do I do this without refactoring? Variables x, y, and z could be some other Java class types, and I just used “int” for simplicity.

  • 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-10T13:10:25+00:00Added an answer on June 10, 2026 at 1:10 pm

    You can write the following. Of course dependency injection and test method might change depending on your current implementation and/or your test scenarios.

    @RunWith(MockitoJUnitRunner.class)
    public class ClassToTestTest {
    
        @Mock AnotherClass anotherClass;
        @InjectMocks ClassToTest classToTest;
    
        @Test public void ensure_that_value___Z___is_passed_to___AnotherClass() {
            classToTest.methodToTest(1, 2);
    
            verify(anotherClass).receiveSomething(eq(3));
        }
    }
    

    Written with Mockito 1.9.5-rc1 in mind.

    Also instead of using the eq matcher you can instead use an ArgumentCaptor, which will capture values or value references (in case of objects). You’ll be able to perform additional assertions on the captured value(s). For example using Truth or FEST-Assert.

    This could look like :

    @RunWith(MockitoJUnitRunner.class)
    public class ClassToTestTest {
    
        ...
        @Captor ArgumentCaptor<List<Z>> listCaptor
    
        @Test public void ensure_that_list_with_value___Z___is_passed_to___AnotherClass() {
            // given
    
            // when
            ...
    
            // then
            verify(anotherClass).receiveList(listCaptor.capture());
            assertThat(listCaptor.getValue()).contains(Z);
        }
    }
    

    Cheers,

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

Sidebar

Related Questions

Let's say I have the following code: public class Foo { private int x;
Lets say that I have the following code: public class Shelter<A extends Animal, B
Say suppose I have the following Java code. public class Example { public static
Lets say I have the following code: public class Base { // Some stuff
Let's say we have the following piece of code: public class Event { }
Lets say I have the following code: public class Collection implements CollectionInterface{ ElementInterface[] elementArray
Say you have the following class public class AccessStatistics { private final int noPages,
Say i have the following: public class Person{ public int ID {get; set;} public
I have written the following code: public class NewClass2 implements Comparator<Point> { public int
Say I have the following code: import java.lang.InterruptedException; import javax.swing.SwingWorker; public class 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.