Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Cucumber differ from JUnit?

Tags:

junit

cucumber

Can anyone explain the difference to me between Cucumber and Junit

From my understanding they are both used to test Java code although I am not sure of the difference?

Are they simply difference implementations of the same test suite or aimed at testing different things?

like image 444
RK . N Avatar asked Oct 15 '14 11:10

RK . N


People also ask

What is difference between JUnit and Cucumber?

The difference is that while JUnit aims at tests, Cucumber aims at collaboration with non technical people. Non technical people will not understand what a unit test does. They will, however, be able to understand and validate an example written in Gherkin.

Is Cucumber a JUnit framework?

Cucumber uses Junit framework to run. If you are not familiar with JUnit read our tutorials here. As Cucumber uses Junit we need to have a Test Runner class. This class will use the Junit annotation @RunWith(), which tells JUnit what is the test runner class.

Can we use Cucumber without JUnit?

Cucumber is just another custom Junit runner. so can not have cucumber without Junit.

What is difference between unit testing and JUnit?

It supports the test to run by writing and testing along. JUnit framework was initially based on the SUnit framework which is used for Unit testing but then later it was updated with Java using Selenium WebDriver. JUnit is now is used as a standard when we need to perform testing in Java.


2 Answers

Cucumber and JUnit are different and solve different things.

Cucumber is a Behavior Driven Design (BDD) framework that takes "stories" or scenarios written in human readable languages such as English and turns those human readable text into a software test.

here's an Example cucumber story:

Example Cucumber Test in plain English

cucumber will then knows how to turn this text into a software test to make sure the software works as described. The output will tell you if the story is actually what the software does and if not, what was different:

 failed test

Here's where the code is fixed to make the cucumber test pass: passed test

This makes what is called an "Executable Specification" which is a nice way of documenting all of the features your software supports. This is different than normal documentation because without the corresponding test, someone reading the document doesn't know if the documentation is up to date.

Other Benefits of Executable Specifications:

  • Non-programmers can read and understand the tests
  • Non-programmers can write the tests since they are in plain English.

BDD results and Executable Specifications are very high level. They cover the overall features and perhaps a few edge cases as examples but don't test every possible condition or every code path. Also BDD tests are "integration tests" in that they test how all your code modules work together, but they don't test everything thoroughly.

This is where JUnit comes in.

JUnit is a lower level "Unit test" tool that allows developers to test every possible code path in their code. Each module of your code (or classes, or even methods) is tested in isolation. It is much more low level than a BDD framework. Using the same calculator story as the Cucumber example, JUnit tests would test lots of different calculation examples and invalid inputs to make sure the program responds correctly and computes the values correctly.

Hope that helps

like image 185
dkatzel Avatar answered Oct 06 '22 00:10

dkatzel


I think Cucumber is more used for integration tests, while JUnit is more used in behaviour tests instead. Besides, Cucumber syntax is more accurate than JUnit, but much more complex. Here you can see a Cucumber test example:

package com.c0deattack.cucumberjvmtutorial;

import cucumber.annotation.en.Given;
import cucumber.annotation.en.Then;
import cucumber.annotation.en.When;
import cucumber.runtime.PendingException;

public class DepositStepDefinitions {

    @Given("^a User has no money in their account$")
    public void a_User_has_no_money_in_their_current_account() {
        User user = new User();
        Account account = new Account();
        user.setAccount(account);
    }

    @When("^£(\\d+) is deposited in to the account$")
    public void £_is_deposited_in_to_the_account(int arg1) {
        // Express the Regexp above with the code you wish you had
        throw new PendingException();
    }

    @Then("^the balance should be £(\\d+)$")
    public void the_balance_should_be_£(int arg1) {
        // Express the Regexp above with the code you wish you had
        throw new PendingException();
    }

    private class User {
        private Account account;

        public void setAccount(Account account) {
            this.account = account;
        }
    }

    private class Account {
    }
}

You can see that JUnit is more simple, but not necessarily less powerful:

import static org.junit.Assert.assertEquals;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

public class MyClassTest {

  @Test(expected = IllegalArgumentException.class)
  public void testExceptionIsThrown() {
    MyClass tester = new MyClass();
    tester.multiply(1000, 5);
  }

  @Test
  public void testMultiply() {
    MyClass tester = new MyClass();
    assertEquals("10 x 5 must be 50", 50, tester.multiply(10, 5));
  }
} 

Hope it helps,

Clemencio Morales Lucas.

like image 34
Clemencio Morales Lucas Avatar answered Oct 06 '22 00:10

Clemencio Morales Lucas