/* WP-Cache Config Sample File See wp-cache.php for author details. */ $wpsc_last_post_update = 1557541754; //Added by WP-Cache Manager $wpsc_save_headers = 0; //Added by WP-Cache Manager $wp_cache_home_path = '/'; //Added by WP-Cache Manager $cache_schedule_interval = 'daily'; //Added by WP-Cache Manager $cache_gc_email_me = 0; //Added by WP-Cache Manager $cache_scheduled_time = '00:00'; //Added by WP-Cache Manager $wp_cache_mobile_groups = ''; //Added by WP-Cache Manager $wp_cache_mobile_prefixes = 'w3c , w3c-, acs-, alav, alca, amoi, audi, avan, benq, bird, blac, blaz, brew, cell, cldc, cmd-, dang, doco, eric, hipt, htc_, inno, ipaq, ipod, jigs, kddi, keji, leno, lg-c, lg-d, lg-g, lge-, lg/u, maui, maxo, midp, mits, mmef, mobi, mot-, moto, mwbp, nec-, newt, noki, palm, pana, pant, phil, play, port, prox, qwap, sage, sams, sany, sch-, sec-, send, seri, sgh-, shar, sie-, siem, smal, smar, sony, sph-, symb, t-mo, teli, tim-, tosh, tsm-, upg1, upsi, vk-v, voda, wap-, wapa, wapi, wapp, wapr, webc, winw, winw, xda , xda-'; //Added by WP-Cache Manager $wp_cache_refresh_single_only = 0; //Added by WP-Cache Manager $wp_cache_make_known_anon = 0; //Added by WP-Cache Manager $wp_cache_mod_rewrite = 0; //Added by WP-Cache Manager $wp_cache_front_page_checks = 0; //Added by WP-Cache Manager $wp_cache_mfunc_enabled = 0; //Added by WP-Cache Manager $wp_supercache_304 = 0; //Added by WP-Cache Manager $wp_cache_no_cache_for_get = 0; //Added by WP-Cache Manager $wp_cache_disable_utf8 = 0; //Added by WP-Cache Manager $cache_time_interval = '600'; //Added by WP-Cache Manager $cache_schedule_type = 'time'; //Added by WP-Cache Manager $cache_page_secret = '5b9836c88039e7acde90683f929191c4'; //Added by WP-Cache Manager $wp_cache_slash_check = 1; //Added by WP-Cache Manager if ( ! defined('WPCACHEHOME') ) define( 'WPCACHEHOME', WP_CONTENT_DIR . "/plugins/wp-super-cache/" ); //Added by WP-Cache Manager $cache_compression = 1; //Added by WP-Cache Manager $cache_enabled = true; //Added by WP-Cache Manager $super_cache_enabled = true; //Added by WP-Cache Manager $cache_max_time = '1800'; //Added by WP-Cache Manager //$use_flock = true; // Set it true or false if you know what to use $cache_path = WP_CONTENT_DIR . '/cache/'; $file_prefix = 'wp-cache-'; $ossdlcdn = 0; // Array of files that have 'wp-' but should still be cached $cache_acceptable_files = array( 'wp-comments-popup.php', 'wp-links-opml.php', 'wp-locations.php' ); $cache_rejected_uri = array('wp-.*\\.php', 'index\\.php'); $cache_rejected_user_agent = array ( 0 => 'bot', 1 => 'ia_archive', 2 => 'slurp', 3 => 'crawl', 4 => 'spider', 5 => 'Yandex' ); $cache_rebuild_files = 1; //Added by WP-Cache Manager // Disable the file locking system. // If you are experiencing problems with clearing or creating cache files // uncommenting this may help. $wp_cache_mutex_disabled = 1; //Added by WP-Cache Manager // Just modify it if you have conflicts with semaphores $sem_id = 2046492738; //Added by WP-Cache Manager if ( '/' != substr($cache_path, -1)) { $cache_path .= '/'; } $wp_cache_mobile = 0; $wp_cache_mobile_whitelist = 'Stand Alone/QNws'; $wp_cache_mobile_browsers = '2.0 MMP, 240x320, 400X240, AvantGo, BlackBerry, Blazer, Cellphone, Danger, DoCoMo, Elaine/3.0, EudoraWeb, Googlebot-Mobile, hiptop, IEMobile, KYOCERA/WX310K, LG/U990, MIDP-2., MMEF20, MOT-V, NetFront, Newt, Nintendo Wii, Nitro, Nokia, Opera Mini, Palm, PlayStation Portable, portalmmm, Proxinet, ProxiNet, SHARP-TQ-GX10, SHG-i900, Small, SonyEricsson, Symbian OS, SymbianOS, TS21i-10, UP.Browser, UP.Link, webOS, Windows CE, WinWAP, YahooSeeker/M1A1-R2D2, iPhone, iPod, Android, BlackBerry9530, LG-TU915 Obigo, LGE VX, webOS, Nokia5800'; //Added by WP-Cache Manager // change to relocate the supercache plugins directory $wp_cache_plugins_dir = WPCACHEHOME . 'plugins'; // set to 1 to do garbage collection during normal process shutdown instead of wp-cron $wp_cache_shutdown_gc = 0; $wp_super_cache_late_init = 0; //Added by WP-Cache Manager // uncomment the next line to enable advanced debugging features $wp_super_cache_advanced_debug = 0; $wp_super_cache_front_page_text = ''; $wp_super_cache_front_page_clear = 0; $wp_super_cache_front_page_check = 0; $wp_super_cache_front_page_notification = '0'; $wp_cache_object_cache = 0; //Added by WP-Cache Manager $wp_cache_anon_only = 0; $wp_supercache_cache_list = 0; //Added by WP-Cache Manager $wp_cache_debug_to_file = 0; $wp_super_cache_debug = 0; $wp_cache_debug_level = 5; $wp_cache_debug_ip = ''; $wp_cache_debug_log = ''; $wp_cache_debug_email = ''; $wp_cache_pages[ "search" ] = 0; $wp_cache_pages[ "feed" ] = 0; $wp_cache_pages[ "category" ] = 0; $wp_cache_pages[ "home" ] = 0; $wp_cache_pages[ "frontpage" ] = 0; $wp_cache_pages[ "tag" ] = 0; $wp_cache_pages[ "archives" ] = 0; $wp_cache_pages[ "pages" ] = 0; $wp_cache_pages[ "single" ] = 0; $wp_cache_pages[ "author" ] = 0; $wp_cache_hide_donation = 0; $wp_cache_not_logged_in = 0; //Added by WP-Cache Manager $wp_cache_clear_on_post_edit = 0; //Added by WP-Cache Manager $wp_cache_hello_world = 0; //Added by WP-Cache Manager $wp_cache_mobile_enabled = 1; //Added by WP-Cache Manager $wp_cache_cron_check = 1; //Added by WP-Cache Manager ?> /* WP-Cache Config Sample File See wp-cache.php for author details. */ $wpsc_last_post_update = 1557541754; //Added by WP-Cache Manager $wpsc_save_headers = 0; //Added by WP-Cache Manager $wp_cache_home_path = '/'; //Added by WP-Cache Manager $cache_schedule_interval = 'daily'; //Added by WP-Cache Manager $cache_gc_email_me = 0; //Added by WP-Cache Manager $cache_scheduled_time = '00:00'; //Added by WP-Cache Manager $wp_cache_mobile_groups = ''; //Added by WP-Cache Manager $wp_cache_mobile_prefixes = 'w3c , w3c-, acs-, alav, alca, amoi, audi, avan, benq, bird, blac, blaz, brew, cell, cldc, cmd-, dang, doco, eric, hipt, htc_, inno, ipaq, ipod, jigs, kddi, keji, leno, lg-c, lg-d, lg-g, lge-, lg/u, maui, maxo, midp, mits, mmef, mobi, mot-, moto, mwbp, nec-, newt, noki, palm, pana, pant, phil, play, port, prox, qwap, sage, sams, sany, sch-, sec-, send, seri, sgh-, shar, sie-, siem, smal, smar, sony, sph-, symb, t-mo, teli, tim-, tosh, tsm-, upg1, upsi, vk-v, voda, wap-, wapa, wapi, wapp, wapr, webc, winw, winw, xda , xda-'; //Added by WP-Cache Manager $wp_cache_refresh_single_only = 0; //Added by WP-Cache Manager $wp_cache_make_known_anon = 0; //Added by WP-Cache Manager $wp_cache_mod_rewrite = 0; //Added by WP-Cache Manager $wp_cache_front_page_checks = 0; //Added by WP-Cache Manager $wp_cache_mfunc_enabled = 0; //Added by WP-Cache Manager $wp_supercache_304 = 0; //Added by WP-Cache Manager $wp_cache_no_cache_for_get = 0; //Added by WP-Cache Manager $wp_cache_disable_utf8 = 0; //Added by WP-Cache Manager $cache_time_interval = '600'; //Added by WP-Cache Manager $cache_schedule_type = 'time'; //Added by WP-Cache Manager $cache_page_secret = '5b9836c88039e7acde90683f929191c4'; //Added by WP-Cache Manager $wp_cache_slash_check = 1; //Added by WP-Cache Manager if ( ! defined('WPCACHEHOME') ) define( 'WPCACHEHOME', WP_CONTENT_DIR . "/plugins/wp-super-cache/" ); //Added by WP-Cache Manager $cache_compression = 1; //Added by WP-Cache Manager $cache_enabled = true; //Added by WP-Cache Manager $super_cache_enabled = true; //Added by WP-Cache Manager $cache_max_time = '1800'; //Added by WP-Cache Manager //$use_flock = true; // Set it true or false if you know what to use $cache_path = WP_CONTENT_DIR . '/cache/'; $file_prefix = 'wp-cache-'; $ossdlcdn = 0; // Array of files that have 'wp-' but should still be cached $cache_acceptable_files = array( 'wp-comments-popup.php', 'wp-links-opml.php', 'wp-locations.php' ); $cache_rejected_uri = array('wp-.*\\.php', 'index\\.php'); $cache_rejected_user_agent = array ( 0 => 'bot', 1 => 'ia_archive', 2 => 'slurp', 3 => 'crawl', 4 => 'spider', 5 => 'Yandex' ); $cache_rebuild_files = 1; //Added by WP-Cache Manager // Disable the file locking system. // If you are experiencing problems with clearing or creating cache files // uncommenting this may help. $wp_cache_mutex_disabled = 1; //Added by WP-Cache Manager // Just modify it if you have conflicts with semaphores $sem_id = 2046492738; //Added by WP-Cache Manager if ( '/' != substr($cache_path, -1)) { $cache_path .= '/'; } $wp_cache_mobile = 0; $wp_cache_mobile_whitelist = 'Stand Alone/QNws'; $wp_cache_mobile_browsers = '2.0 MMP, 240x320, 400X240, AvantGo, BlackBerry, Blazer, Cellphone, Danger, DoCoMo, Elaine/3.0, EudoraWeb, Googlebot-Mobile, hiptop, IEMobile, KYOCERA/WX310K, LG/U990, MIDP-2., MMEF20, MOT-V, NetFront, Newt, Nintendo Wii, Nitro, Nokia, Opera Mini, Palm, PlayStation Portable, portalmmm, Proxinet, ProxiNet, SHARP-TQ-GX10, SHG-i900, Small, SonyEricsson, Symbian OS, SymbianOS, TS21i-10, UP.Browser, UP.Link, webOS, Windows CE, WinWAP, YahooSeeker/M1A1-R2D2, iPhone, iPod, Android, BlackBerry9530, LG-TU915 Obigo, LGE VX, webOS, Nokia5800'; //Added by WP-Cache Manager // change to relocate the supercache plugins directory $wp_cache_plugins_dir = WPCACHEHOME . 'plugins'; // set to 1 to do garbage collection during normal process shutdown instead of wp-cron $wp_cache_shutdown_gc = 0; $wp_super_cache_late_init = 0; //Added by WP-Cache Manager // uncomment the next line to enable advanced debugging features $wp_super_cache_advanced_debug = 0; $wp_super_cache_front_page_text = ''; $wp_super_cache_front_page_clear = 0; $wp_super_cache_front_page_check = 0; $wp_super_cache_front_page_notification = '0'; $wp_cache_object_cache = 0; //Added by WP-Cache Manager $wp_cache_anon_only = 0; $wp_supercache_cache_list = 0; //Added by WP-Cache Manager $wp_cache_debug_to_file = 0; $wp_super_cache_debug = 0; $wp_cache_debug_level = 5; $wp_cache_debug_ip = ''; $wp_cache_debug_log = ''; $wp_cache_debug_email = ''; $wp_cache_pages[ "search" ] = 0; $wp_cache_pages[ "feed" ] = 0; $wp_cache_pages[ "category" ] = 0; $wp_cache_pages[ "home" ] = 0; $wp_cache_pages[ "frontpage" ] = 0; $wp_cache_pages[ "tag" ] = 0; $wp_cache_pages[ "archives" ] = 0; $wp_cache_pages[ "pages" ] = 0; $wp_cache_pages[ "single" ] = 0; $wp_cache_pages[ "author" ] = 0; $wp_cache_hide_donation = 0; $wp_cache_not_logged_in = 0; //Added by WP-Cache Manager $wp_cache_clear_on_post_edit = 0; //Added by WP-Cache Manager $wp_cache_hello_world = 0; //Added by WP-Cache Manager $wp_cache_mobile_enabled = 1; //Added by WP-Cache Manager $wp_cache_cron_check = 1; //Added by WP-Cache Manager ?> BDD with JBehave: Setup and basic tests | Danis Blog BDD with JBehave: Setup and basic tests – Danis Blog
29 Oct

BDD with JBehave: Setup and basic tests

In an earlier article series I showed how to use Mockito together with JUnit in order to write good unit tests. While this tool is good for Test Driven Development (TDD), it falls short when it comes to Behavior Driven Development (BDD). In this article, I’m going to introduce another tool that has caught my attention and leverages your testing facilities to make use of the BDD-paradigms. But first, let’s see what we mean when we talk about BDD.

Preface

There are a lot of posts and tutorials about JBehave, but none have satisfied my expectations so far. If you’ve never heard of JBehave before, it might be a bit hard to start out as a complete beginner, since most of the magic is happening under the hood and not transparent to the user.

A very good introduction to JBehave can be found in this blog post on Codecentric.com. However, this post takes you only to a certain point, without covering the full capabilities of JBehave. This article series is intended to pick up where Codecentric.com left off.

All code to the sample project built in this tutorial is available in my GitHub repository.

A short introduction into BDD and JBehave

Behavior Driven Development (or BDD in short) is often referred to as the successor of Test Driven Development (TDD) and should strip away all technical details and special knowlege of how code should be tested. It does so by defining communication as central means of how collaborators should define what the system should do and what the expected result is. This also means that language becomes universally important in BDD, since not all collaborators have the same knowledge of technologies, tools and processes. BDD uses a domain-specific language (DSL) to describe behaviors of and expectations from a system.

JBehave is the link between this DSL and Java by enabling software developers to incorporate expected behavior in to their test suite and run them automatically – similar to Unit Tests but based on tests formulated in natural language. The description of expected behaviour can (or should) even come from a non-technical person with extensive knowledge of the problem domain.

Going into details of the principles of BDD and technical details JBehave would be fairly out of scope of this article series. There is a vast number of articles on BDD – read the introductory article by Dan North or google it if you want to know more. Also, JBehave is extensiveley documented. What this article series tries to do is to give you a general idea of how BDD tests can be written using JBehave and get you started as quickly as possible.

Getting started with JBehave

Ok, now it’s time to get our hands dirty. Let’s build our first project which is tested following the BDD approach. For this setup we will use the following setup:

If you’re using another IDE, you may have to undertake additional steps. I know that there’s a JBehave plugin for Eclipse, too. However, this tutorial does only cover setup with IntelliJ.

We will use a  very simple example as use case for our tests. We will write Java code which implements a digital radio. Of course we will not have a real radio (i.e. you won’t hear any sound coming out of your speakers), since we only need some code to run our test against.

The radio The radio will need a power source in order to play and should have some basic functionalities, such as:

  • playing music
  • selecting a station
  • saving a station
  • showing the currently selected station
  • … and so on

The radio will be implemented in a single class Radio.java, which will be tested using BDD principles. This means we will define the radio’s behaviour first, before adding any logic to the class.

Include JBehave as a dependency

Setup your project with Maven (or directly in the IDE). Of course, in order to use the functionalities of JBehave, we must add JBehave as a dependency:

Note I have also included a dependency to Hamcrest in order to use their really useful matchers. This should be enough for now to write our first JBehave test.

Our first JBehave test

JBehave needs three things to run a test:

  • a story, which will describe the expected behavior of the test candidate in a user friendly manner. The story is contained in a *.story file.
  • a test, which contains the steps to execute the described behavior. This test is usually just a Java file with some special annotations.
  • a mapping between the story and the steps, so that JBehave knows what steps to execute for what parts of the story. This is usually a class containing a configuration to automate the test. The class can be executed as a JUnit test and output results in various formats.

The story

Let’s start with the most simple use case.  Let’s say I have a digital radio, which I want to turn on to listen to music. The expected behavior could be decribed as follows:

Easy to read, huh? This is not a free text. It is acutally code being executed by JBehave! That’s the good thing about BDD: Since the behavior is language agnostic, expectations can be formulated almost entirely in natural language. You only have to follow a basic syntax pattern with the following keywords:

  • Given: Decribe the starting position for the behavior (preconditions)
  • When: Describe what is happening in the test
  • Then: Describe the expected behavior

Of course there’s more and the descriptions can also be parameterized. But this should be enough for us to take in for now.

The Steps

As said, JBehave needs the steps corresponding with the story description. This can be done using a POJO with some JBehave-annotated methods:

As you can see, the annotations carry the same namings like keywords described above. It is important that the string passed in as an argument matches the text following the keyword.

The more perceptive of you might have noted that we’re still missing a radio that contains the actual behavior to be tested, so let’ts add that, too:

 

The Mapping

Now, in order for JBehave to know what steps to execute for what parts of the story, we must provide it with some basic configuration. I won’t dig into the details of what else could be configured, but this should be enough for the moment.

What this code does is tell JBehave the classpath to load stories from and write the results to the console in text format.

All set

Now let’s try it out. Right click on the mapping class (TurnRadioOn.java) and run it as a Unit Test. You should get the following output:

java.lang.AssertionError
 at org.junit.Assert.fail(Assert.java:86)
 at org.junit.Assert.assertTrue(Assert.java:41)
 at org.junit.Assert.assertTrue(Assert.java:52)
 at info.tiefenauer.tutorials.jbehave.RadioSteps.theRadioShouldBeTurnedOn(RadioSteps.java:30)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
...

Uh-oh. Our test has failed. But that’s actually a good thing, since we can fix it now and will immediately know whether our fix has produced the expected behavior. So let’s complete the switchOnOff-Method to simulate the radio’s behavior:

Let’s run our test again and we should see the following test result:

Good to know

If you’re (like me) keeping your stories and other test-related files in the test folder created by maven, you can do so but you need to tell Maven to also load your story files. By default, maven does not copy .story files to the classpath unless you explicitly tell it so. This can be achieved by adding additional information to the build step:

Also, JBehave will run all your stories as a single unit test by default. if you want the results of the individual tests to be shown separately and neatly like in the screenshot above, you should add a dependency to a custom test runner like the one below:

After that you can update your test file to be run with the custom test runner:

@RunWith(JUnitReportingRunner.class)
public class TurnRadioOn extends JUnitStory {
...
}

Congratulations!

You have just run your first BDD test using JBehave. But wait, there’s more! Let’s see how we can extend our BDD-tests in order so test behavior in a more sophisticated manner.

  • Setup with Maven

If you’re very lazy and you dont want to go over all the boilerplate code of including dependencies, creating story files, steps and mapping class, you can use one of the Maven archetypes provided by JBehave. To do this simply choose the archetype when generating the project structure. For example, if you use the command line, you could just run

mvn archetype:generate -Dfilter=org.jbehave:jbehave

to generate your project in interactive mode and then select the jbehave-simple-archetype , provide your project details and then hit enter to generate a simple maven project (including a sample story and steps) that can be imported into the IDE of your choice.

For this tutorial, we’re doing the setup by hand, to help you understand the inner workings and prerequisites needed for JBehave.

Series NavigationBDD with JBehave: Advanced tests >>

2 thoughts on “BDD with JBehave: Setup and basic tests

  1. Hello,
    I’m just starting using JBehave with IntelliJ. But I got a problem, I can’t run a story.
    I got the following error:
    “In order to run a story file you need to first set a main class in the JBehave settings.”

    And indeed in the setting ‘JBehave Support’ pluging ask for the current project to choose the main class for running stories. I assume i must put the class where there are my senarios, yet it refuse all my class as unaceptable.

    I comment in hope you have an idea of what my problem is, and in the off chance that you will see this.
    Thanks

  2. Hi Victor
    Sorry for the late reply. Do you have a project on GitHub that I can check out and verify the issue? Or could you resolve the problem meanwhile?

Leave a Reply

Your email address will not be published. Required fields are marked *