How can I access member variable of method of a class under test? The Next CEO of Stack OverflowCan I access member variable of method of a class under test?How can I concatenate two arrays in Java?How do you assert that a certain exception is thrown in JUnit 4 tests?How can I create an executable JAR with dependencies using Maven?How can I convert a stack trace to a string?How to make mock to void methods with mockitoHow can I make a method return an argument that was passed to it?How to run test methods in specific order in JUnit4?Mockito : how to verify method was called on an object created within a method?How to verify that a specific method was not called using Mockito?Can I access member variable of method of a class under test?

Anatomically Correct Strange Women In Ponds Distributing Swords

How does the Z80 determine which peripheral sent an interrupt?

How does the mv command work with external drives?

Why does the UK parliament need a vote on the political declaration?

Can I run my washing machine drain line into a condensate pump so it drains better?

Can I equip Skullclamp on a creature I am sacrificing?

Why am I allowed to create multiple unique pointers from a single object?

How did the Bene Gesserit know how to make a Kwisatz Haderach?

Which tube will fit a -(700 x 25c) wheel?

If Nick Fury and Coulson already knew about aliens (Kree and Skrull) why did they wait until Thor's appearance to start making weapons?

What happens if you roll doubles 3 times then land on "Go to jail?"

Several mode to write the symbol of a vector

MessageLevel in QGIS3

Return the Closest Prime Number

Is 'diverse range' a pleonastic phrase?

I believe this to be a fraud - hired, then asked to cash check and send cash as Bitcoin

Is it possible to search for a directory/file combination?

If/When UK leaves the EU, can a future goverment conduct a referendum to join the EU?

How to solve a differential equation with a term to a power?

To not tell, not take, and not want

Bold, vivid family

What is the result of assigning to std::vector<T>::begin()?

Can you replace a racial trait cantrip when leveling up?

How to safely derail a train during transit?



How can I access member variable of method of a class under test?



The Next CEO of Stack OverflowCan I access member variable of method of a class under test?How can I concatenate two arrays in Java?How do you assert that a certain exception is thrown in JUnit 4 tests?How can I create an executable JAR with dependencies using Maven?How can I convert a stack trace to a string?How to make mock to void methods with mockitoHow can I make a method return an argument that was passed to it?How to run test methods in specific order in JUnit4?Mockito : how to verify method was called on an object created within a method?How to verify that a specific method was not called using Mockito?Can I access member variable of method of a class under test?










-2















Firstly I request to not mark it as duplicate of my previous question. It was incorrectly marked as duplicate and wrongly answered. Please read the question carefully and respond. Thanks..!!



So I want to access a member variable which has scope only inside the method which is to be tested during unit test of my controller class. So the code looks like below.



@Controller
public class testController(){

@Autowired
private Dependency dependency;

@RequestMapping("/SearchProject)
public String systemUnderTest()
// some logic
Integer[] totalrecords = new Integer[1];
List<SomeObject> aList = dependency.someMethod(.,.,totalRecords,.,.);
//some logic
int value = Math.ceil(totalrecords[0]/CONSTANT_VALUE);
return "string";




What is happening is totalRecords[0] is set to a value inside someMethod().



Now when I am unit testing it using standalone method of MockMvc, it is giving NPE at line where 'value' is accessed.



Is there any way I can access and set this 'value' or 'totalRecords[0]' variable during my test ? Let me know if any more details are needed.










share|improve this question
























  • Have the Dependency object as a MockBean, and set the mock with mockito so that when someMethod is called on it, it will set some test value inside the array.

    – kumesana
    Mar 21 at 17:18











  • Use constructor injection instead of field injection. In your test, provide a mock Dependency. Mock the behaviour of dependency.someMethod to populate the array.

    – Ben R.
    Mar 21 at 17:25











  • I am already mocking the dependency and returning my custom List object from it using when(T).thenReturn(). But how to set the array[0] to a value I dont know.

    – aarish_codev
    Mar 21 at 17:44











  • You're basically asking why your test throws an exception, but you didn't post the exception stack trace, and you didn't post the code of your test...

    – JB Nizet
    Mar 22 at 11:23











  • @JBNizet I never asked why it throws exception.I already explained the reason for exception. 'IFF" you read the question carefully I don't think its hard to understand. Also look for the '?' sign while reading my question, you will know exactly what I'm asking. Thanks.

    – aarish_codev
    Mar 25 at 8:39















-2















Firstly I request to not mark it as duplicate of my previous question. It was incorrectly marked as duplicate and wrongly answered. Please read the question carefully and respond. Thanks..!!



So I want to access a member variable which has scope only inside the method which is to be tested during unit test of my controller class. So the code looks like below.



@Controller
public class testController(){

@Autowired
private Dependency dependency;

@RequestMapping("/SearchProject)
public String systemUnderTest()
// some logic
Integer[] totalrecords = new Integer[1];
List<SomeObject> aList = dependency.someMethod(.,.,totalRecords,.,.);
//some logic
int value = Math.ceil(totalrecords[0]/CONSTANT_VALUE);
return "string";




What is happening is totalRecords[0] is set to a value inside someMethod().



Now when I am unit testing it using standalone method of MockMvc, it is giving NPE at line where 'value' is accessed.



Is there any way I can access and set this 'value' or 'totalRecords[0]' variable during my test ? Let me know if any more details are needed.










share|improve this question
























  • Have the Dependency object as a MockBean, and set the mock with mockito so that when someMethod is called on it, it will set some test value inside the array.

    – kumesana
    Mar 21 at 17:18











  • Use constructor injection instead of field injection. In your test, provide a mock Dependency. Mock the behaviour of dependency.someMethod to populate the array.

    – Ben R.
    Mar 21 at 17:25











  • I am already mocking the dependency and returning my custom List object from it using when(T).thenReturn(). But how to set the array[0] to a value I dont know.

    – aarish_codev
    Mar 21 at 17:44











  • You're basically asking why your test throws an exception, but you didn't post the exception stack trace, and you didn't post the code of your test...

    – JB Nizet
    Mar 22 at 11:23











  • @JBNizet I never asked why it throws exception.I already explained the reason for exception. 'IFF" you read the question carefully I don't think its hard to understand. Also look for the '?' sign while reading my question, you will know exactly what I'm asking. Thanks.

    – aarish_codev
    Mar 25 at 8:39













-2












-2








-2


1






Firstly I request to not mark it as duplicate of my previous question. It was incorrectly marked as duplicate and wrongly answered. Please read the question carefully and respond. Thanks..!!



So I want to access a member variable which has scope only inside the method which is to be tested during unit test of my controller class. So the code looks like below.



@Controller
public class testController(){

@Autowired
private Dependency dependency;

@RequestMapping("/SearchProject)
public String systemUnderTest()
// some logic
Integer[] totalrecords = new Integer[1];
List<SomeObject> aList = dependency.someMethod(.,.,totalRecords,.,.);
//some logic
int value = Math.ceil(totalrecords[0]/CONSTANT_VALUE);
return "string";




What is happening is totalRecords[0] is set to a value inside someMethod().



Now when I am unit testing it using standalone method of MockMvc, it is giving NPE at line where 'value' is accessed.



Is there any way I can access and set this 'value' or 'totalRecords[0]' variable during my test ? Let me know if any more details are needed.










share|improve this question
















Firstly I request to not mark it as duplicate of my previous question. It was incorrectly marked as duplicate and wrongly answered. Please read the question carefully and respond. Thanks..!!



So I want to access a member variable which has scope only inside the method which is to be tested during unit test of my controller class. So the code looks like below.



@Controller
public class testController(){

@Autowired
private Dependency dependency;

@RequestMapping("/SearchProject)
public String systemUnderTest()
// some logic
Integer[] totalrecords = new Integer[1];
List<SomeObject> aList = dependency.someMethod(.,.,totalRecords,.,.);
//some logic
int value = Math.ceil(totalrecords[0]/CONSTANT_VALUE);
return "string";




What is happening is totalRecords[0] is set to a value inside someMethod().



Now when I am unit testing it using standalone method of MockMvc, it is giving NPE at line where 'value' is accessed.



Is there any way I can access and set this 'value' or 'totalRecords[0]' variable during my test ? Let me know if any more details are needed.







java spring model-view-controller junit mockito






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 24 at 4:36







aarish_codev

















asked Mar 21 at 17:05









aarish_codevaarish_codev

113




113












  • Have the Dependency object as a MockBean, and set the mock with mockito so that when someMethod is called on it, it will set some test value inside the array.

    – kumesana
    Mar 21 at 17:18











  • Use constructor injection instead of field injection. In your test, provide a mock Dependency. Mock the behaviour of dependency.someMethod to populate the array.

    – Ben R.
    Mar 21 at 17:25











  • I am already mocking the dependency and returning my custom List object from it using when(T).thenReturn(). But how to set the array[0] to a value I dont know.

    – aarish_codev
    Mar 21 at 17:44











  • You're basically asking why your test throws an exception, but you didn't post the exception stack trace, and you didn't post the code of your test...

    – JB Nizet
    Mar 22 at 11:23











  • @JBNizet I never asked why it throws exception.I already explained the reason for exception. 'IFF" you read the question carefully I don't think its hard to understand. Also look for the '?' sign while reading my question, you will know exactly what I'm asking. Thanks.

    – aarish_codev
    Mar 25 at 8:39

















  • Have the Dependency object as a MockBean, and set the mock with mockito so that when someMethod is called on it, it will set some test value inside the array.

    – kumesana
    Mar 21 at 17:18











  • Use constructor injection instead of field injection. In your test, provide a mock Dependency. Mock the behaviour of dependency.someMethod to populate the array.

    – Ben R.
    Mar 21 at 17:25











  • I am already mocking the dependency and returning my custom List object from it using when(T).thenReturn(). But how to set the array[0] to a value I dont know.

    – aarish_codev
    Mar 21 at 17:44











  • You're basically asking why your test throws an exception, but you didn't post the exception stack trace, and you didn't post the code of your test...

    – JB Nizet
    Mar 22 at 11:23











  • @JBNizet I never asked why it throws exception.I already explained the reason for exception. 'IFF" you read the question carefully I don't think its hard to understand. Also look for the '?' sign while reading my question, you will know exactly what I'm asking. Thanks.

    – aarish_codev
    Mar 25 at 8:39
















Have the Dependency object as a MockBean, and set the mock with mockito so that when someMethod is called on it, it will set some test value inside the array.

– kumesana
Mar 21 at 17:18





Have the Dependency object as a MockBean, and set the mock with mockito so that when someMethod is called on it, it will set some test value inside the array.

– kumesana
Mar 21 at 17:18













Use constructor injection instead of field injection. In your test, provide a mock Dependency. Mock the behaviour of dependency.someMethod to populate the array.

– Ben R.
Mar 21 at 17:25





Use constructor injection instead of field injection. In your test, provide a mock Dependency. Mock the behaviour of dependency.someMethod to populate the array.

– Ben R.
Mar 21 at 17:25













I am already mocking the dependency and returning my custom List object from it using when(T).thenReturn(). But how to set the array[0] to a value I dont know.

– aarish_codev
Mar 21 at 17:44





I am already mocking the dependency and returning my custom List object from it using when(T).thenReturn(). But how to set the array[0] to a value I dont know.

– aarish_codev
Mar 21 at 17:44













You're basically asking why your test throws an exception, but you didn't post the exception stack trace, and you didn't post the code of your test...

– JB Nizet
Mar 22 at 11:23





You're basically asking why your test throws an exception, but you didn't post the exception stack trace, and you didn't post the code of your test...

– JB Nizet
Mar 22 at 11:23













@JBNizet I never asked why it throws exception.I already explained the reason for exception. 'IFF" you read the question carefully I don't think its hard to understand. Also look for the '?' sign while reading my question, you will know exactly what I'm asking. Thanks.

– aarish_codev
Mar 25 at 8:39





@JBNizet I never asked why it throws exception.I already explained the reason for exception. 'IFF" you read the question carefully I don't think its hard to understand. Also look for the '?' sign while reading my question, you will know exactly what I'm asking. Thanks.

– aarish_codev
Mar 25 at 8:39












0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55285705%2fhow-can-i-access-member-variable-of-method-of-a-class-under-test%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55285705%2fhow-can-i-access-member-variable-of-method-of-a-class-under-test%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현