Welcome to my website. I am always posting links to photo albums, art, technology and other creations. Everything that you will see on my numerous personal sites is powered by the formVistaTM Website Management Engine.


  • Mocking Static Methods That Return void in Java
    05/11/2017 9:29AM
    This is one of those things that I tend to do on a regular basis . . . but unfortunately don't remember the details each time, so I am adding it for future reference.

    Often, developers will want to mock static methods that return void.  The Mockito and PowerMockito frameworks provide for this, but the syntax isn't immediately obvious.

    Following is an example.

    public class SomeClass {
        public static void doSomething(String arg1, int arg2) {
            // Method that does something...

    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.mockito.Mockito;
    import org.powermock.api.mockito.PowerMockito;
    import org.powermock.core.classloader.annotations.PowerMockIgnore;
    import org.powermock.core.classloader.annotations.PrepareForTest;
    import org.powermock.modules.junit4.PowerMockRunner;

     * The RunWith and PrepareForTest annotations are following annotations are
     * necessary to mock the static methods in the SomeClass class. The RunWith
     * enables the class to be run via PowerMock, and the PrepareForTest is an array
     * of the classes with static members that we want to mock.
     * The PowerMockIgnore annotation tells PowerMock to defer the loading of
     * classes with the names supplied to the system classloader.  This will vary
     * depending on the dependency tree that you are using/testing.  It is also
     * not necessary, but here for example purposes.
    @PrepareForTest({ SomeClass.class })
    public class SomeTestClass {

        public void shouldDoSomethingExpected() throws Exception {

            // Set up the SomeClass's static members for mocking

            // Configure the mock for the method in question.
            // The following syntax is what is key here
                .when(SomeClass.class, "doSomething", Mockito.anyString(), Mockito.anyInt());

  • Solution for Executing Native Process from Java that Requires sudo
    12/22/2016 9:52AM

    If you are building a Java program that requires the ability to execute native commands on the machine which require sudo it requires some additional considerations other than just writing the Java code.

    The problem is that sudo, by default, requires a tty for executing sudo such that a password can entered.  Even if you configure sudoers to grant NOPASSWD access to a specific command you will still get the following error

    sudo: sorry, you must have a tty to run sudo

    In my case, I was writing a set of integration tests in Java that needed to be able to start and stop a service to run a test.

    I settled on adding an additional sudoers config file in /etc/sudoers.d.  This ended up be the cleanest and most encapsulated change that did not then require any special considerations in the Java code.

    The change simply involved adding a file with the following contents to /etc/sudoers.d which indicates that running sudo for the rchapin user does NOT require a tty and then grants access to the specific commands.

    Defaults:rchapin !requiretty
    rchapin ALL=(root) NOPASSWD: /bin/systemctl stop rabbitmq-server.service
    rchapin ALL=(root) NOPASSWD: /bin/systemctl start rabbitmq-server.service

  • Java PowerMock Could not reconfigure JMX java.lang.LinkageError Solution
    08/09/2016 9:00AM

    If you are using Mockito and PowerMock to build mocks for your Java tests and you run into the following error:

    2016-05-05 17:31:20,204 main ERROR Could not reconfigure JMX java.lang.LinkageError: loader constraint violation: loader (instance of org/powermock/core/classloader/MockClassLoader) previously initiated loading for a different type with name "javax/management/MBeanServer"
            at java.lang.ClassLoader.defineClass1(Native Method)
            at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
            at org.powermock.core.classloader.MockClassLoader.loadUnmockedClass(MockClassLoader.java:238)
            at org.powermock.core.classloader.MockClassLoader.loadModifiedClass(MockClassLoader.java:182)
            at org.powermock.core.classloader.DeferSupportingClassLoader.loadClass(DeferSupportingClassLoader.java:70)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

    Simply add the following as a class level annotation:

    @PowerMockIgnore( {"javax.management.*"})

  • Using the Eclipse Memory Analyzer (MAT) Remotely on Large Heap Dumps
    11/23/2015 10:42AM

    Sometime your java applilcation will fail and generate an enormous heap dump.  One that may be too large to be able to transfer to your local machine and to analyze for lack of RAM, time or both.

    One solution is to install the MAT tool on the remote server and generate an HTML output of the analysis to download and view locally.  This saves the headache of attempting to get X Windows installed on the remote machine and get all of the ssh tunneling sorted out (which is of course an option as well).

    First, download and install the stand-alone Eclipse RCP Application.  Then transfer to your server and unpack.  Then determine how large the heap dump is and, if necessary, modify the MemoryAnalyzer.ini file to instantiate a JVM with enough RAM for your heap dump.

    In this example, I have an 11GB heap dump and have modified the last two lines (adding -Xms)


    Do an initial run to parse the heap dump.  This will generate intermediary data that can be used by subsequent runs to make future analysis faster.

    ./ParseHeapDump.sh /path/to/heap-dump

    After that completes, you can run any of a number of different analysis on the data.  The following is an illustration of how to search for memory leak suspects.

    ./ParseHeapDump.sh /path/to/heap-dump org.eclipse.mat.api:suspects

    Additional reports:


    To give creadit where it is due, this is basically a copy of a post by Ashwin Jayaprakash, but I wanted to capture it here as well.

  • JVM Option for Increasing the Default Number of Lines in the StackTrace
    09/11/2015 2:27PM

    By default (Java 1.6 or greater), the JVM will output, at most, 1024 lines of the stack trace.

    In the situation where you have some recursion problem or some infinite loop that results in a stack overflow error you will need to increase this value with a JVM option to see the origin of your crash.

    To do so, add the following option to the java command

    $ java -XX:MaxJavaStackTraceDepth=-1 -jar some.jar some.package.Class  etc, etc,

    -1 indicates no limit.  Any positive integer indicates the limit to the number of lines in the stack trace.  0 means exactly what it means and will output 0 lines.

    A great resource for java options.

  • 1 2 3 4 5 6 7 8 9 10 11 >>
Advanced Search