IntelliJ IDEA: Create JUnit Test method code snippet shortcut

I’ve been using IntelliJ over Eclipse for a while now, mainly because it feels newer and more tuned to develop with Maven-based projects. This is just me. Anyway, if you do TDD, you’ll want to take advantage of IntelliJ’s Live Templates.

In this post, I’m writing about adding JUnit test code snippets, like so:


@Test
public void testSalutationMessage() {
    System.out.println("Inside testSalutationMessage()");
    message = "Hi," + "Anton" + "!";
    assertEquals(message, util.salutationMessage());
}

You could copy-and-paste an existing method and modify it, type it manually each time, or just type “test” and hit the Tab key and, voila, it’s there! I bet you’d choose the latter…to do this in IntelliJ:

1. Go to Preferences -> Live Templates
2. Click the ‘+’ sign on the right (close to Reset)
3. Add the following:


Abbreviation: 
test

Description: 
JUnit Test Method (or whatever you like)

Template Text:
@Test
public void test$NAME$() throws Exception {
    $BODY$
}

4. Leave “Shorten FQ names” checked
5. Click on “Change” and check “Java”
6. Apply and try it out (i.e. typing “test” and hitting the Tab key should add your code snippet)

Hope this helps! Till the next. 🙂

IntelliJ IDEA: Getting “Fetch failed. Fatal: Could not read from remote repository”

In case you encounter this issue, do the following:

1. Preferences > SSH
2. Make sure SSH executable is set to “Native.” (If already so, switch to “Built-in,” apply it, then switch back to “Native.”)
3. Happy fetching!

Git: Create a duplicate branch of master

I worked on upgrading a web service from JDK 1.6 to JDK 1.8 and was successful (that’s something I need to write a post on); however, I wanted to make sure I could revert back to pre-1.8 in case it breaks in production. The solution was to create a clone or duplicate of master before I merged the branch.

This would then allow me to merge that branch if needed. Here’s how:


git checkout -b java6-branch origin/master

And then, it needs to be pushed so others can access it:


git push origin java6-branch

That’s it. You can double-check by logging into your Git site. 🙂

P.S. Best practice is to name your branches in lowercase and in groupings (i.e. foo/bar1, foo/bar2).

ActiveMQ error: “java.lang.RuntimeException: javax.jms.JMSException: Invalid version: 6, could not load org.apache.activemq.openwire.v6.MarshallerFactory”

I was working on a project that needed to send JMS messages to an Apache ActiveMQ broker that was running an older version, specifically, 5.4.x. It started failing with the error below when I switched to it, but had no issues when it was connecting to another broker running 5.9.0.

java.lang.RuntimeException: javax.jms.JMSException: Invalid version: 6, could not load org.apache.activemq.openwire.v6.MarshallerFactory

Note: It could be another version.

I googled to see what the problem was and read that it had to do with a version mismatch between my client and broker. Turned out that I was using 5.9.0, which is why it didn’t conk out with the other broker. Unfortunately, I didn’t have control of the current broker running 5.4.x, so it was back to the drawing board.

Thankfully, I found that ActiveMQ has a legacy dependency that you can use to resolve the issue. Simply add the following and you’ll be good to go:


<dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-openwire-legacy</artifactId>
    <version>5.9.0</version>
</dependency>