Question

This is a sample program:

public class FunctionalTest {
    public int f(int r) {
        int result = r * 5;
        return result;
    }
    public static void main(String[] args) {
        FunctionalTest funct = new FunctionalTest();
        System.out.println(funct.f(5));
    }
}

I'm a beginner. How to write a functional test for this code? How to write functional tests? Do I need to TestNG? Is it enough to write the examination method? Could someone explain to me and write a sample functional test for this program?

Was it helpful?

Solution 2

First of all, you need to have a clear definition of contract you want to verify. From the code, I assume it is something like "the method should return the number equal to argument multiplied by 5".

TestNG, JUnit or other test frameworks is not mandatory for your case. The test may look like:

public void testF() {
    int arg = 5;

    int result = new FunctionalTest().f(arg);

    assert result == arg * 5;
}

Also please keep in mind that to use assert you need JVM started with -ea flag.

OTHER TIPS

Well, if you're specifically asking for functional testing, there's not much you can do with that code snippet. You can do a unit test from the f method using JUnit like this:

@Test
public void testF(){
FunctionalTest t1 = new FunctionalTest();
    assertEquals((t1.f(1) % 5), 0); //checks that is getting multiplied by 5.
}

However, you want functional testing, so by running your compiled app and assessing the result you're testing your app functionality by multiple units (AKA integration): your f method and your main method.

Regards!

Beware the terms you used:

  • the functional testing provide values to your user/customer

That implies:

  • black box testing: you have to test your whole system (hard+soft)
  • the test should target your user/customer needs (explicit report or test)

You can use whatever you want to test the feature (from unit test to jbehave).

In your case (using JUnit 4 and AssertJ):

import org.assertj.core.api.Assertions;
import org.junit.Test;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

/*
As an user
I want have 25 whatever I sent
*/
public class NumberGenerationTest {

  private static final String PATH = "directory of your class file";
  private InputStream stdout;

  /* Nominal case */
  @Test
  public void shall_return_number_25_when_called_with_5() throws Exception {
    when_I_call_FunctionalTest_with("5");
    then_it_returns("25");
  }
  /* Nominal case or potential error case */
  @Test
  public void shall_return_number_25_when_called_with_10() throws Exception {
    when_I_call_FunctionalTest_with("10");
    then_it_returns("25");
  }

  /* Nominal case or potential error case */
  @Test
  public void shall_return_number_25_when_called_with_ABC() throws Exception {
    when_I_call_FunctionalTest_with("ABC");
    then_it_returns("25");
  }

  private void when_I_call_FunctionalTest_with(String parameter) throws Exception {
    ProcessBuilder builder = new ProcessBuilder("java" ,"-classpath", PATH,"FunctionalTest" , parameter);
    builder.redirectErrorStream(true);
    Process process = builder.start();
    stdout = process.getInputStream ();

  }

  private void then_it_returns(String expectedResult) throws Exception {
    BufferedReader reader = new BufferedReader (new InputStreamReader(stdout));
    String line = reader.readLine ();
    Assertions.assertThat(line).isNotNull();
    Assertions.assertThat(line).isEqualTo(expectedResult);
  }

}

It seems you have an error in your main() ... or not.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top