SpringBoot: Making use of @Value properties from YAML files for Cucumber tests

In case you use:
1. SpringBoot to build your web applications using Java
2. Test it with Cucumber
3. Use YAML files for configurable property values

You can use the YamlPropertiesFactoryBean to make use of @Value for your test configuration.

application.yml:


myapp:
  base:
    url: some_url

cucumber.xml (or application context .xml):


<context:component-scan base-package="cucumber.runtime.java.spring"/>
    <context:annotation-config/>

    <bean id="yamlProperties"
          class="org.springframework.beans.factory.config.YamlPropertiesFactoryBean">
        <property name="resources" value="classpath:application.yml"/>
    </bean>

    <context:property-placeholder properties-ref="yamlProperties"/>

Java code (snipped):


@ContextConfiguration(locations = {"classpath:cucumber.xml"})
public class TestSteps {

    @Value("${myapp.base.url}")
    private String baseUrl;
...

The spring bean, yamlProperties, will take care of all the necessary wiring, so all you have to do is make use of the @Value annotation. The benefit of this is you can create properties for various environments (in your YAML file) and be able to automate running your tests against any environment using spring profiles.

Hope this helps.

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).