Junit test function which returns a string

Tee Jay

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?

TuyenNTA

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);
  }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

JUnit POST @Test returns empty string

How to test function which returns function with parameters?

How to test a function which returns a promise $q?

Implementing a function which returns function definition as string

How to test a function which returns void in google test?

pinvoke to function which returns string array

Unable to compile function which returns parts of a string

Function which returns a TABLE after splitting a string

Junit: What is the best way to check which test is testing a function in Intellij?

How to test a function which returns anonymous function in jest?

Trying to test a function in Jasmine which returns `expected a spy but got a function`

Test a function in Python which merges files and returns nothing

How to test a function which returns something and has side effect?

Jasmine, AngularJS: Unit test a function which returns a value after timeout

How do I test a function which returns a complex object?

How to unit test a function which calls another that returns a promise?

How to test node.js module which returns anonymous function?

How to write Unit test for a method which returns anonymous function

TypeScript: function that returns a generic Type which extends Record<string, string>

JUnit test which use void

Test if a function name includes string, if the function returns an observable

function which returns true is the string is in upper-case using XSLT

A function that returns a string which resemples a playing board in python

User defined function returns value error which should be string

Create a function which takes in a float as input and returns a string containing a number

How to unit test (using Jasmine) a function in a controller which calls a factory service which returns a promise

How to test a function that returns a string, but has no inputs in Java

Function which returns function scheme

Test function that returns an observable

TOP Ranking

HotTag

Archive