BDD: Cucumber Hello World Using Java and IntelliJ

Categories: Java; Tagged with: ; @ March 14th, 2015 16:42

Example:

Feature: Convert between different currencies
  Scenario: Convert SGD to CNY
    Given currency:"SGD", amount:10
    Given fxRate is 4.60
    When convert to "CNY"
    Then the result is 46 in CNY

JUnit Test: it can be generated by cucumber plugins, but not perfect for my IDE. (cannot generate all steps, but you still can copy the generated JUnit code from the output)

package com.liguoliang.bdd;

import cucumber.annotation.Before;
import cucumber.annotation.en.Given;
import cucumber.annotation.en.Then;
import cucumber.annotation.en.When;
import org.junit.Assert;

import java.math.BigDecimal;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
 * Created by guoliang on 3/14/15.
 */
public class FxConverterTest {

    private FxConverter fxConverter;

    @Before
    public void setUp() {
        fxConverter = new FxConverter();
    }


    @Given("^currency:\"([^\"]*)\", amount:(\\d+)$")
    public void currency_amount(String currency, BigDecimal amount) throws Throwable {
        fxConverter.setCurrencySource(currency);
        fxConverter.setAmountInput(amount);
    }

    @Given("^fxRate is (.+)$")
    public void fxRate_is_(BigDecimal rate) throws Throwable {
        fxConverter.setFxRate(rate);
    }

    @When("^convert to \"([^\"]*)\"$")
    public void convert_to(String currency) throws Throwable {
        fxConverter.convertTo(currency);

    }

    @Then("^the result is (.+) in CNY$")
    public void the_result_is_in_CNY(BigDecimal resultExpected) throws Throwable {
        assertTrue(resultExpected.compareTo(fxConverter.getAmountResult()) == 0);
    }

}

Java: calculate number complement using BitSet

Categories: Java; Tagged with: ; @ March 10th, 2015 20:15

Two’s complement is the way every computer I know of chooses to represent integers. To get the two’s complement negative notation of an integer, you write out the number in binary. You then invert the digits, and add one to the result.

www.cs.cornell.edu/~tomf/notes/cps104/twoscomp.html

    	Integer input = 50;
    	
    	String binString = Integer.toBinaryString(input);
    	BitSet bitSet = BitSet.valueOf(new long[] {input});
    	
    	for (int i = 0; i < binString.length(); i++) {
			bitSet.flip(i);
		}
    	
    	System.out.println(Arrays.toString(bitSet.toByteArray()));

infinitest: Brilliant Continuous Test Runner for Java

Categories: Java; Tagged with: ; @ November 24th, 2013 11:49

A Continuous Test Runner for Java

http://infinitest.org

 

When you changed any test case or java code which is covered by test cases, Infinitest will invoke the test case automatically for you!

image



// Proudly powered by Apache, PHP, MySQL, WordPress, Bootstrap, etc,.