JMockit With a Static, a Thread, and a Singleton

Working on a legacy Logging class that uses Thread and a custom Log class singleton (that doesn’t even extend slf4j.Logger, but uses it. ummm . . . future refactor); the task was givern to me to test it. Looked in the pom and saw JMockit AND Mockito referenced, and no PowerMock. There were very few tests, less than 1% for the codebase and neither mock libraries had been implemented in any tests. Knowing there were a lot of final and static classes and other nasty code in this old java app JMockit seemed a good choice because Mockito won’t test these without Powermock.

The CustomLogger class in question, that has Thread and the Log singleton was something like this:


public final class CustomLogger {
	public static void logStack()
	{
		try
		{
			Map traces = Thread.getAllStackTraces();
			printStack(traces);
		}
		catch(Error e)  
		{
			//<-- HEADACHE!!!! catches junit assertion errors, 
			// and all stays green even when things are red
		}
	}

	public static String toString(String name, StackTraceElement[] trace)
	{
		StringBuffer buffer = new StringBuffer();
		buffer.append("Thread "+name);
		buffer.append("\n");
		for(StackTraceElement element:trace)
		{
			buffer.append("\tat "+element);
			buffer.append("\n");
		}
		return buffer.toString();
	}

	private static void printStack(Map traces)
	{
		StringBuffer buffer = new StringBuffer();
		for(Thread thread:traces.keySet())
		{
			buffer.append(toString(thread.getName(), traces.get(thread)));
		}
		Log.getInstance().error(buffer.toString()); // the singleton instance
	}
}

A LOT of things going on in here. I decided to mock the Thread and also the underlaying Log singleton. Things get a little complicated. On the Log mock I used the validate() technique — but of course there was a caveat: logStack() has an empty catch block. This means that method catches any JUnit failures. I needed a way around it so I decided to throw an actual high level Exception with a message for a failed condition.

I wrote the tests with JUnit/JMockit on this as such:


public class CustomLoggerTest
{

	private final StackTraceElement[] traces = new StackTraceElement[2];

	private final Map stackTraces = new HashMap();

	private Thread localThread;

	@Before
	public void setUp() throws Exception
	{
		traces[0] = new StackTraceElement("TestClass34", "testMethod34", "testFile34", 34);
		traces[1] = new StackTraceElement("TestClass51", "testMethod51", "testFile51", 51);

		Thread localThread = new Thread();

		stackTraces.put(localThread, traces);
	}

	@After
	public void tearDown() throws Exception
	{
		localThread = null;
	}

	@Test
	public void testLogStack()
	{
		new Expectations()
		{
			@Mocked({ "getAllStackTraces" })
			final Thread unused = null;
			{
				Thread.getAllStackTraces();
				result = stackTraces;
			}
		};

		new Expectations()
		{
			Log log;
			{

				Log.getInstance();  //<-- mocking the singleton
				returns(log);
				log.error(anyString);
				times = 1;
				forEachInvocation = new Object()
				{
					void validate(Invocation inv, String buffer) throws Exception
					{	
						String expected =
								"Thread Thread-1\n\tat TestClass34.testMethod34(testFile34:34)\n\tat " + "TestClass51.testMethod51(testFile51:51)\n";
						System.out.println(expected);	
						System.out.println(buffer);		
					
						if (!expected.equalsIgnoreCase(buffer)) {
							//Assert.fail();  //can't work due to error trapping in method!
							throw new Exception("Assertion Error: Log Output Not As Expected");
						}
					}
				};
			}
		};
		CustomLogger.logStack(); //<-- Custom Logger is static
	}

}

Side note: the test coverage in this article isn't complete this is just the most interesting stuff.

Now about that empty empty catch block; that thing cause me some head scratching. Everytime it went through the assert would throw but the test wouldn't report as RED in a fail situation. The class can't fail properly because it catches a failed assertEquals. The mechanism in JUnit throws a java.lang.AssertionError or a junit.framework.AssertionFailedError -- trapped by the empty catch block. The best thing to do would be to fix the code (haha), but those weren't the marching orders. Please don't get me started.

This goes to show you -- when you are writing codde do NOT ever make *any* assumptions about seemingly harmless methods. You can get caught inheriting a methoud you may never think gets called (like an equals or hash method_ and it does.

Now about JMockit. It solved my problem, but I find the documentation not too friendly and the syntax is barely readable to me. I much, much prefer Mockito. I am not sure what I think about a mixed JMockit/Mockito application; I made the decision not to do so on that code base. But in the future might rethink that -- unit tests should be terse and readable in my opinion, not so cleaver as to require a lot of refactoring. Self contained per each test is possible, with few harnasses if possible. This makes things like onboarding and code maintenance MUCH easier. Who cares, as long as the tests run quickly and they give you meaningful, readable coverage? They are, after all, the business requirements especially in TDD is a shop's chosen method.

brAAAA(GILE)AAAins!

Hmmm, Brain Chemistry Driven Development.  Are you doing it?  Why not?

Check this out on InfoQ, neuroscience agile leadership —

http://www.infoq.com/articles/neuroscience-agile-leadership

It’s a brain chemistry treatise on agile leadership. The author has a “Certificate in NeuroLeadership.”  I researched this Yet Another Certification — but this one is really getting out there.  The theory of this article is that you can manage people by understanding their neurobiology, and use it to make them physiologically accept change.

Talks about prefrontal cortex, limbic systems, dopamine receptors, the amygdala. All the things you hear during scrum of scrum of scrum of scrums, or SCOSCOSCOs, as they call it now.

Bit of a background: I used to do door to door cognitive science for the visual system, that is, for a while I was a graduate student in visual cognitive psychology. One of the departments I didn’t like to hang around was one where they drilled a hole into rats skulls to glue in a cannula to drip dopamine onto their brains. One day a scientitst picked a rat up by a brain cannula and I was off to developmental studies for good.

In Agile NeuroScrumming — since we are all addicted to our own lives, we need a NSCM (neuro-scrum master) to take employees through a period of withdrawal and break the cycle of dependency on one’s own independence.

Now this. My god, Watson, now I think its only a matter of time before the PM blokes install a brain chemistry changing machine for the projects . . . !!!!

I’m going to be honest. Agile has went a direction I never thought possible. I mean, like TOO metric. Like TOO “process, not people.” Here are some quotes from this article:

“We can start where they are at (employees in the danger/reward contiuum), and design our interactions to minimize the feelings of danger and maximize feelings of reward. “

“As leaders, we must be patient and know that achieving a mindset shift in our people – essentially rewiring the brain to create new habits – requires clearing the path and creating a safe environment that allows a shift to take place; where people have the overview and feel in control of their work.”

FEEL in control of their work?  Because in reality they aren’t?  Been there.  Rewire our brains?  That’s an Agile manager’s job?  WTF is that about.  Seriously, in this area of specialization how many c-panel types or managers in general have it *so* together that they know how to do a person’s task list.   How many of them have it together in general?  None of my managers for my last contracts dating back 15 years could do one thing I could do.  Now they are going to change my brain for me.  Maybe while they are at it they can pick a new religion for me or rewire me to eat more chia seeds.  Don’t underestimate the power of chia seeds.

I checked in the code and surprisingly to me, the build broke because of an integration test failure caused by unannounced changes in database configurations on the QA server.  White coated PMs and team leads were dispatched immediately and administered high doses of zoloft to workers of neuroscrum cell 7-2521.  The configuration manager fared much worse, and was removed to receive several treatments of shock adjustment therapy.

Maybe, and it’s just my humble opinion, but this is just another approach to assign “all of the responsibility, none of the blame.”  Sure there are salient points in the article– like make a work environment “safe to fail.”  But seriously, will a manager in that author’s world do a CYA polka faster than you can say “let’s revert to Windows 7” when a project fails?    Don’t get me wrong, I’m not writing off neuromanagement for running the local Pizza Hut.  Heck, you can get drug tests at the dollar general, Kroger runs infrared cameras in their stores, why not run some bodily invasive stress detectors on your employees?

Contrast all this to a Joel Spolsky’s treatise on Microsoft (ownership) vs Juno (people bailing left and right):

At Microsoft, management was extremely hands-off. In general, everybody was given some area to own, and they owned it. 

At Juno, quite the opposite was the case. Nobody at Juno owned anything, they just worked on it, and different layers of management happily stuck their finger into every pie, giving orders left and right in a style which I started calling hit and run management because managers tended to pop up unannounced, give some silly order for exactly how they wanted something done, dammit, without giving any thought to the matter, and leave the room for everyone else to pick up the pieces. 

And Joel’s conclusion:

PaxDigita Culture

So this is why I’m concerned with creating the right culture of hands-off management at PaxDigita. In general:

  • everybody owns some area. When they own it, they own it. If a manager, or anybody else, wants to provide input into how that area is managed, they have to convince the owner. The owner has final say.
  • every decision is made by the person with the most information.
  • management is extremely flat. Ideally, managers just don’t have time to get their fingers in the pies of their reports. You may be interested to read about a GE plant in North Carolina that has 170 employees who all report directly to the plant manager.

I’m not sure how the great brain machine get’s along with his lesson.

Maybe someday, an Agile manager can be merely seen as a kind of thought-dairy farmer.   They will come in, rub iodine all over the employee-resource head organs and hook up cerebellum milking devices.  Now that’s lean baby.

Seriously, what are we really trying to accomplish with all this?

Quick Script for Switching Maven Settings,
Or Any Settings

Sometimes I’m on several projects with differing and complicated configs  for Maven, and use a few different Eclipse/IDE installs to manage all the preferences between the projects.  With many different leads can be different Check Style files, PMD rules sets, formatting rules, naming conventions etc.  Why not just the same standards universally?  Well, different projects need different things and have different people.

For just the Maven environments, here is a simple Windows batch script I use to switch up the settings.  Assuming there are two complicated settings.xml files, it prompts for either and copies onto the file name “settings.xml” in your .M2 directory.

@echo off &setlocal

:COMMANDLOOP
echo.
echo 1 = Maven settings for Project 1
echo 2 = Maven settings for Project 2
set "TEMPCMD=%CD%"
set /P "TEMPCMD=%CD% :"

IF "%TEMPCMD%"=="1" (
 del settings.xml
 copy settings-1.xml settings.xml
 ECHO "settings for Project 1 complete"
) ELSE If "%TEMPCMD%"=="2" (
 del settings.xml
 copy settings-2.xml settings.xml
 ECHO "settings for Project 2 complete"
) ELSE (
 echo "Please enter 1 or 2. Try again."
 GOTO COMMANDLOOP
)

pause

Getting a Time Quickly From Java Calendar

We have an old legacy DB that stores time as a string.  No timezones etc.  Here’s a quick solution:

Threading some queries

I set up a quick multi-thread query test for a complex model entity made of several hibernate table entities. What I was trying to accomplish was a faster retrieval by running the queries in parallel. Spring JPARepository is the underlying querying mechanism. Here’s the higher level entity:

/*
* LookupEntity is a composite pojo of TableX instances,
* TableX instances are just any jpa/hibernate entities
* that share the same id (for simplicity)
*/
public LookupEntity {
private Integer id;
private Table1 table1;
private Table2 table2;
private Table3 table3;

public LookupEntity(){}

public Table1 getTable1() {return table1;}
public void setTable1(Table1 table1) {this.table1 = table1;}

public Table2 getTable2() {return table2;}
public void setTable2(Table2 table1) {this.table2 = table2;}

public Table3 getTable3() {return table3;}
public void setTable3(Table3 table3) {this.table3 = table3;}
}

In my service method is the threading test. I wrote a quick @TimeMethod annotation and point cut to time the method:

/*
* getLookupThreaded will run TableX repository methods to populate
* the TableX objects in a LookupEntity
*/
@TimeMethod
public LookupEntity getLookupThreaded(final Integer id) {
final LookupEntity lookupEntity = new LookupEntity(id);

Runnable r1 = new Runnable() {
public void run() {
lookupEntity.setTable1(table1Repository.findOne(id));
};
};
Runnable r2 = new Runnable() {
public void run() {
lookupEntity.setTable2(table2Repository.findOne(id));
};
};
Runnable r3 = new Runnable() {
public void run() {
lookupEntity.setTable3(table2Repository.findOne(id));
};
};

Thread t1 = new Thread(r1);
Thread t2 = new Thread(r2);
Thread t3 = new Thread(r3);

t1.start();
t2.start();
t3.start();

return lookupEntity;
}

This worked pretty well but would still need more scaling management with the threads if a server was going to hit the method 10k times. But what I found was more weaknesses in my table designs and indexing as opposed to coding deficiencies after turning on jpa and hibernate logging. Personally, I avoid threading and non injection container singletons like the plague unless absolutely necessary because they introduce, many times, unnecessary complexity. But if I were to write some low connection count analysis procedures; maybe doing big data style calculations in a job, I might consider threading to save time.

Eclipse: Different Maven settings.xml

Well it happened — a third party project team decided they’d configure a complicated settings.xml file, which resides in the .m2 folder on your local.  But I already have projects that don’t like what they wrote in that file, so I had to create two:

  • settings-common.xml for our projects
  • settings-thirdparty.xml for their projects

Now when I open a workspace, I just configure the Maven plugin in Eclipse to point at the correct settings file:

Of course, to run from the command line you should specify the proper settings file as a parameter:

C:\<pom directory> mvn install --settings c:\User\.m2\settings-common.xml

Java Long and Integer Max

If ever you forget, just pop open a groovy console and get the max values for Java’s Long and Integer objects:

I use the Groovy console quite a bit.

Reusable UI Widgets . . . ?

About reusable UI.  I have a hard time believing it.

The backend seems much simpler to me; conducive to patterns, more easily solvedd in some ways for representing domains.  The complexity of the back and middle ware IMHO comes in with engineering, capability, scalability and new technologies.

But UI?  Oh my my my . . .

Interesting that there are only a few MVCs and persistence frameworks to choose from, comparatively.  But a billion calednar widgets.

Here’s a library of GWT widget extensions, one of many.

http://www.gwt-ext.com/demo/

Here’s the PrimeFaces Showcase:

http://www.primefaces.org/showcase/ui/home.jsf

Some JS/HTML5 Widgets, they haven’t adapted the word “showcase” for Javascript yet (that I’ve seen), but they will:

http://wijmo.com/demo/explore/

Flex/Flash. Of course Adobe. They’ve made 15 trillion of each type of widget and still no one likes Flash. Imagine — the platform destroyed its goal — useability:

http://www.adobe.com/cfusion/exchange/index.cfm?l=-1&o=desc&cat=186&event=productHome&s=5&exc=15

Flash has soooo many private sites full of widgets:

http://www.coolwidgetsgadgets.com/

So, there are for instance a billion calendar widgets.  And every single gig I had to do UI for, not one of them was good enough for what the business wanted.  Barely a one.

On almost every gig I did UI on, a few things happened:

1. Copy-paste UI code. The biggest nightmare, usually due top-down developers who didn’t have programming fundamentals (like a BA who became a “developer.”) Write 100 times, deploy 100 times. Maintenance hell.
2. Inversion of control nightmares — usually from hardcore backend/administrator types who think the command line solves everything. One widget that does everything — and very brittle at that in the end. More points of entry, higher cyclomatic index, more likelihood to break.
3.Happy medium — one widget does 80% of the lifting, write custom ones the other 20%. I think this is the best we could do with UX.

That happy medium is the *craftsmanship* — picking the point of scale or pattern.  It’s not something easily learned or taught; it may take some experience to figure out when a customer needs 1000 calendar widgets . . . or one.  Or even to convince them they need just one.

———————-

On the good side of this, think of all those engineers with this knowledge of UX, developing and honing these controls.  Makes one wonder — is this a search for a perfect implementation of a control, or for us to come to terms with the limitations of what a calendar control, for instance, might be?

A Decent Mockito.verify Example

I always get confused as to when it would be a good time to use verify. I also see verify thrown into tests for no reason at all, except maybe as a practice to remember the syntax. I had written a Spring validation class and a test, and since Errors is an interface and is more easily used in tests as a mock (IMHO) I thought it a good spot to do a verify for the test instead of the usual assertsEquals or whatever.

The rule is, for an object add, the objectId must be null or else I return a validation error.

The calling Spring (3.2) controller method:


@ApiOperation(value = “add new object”)

@RequestMapping(method = RequestMethod.PUT)

public
@ResponseBody

ObjectAdd add(@Valid
@RequestBody Object object) {

    return objectService.add(object);

}

Here’s my validation class. What it does is ensure there wasn’t an ID submitted to my PUT method – help the UI people out a little I know that get’s confusing. Also, if we make our rest API public this will help and . . it’s just good practice.

The test we’ll write is on this class, to confirm it is checking for a null ID value. We’ll confirm that the Error object method either get’s called, or doesn’t, depending on if the ID is null or not (remember we are in ADD mode so we do not want an ID).

@Component(“objectValidator”)

public
class ObjectValidator implements Validator {


@Value(“${object.Id}”)

private String idName;


@Value(“${object.add.empty.id}”)

private String idError;


@SuppressWarnings(“rawtypes”)


@Override

public
boolean supports(Class clazz) {

    return Object.class.isAssignableFrom(clazz);

}


@Override

    public
void validate(Object target, Errors errors) {

        Object object = (Object) target;

        if (null != object.getObjectId()) {

         errors.rejectValue(idName, idError);

        }

    }

}

Here’s the pojo, Jacksonfied because it comes in via a Controller and @Valid is used on the @Request:

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.WRAPPER_OBJECT)

@JsonTypeName(“object”)

public
class Object

{

    @JsonProperty

    private Integer objectId;

    @JsonProperty

    private String objectText;

    @JsonProperty

    private Date createDate;

    public Object()

    {

        // no-arg constructor for Jackson

    }

    public Integer getObjectId()

    {

        return objectId;

    }

    public
void setObjectId(Integer objectId)

    {

        this.objectId = objectId;

    }

    public String getObjectText()

    {

        return objectText;

    }

    public void setObjectText (String objectText)

    {

        this.objectText = objectText;

    }

    public Date getCreateDate()

    {

        return createDate;

    }

    public void setCreateDate(Date createDate)

    {

        this.createDate = createDate;

    }

}

And here’s my unit test with verification:

public
class ObjectValidatorTest {


@InjectMocks

private ObjectValidator objectValidator = new ObjectValidator();

@Mock

private Errors mockErrors;


@Before

public
void setUp() throws Exception {

    MockitoAnnotations.initMocks(this);

}


@After

public
void tearDown() throws Exception {

}


@Test

public
void testValidate() {

    Object object = new Object();

    object.setObjectId(null);

    objectValidator.validate(object, mockErrors);

    verify(mockErrors, times(0)).rejectValue(anyString(),anyString());

    

    object.setObjectId(44);

    objectValidator.validate(object, mockErrors);

    verify(mockErrors, times(1)).rejectValue(anyString(),anyString());

}


@Test

public
void testSupports() {

    assertTrue(objectValidator.supports(Object.class));

    assertFalse(objectValidator.supports(String.class));

}


I think this makes a lot of sense. We aren’t testing Errors itself, just if our validation methods logic calls Errors or ignores it in the correct situations.

One thing to force ourselves to really think about when writing mock tests is “what are we really testing.” A lot of times I get errors, forgetting “ah, yeah even that service had to be mocked.” Verification can help us test our class that has a mock component.

Spring MVC beat me down with a 404

Before I get started on this, here is the solution I chose for setting up a 404 error trap in a Spring Web MVC (Spring 3.2). It may seem old school, but after hours of research and testing this is what works the best for my application.

Solution

In web.xml add something like this, you r location could be an html page but mine is an error controller endpoint:

<error-page>

        <error-code>404</error-code>

        <location>/error/404</location>

</error-page>

My error controller looks like this, in my case I return an error object that has a status and message property that gets deserialized to json:

@Api(description = “Error Controller to catch errors not covered by ControllerAdvice.”, value = “/error”)

@Controller

@RequestMapping(value = “/error”, produces = MediaType.APPLICATION_JSON_VALUE)

public
class ErrorController {

@RequestMapping(value=“/404”)


@ResponseStatus(HttpStatus.NOT_FOUND)


@ApiOperation(value = “404 Error Handler”)


public
@ResponseBody ControllerResponse unmappedRequest(HttpServletResponse response) throws IOException {

    return
new ControllerResponse.Builder().status(ControllerResponseEnum.error).message(HttpStatus.NOT_FOUND.getReasonPhrase()).build();

}

}

ControllerReponse is just a simple json pojo

@JsonInclude(Include.NON_NULL)

public
class ControllerResponse {

@JsonProperty

@JsonFormat(shape = JsonFormat.Shape.STRING)

private ControllerResponseEnum status;


@JsonProperty

private String data;


@JsonProperty

private String meta;


@JsonProperty

private String message;

…. Plus getters/setters etc. . . .

ControllerResponseEnum simply lists the values suggested in the JSend spec: success, fail, or error.

public
enum ControllerResponseEnum {

    success, fail, error

}

Things That Didn’t Work For Me – 406’s everywhere

Special controller mapped to /** didn’t work

At first I thought I could just create an error controller and catch everything coming in at /** by adding a request mapping and throw the error from there, centralizing my error responses. This works great like this . . . (but . . . )


@RequestMapping(value=“/**”)

@ResponseStatus(HttpStatus.NOT_FOUND)

@ApiOperation(value = “404 Error Handler”)

public
@ResponseBody ControllerResponse unmappedRequest(HttpServletResponse response) throws IOException {

The good thing is – it intercepts EVERYTHING. The bad thing is – it intercepts EVERYTHING. So I ran into problems with my static resource files. There’s a schema and some ui on the path needed for the ReST application:

    <mvc:resources mapping=“/static/**” location=“/static/” />

But the “/**” controller wreaks havoc. I tried everything under the sou to get this to work: writing redirects in the error controller for instance, some fancy web.xml default servlet mappings. Nothing. It throws out 406 errors (NOT ACCEPTABLE error).

Interceptors require too much code to maintain, comparatively to the solution I needed

Even tried configuring an intercept filter and handle the code there, but I started to write waaaaaay tooo much.

    <mvc:interceptors>

        <mvc:interceptor>

            <mvc:mapping path=“/**” />

            <bean class=“com.ist.common.error.StaticContentInterceptor”/>

        </mvc:interceptor>

    </mvc:interceptors>

Or try it with java config file:

@Configuration

@EnableWebMvc

public
class StaticContentConfig extends WebMvcConfigurerAdapter {

//@formatter:off

@Override

public
void addInterceptors(InterceptorRegistry registry) {

    registry.addInterceptor(new
StaticContentInterceptor())

        .addPathPatterns(“/**”) }

//@formatter:on

}

Nope.

MIME type mappings – might as well just do <error> config if you are messing with web.xml:

How about mounting every freakin static mime type . . . .

<servlet-mapping>

<servlet-name>MyApp</servlet-name>

<url-pattern>/</url-pattern>

</servlet-mapping>

<!– The ‘dynamic’ content –>

<servlet-mapping>

<servlet-name>default</servlet-name>

<url-pattern>*.css</url-pattern>

</servlet-mapping>

<servlet-mapping>

<servlet-name>default</servlet-name>

<url-pattern>*.js</url-pattern>

</servlet-mapping>

<servlet-mapping>

<servlet-name>default</servlet-name>

<url-pattern>*.jpg</url-pattern>

</servlet-mapping>
<!– The ‘static’ content –>

The 404 isn’t a Spring MVC issue. The error is scoped outside of the framework. By the time you get there, you’ve passed up a lot of other functionality. Here are some key conversations about the issue that had me settle on what I put in:

https://jira.springsource.org/browse/SPR-8837

http://stackoverflow.com/questions/11792231/spring-mvc-catch-http-errors-400-404

So I guess what burned me is that a 404 is a basic error of doing web applications, and I just don’t like having more than one way to do things if I can help it. I handle 500 errors via the framework, so why not 404’s? The solution makes sense for now, as the simplest. Of course you could write a log of code in the interceptor or controller /** method, but that (IMHO) defeats the purpose of convention over a coded configuration. Since I was left to configuration, I just chose the simplest path.