My java class has 2 private methods, method1 and method2. method1 invokes method2. How can I test method1 at the same time mocking method2 using any of the java testing frameworks?  Edited question: I am using this syntax to invoke a private method: Whitebox.invokeMethod(myClass, "myPrivateMethod","myParameter");  "myPrivateMethod" invokes another private method: "anotherPrivateMethod". I would like to mock "anotherPrivateMethod". I usually do this using syntax: PowerMockito.doReturn("mockedResponse").when(myClass, "anotherPrivateMethod", "anyParameter");  The combination above does not seem to work. Basically, when I first use PowerMockito to mock "anotherPrivateMethod" and use Whitebox to invoke the "myPrivateMethod", the mock is not working. I am not getting "mockedResponse" but instead the actual "anotherPrivateMethod" is getting invoked. What am I doing wrong?
3346
How do I unit test (using xUnit) a class that has internal private methods, fields or nested classes? Or a function that is made private by having  (static in C/C++) or is in a private () namespace?  It seems bad to change the access modifier for a method or function just to be able to run a test.
18943
I'm using the Apache Commons EqualsBuilder to build the equals method for a non-static Java inner class. For example:  import org.apache.commons.lang.builder.EqualsBuilder;  public class Foo {     public class Bar {         private Bar() {}          public Foo getMyFoo() {             return Foo.this         }          private int myInt = 0;          public boolean equals(Object o) {             if (o == null || o.getClass() != getClass) return false;              Bar other = (Bar) o;             return new EqualsBuilder()                 .append(getMyFoo(), other.getMyFoo())                 .append(myInt, other.myInt)                 .isEquals();         }     }      public Bar createBar(...) {         //sensible implementation     }      public Bar createOtherBar(...) {         //another implementation     }      public boolean equals(Object o) {         //sensible equals implementation     } }   Is there syntax by which I can refer to other's Foo reference apart from declaring the getMyFoo() method? Something like other.Foo.this (which doesn't work)?
89434
I am calling, through reflection, a method which may cause an exception. How can I pass the exception to my caller without the wrapper reflection puts around it? I am rethrowing the InnerException, but this destroys the stack trace. Example code:  public void test1() {     // Throw an exception for testing purposes     throw new ArgumentException("test1"); }  void test2() {     try     {         MethodInfo mi = typeof(Program).GetMethod("test1");         mi.Invoke(this, null);     }     catch (TargetInvocationException tiex)     {         // Throw the new exception         throw tiex.InnerException;     } }
4097
Is it possible to call a constructor from another (within the same class, not from a subclass)? If yes how? And what could be the best way to call another constructor (if there are several ways to do it)?
60003
I've got a few methods that should call System.exit() on certain inputs. Unfortunately, testing these cases causes JUnit to terminate! Putting the method calls in a new Thread doesn't seem to help, since System.exit() terminates the JVM, not just the current thread. Are there any common patterns for dealing with this? For example, can I subsitute a stub for System.exit()?   [EDIT] The class in question is actually a command-line tool which I'm attempting to test inside JUnit. Maybe JUnit is simply not the right tool for the job? Suggestions for complementary regression testing tools are welcome (preferably something that integrates well with JUnit and EclEmma).
93585
My project assumes quite big integration with external webservices. Unfortunately first major service is SOAP and second is REST.  Problem is that there are getting more and more places in the code where for example SOAP callout needs some value from different REST callout. Putting this methods inside one wrapper is impossible for me to test since Apex is allowing only to mock one type of callout at the time (either REST or SOAP).  Question: How to create unit test with mocks for both callouts to achieve 100% code coverage of the wrapper method?
77222
I have a really simple Java class that effectively decorates a Map with input validation, with the obvious void set() and String get() methods.  I'd like to be able to effectively call those methods and handle return values and exceptions from outside the JVM, but still on the same machine Update: the caller I have in mind is not another JVM; thanks @Dave Ray  My implementation considerations are typical    performance ease of implementation and maintenance (simplicity?) reliability flexibility (i.e. can I call from a remote machine, etc.)   Is there a 'right way?' If not, what are my options, and what are the pro/cons for each?  (Stuff people have actually done and can provide real-life feedback on would be great!)
14927
Disclaimer Yes, I am fully aware that what I am asking about is totally stupid and that anyone who would wish to try such a thing in production code should be fired and/or shot.  I'm mainly looking to see if can be done. Now that that's out of the way, is there any way to access private class members in C++ from outside the class?  For example, is there any way to do this with pointer offsets? (Naive and otherwise non-production-ready techniques welcome) Update As noted in the comments, I asked this question because I wanted to write a blog post on over-encapsulation (and how it affects TDD).  I wanted to see if there was a way to say &quot;using private variables isn't a 100% reliable way to enforce encapsulation, even in C++.&quot;  At the end, I decided to focus more on how to solve the problem rather than why it's a problem, so I didn't feature some of the stuff brought up here as prominently as I had planned, but I still left a link. At any rate, if anyone's interested in how it came out, here it is:   (I suggest reading it before you decide that I'm crazy).
9792
Yesterday I had a two-hour technical phone interview (which I passed, woohoo!), but I completely muffed up the following question regarding dynamic binding in Java.  And it's doubly puzzling because I used to teach this concept to undergraduates when I was a TA a few years ago, so the prospect that I gave them misinformation is a little disturbing...  Here's the problem I was given:  /* What is the output of the following program? */  public class Test {    public boolean equals( Test other ) {     System.out.println( "Inside of Test.equals" );     return false;   }    public static void main( String [] args ) {     Object t1 = new Test();     Object t2 = new Test();     Test t3 = new Test();     Object o1 = new Object();      int count = 0;     System.out.println( count++ );// prints 0     t1.equals( t2 ) ;     System.out.println( count++ );// prints 1     t1.equals( t3 );     System.out.println( count++ );// prints 2     t3.equals( o1 );     System.out.println( count++ );// prints 3     t3.equals(t3);     System.out.println( count++ );// prints 4     t3.equals(t2);   } }   I asserted that the output should have been two separate print statements from within the overridden equals() method: at t1.equals(t3) and t3.equals(t3).  The latter case is obvious enough, and with the former case, even though t1 has a reference of type Object, it is instantiated as type Test, so dynamic binding should call the overridden form of the method.  Apparently not.  My interviewer encouraged me to run the program myself, and lo and behold, there was only a single output from the overridden method: at the line t3.equals(t3).  My question then is, why?  As I mentioned already, even though t1 is a reference of type Object (so static binding would invoke Object's equals() method), dynamic binding should take care of invoking the most specific version of the method based on the instantiated type of the reference.  What am I missing?
15717
I'd like to do something like this:  &gt;&gt; foo = @() functionCall1() functionCall2()   So that when I said:  &gt;&gt; foo()   It would execute functionCall1() and then execute functionCall2().  (I feel that I need something like the )  EDIT:  functionCall1 and functionCall2 are not necessarily functions that return values.
103121
I am attempting to mock a call to an indexed property. I.e. I would like to moq the following:  object result = myDictionaryCollection["SomeKeyValue"];   and also the setter value  myDictionaryCollection["SomeKeyValue"] = myNewValue;   I am doing this because I need to mock the functionality of a class my app uses.   Does anyone know how to do this with MOQ? I've tried variations on the following:  Dictionary&lt;string, object&gt; MyContainer = new Dictionary&lt;string, object&gt;(); mock.ExpectGet&lt;object&gt;( p =&gt; p[It.IsAny&lt;string&gt;()]).Returns(MyContainer[(string s)]);   But that doesn't compile.  Is what I am trying to achieve possible with MOQ, does anyone have any examples of how I can do this?
18843
If I have a for loop which is nested within another, how can I efficiently come out of both loops (inner and outer) in the quickest possible way?  I don't want to have to use a boolean and then have to say go to another method, but rather just to execute the first line of code after the outer loop.  What is a quick and nice way of going about this?  I was thinking that exceptions aren't cheap/should only be thrown in a truly exceptional condition etc. Hence I don't think this solution would be good from a performance perspective.  I don't feel it it is right to take advantage of the newer features in .NET (anon methods) to do something which is pretty fundamental.
43162
I have two different @wire methods that are calling Apex methods to get different results from two different objects.  After this information has been retrieved by each @wire respectively, I'd like to afterwards compare the results and set a few other attributes on the page.  How do I achieve this?  Should I use connectedCallback() or renderedCallback() or neither?  Is there a better way to do multiple Apex method calls then process them afterwards within the JS when the component loads?  I'm hoping there's a graceful way to accomplish this in LWC without having to chain Promises, or having to handle all of this within a single Apex method call.
8436
What is the best way to unit test a method that doesn't return anything? Specifically in c#.  What I am really trying to test is a method that takes a log file and parses it for specific strings. The strings are then inserted into a database. Nothing that hasn't been done before but being VERY new to TDD I am wondering if it is possible to test this or is it something that doesn't really get tested.
101466
I have the following situation I think would be best to show in sample program code.  I have a Java class that extends JPanel.  In this class are two objects which are two more JPanels.  In one of the JPanel objects is a JTable object.  I added a listener to this JTable that detects a double click.  When it detects a double click, I want to fire a method in the top class.  How do I reference this method in Java?  public class TopPanel extends JPanel {    JPanel OnePanel;   JPanel TwoPanel;    public void MethodToFire;  }  public class OnePanel extends JPanel {    JTable TheTable;  }  public class TheTable extends JTable {    public TheTable {     this.addMouseListener(new MouseAdapter(){       public void mouseClicked(MouseEvent e){           if (e.getClickCount() == 2){ SYNTAX CALLING THE METHOD IN TopPanel  }       }     } );   } }
108751
In the following setup, does method B run in a (new) transaction?  An EJB, having two methods, method A and method B  public class MyEJB implements SessionBean     public void methodA() {        doImportantStuff();        methodB();        doMoreImportantStuff();     }      public void methodB() {        doDatabaseThing();     } }   The EJB is container managed, with methodB in requires_new transaction, and method A in required transaction. thus:  &lt;container-transaction id="MethodTransaction_1178709616940"&gt;   &lt;method id="MethodElement_1178709616955"&gt;     &lt;ejb-name&gt;MyName&lt;/ejb-name&gt;     &lt;method-name&gt;*&lt;/method-name&gt;   &lt;trans-attribute&gt;Required&lt;/trans-attribute&gt;   &lt;/method&gt;   &lt;method id="MethodElement_1178709616971"&gt;     &lt;ejb-name&gt;MyName&lt;/ejb-name&gt;     &lt;method-name&gt;methodB&lt;/method-name&gt;   &lt;/method&gt;   &lt;trans-attribute&gt;RequiresNew&lt;/trans-attribute&gt; &lt;/container-transaction&gt;   Now let another EJB call methodA with an EJB method call. methodA now runs in an transaction. Will the subsequent call to methodB from methodA run in the same transaction, or does it run in a new transaction? (mind, it's the actual code here. There is no explicit ejb-call to method B)
101443
Does anyone have any advice for a consistent way to unit test a multithreaded application?  I have done one application where our mock "worker threads" had a thread.sleep with a time that was specified by a public member variable.  We would use this so we could set how long a particular thread would take to complete its work, then we could do our assertions.  Any ideas of a better way to do this?  Any good mock frameworks for .Net that can handle this?