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?
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.
Comments