Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Junit test function which returns a string

I have a function inside a class :

public String covertToLowerCase(String sliceName) {
        sliceName = sliceName.trim().toLowerCase();
        sliceName = sliceName.replaceAll("\\.txt$|\\.dat$", "");
        return sliceName;
    }

I want to test this using Junit. I have created a separate test file which has the following:

  public class MyControllerTest {

  private MyController myController;
  private static final String SLICE_NAME = "Hello World";

  @Test
  public void shouldReturnSanitizedString() throws Exception {
  String expected = myController.covertToLowerCase(SLICE_NAME);
  // assert that actual and expected are same
  }

I am not able to understand how to test this and all the other examples are specific to their functions. I just want the function to return a sanitized string? How can I go about this?

like image 785
Tee Jay Avatar asked Oct 23 '25 18:10

Tee Jay


1 Answers

You want to test, first you have to prepare data to test, the input value, the expected value => call test function with input value => get the actual value return by function under test => assert the expected value with the actual value. Here is a list of assert function that you can use.

public class MyControllerTest {

  private MyController myController;
  private final String SLICE_NAME = "Hello World";
  private final String expected = "hello world";

  @Test
  public void shouldReturnSanitizedString() throws Exception {
  String actual = myController.covertToLowerCase(SLICE_NAME);
  // assert that actual and expected are same
  assertEquals(expected, actual);
  }
}
like image 53
TuyenNTA Avatar answered Oct 26 '25 07:10

TuyenNTA



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!