Junit test function which returns a string

15,643

Solution 1

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

Solution 2

For the record, more "real world" tests would rather look like:

public class MyControllerTest {
  private MyController underTest = new MyController();

  @Test(expected=NullPointerException.class)
  public void testConvertToLowerCaseWithNull() {
    underTest.convertToLowerCase(null);
  } 

The above is just an example - your method could decide for example to throw an IllegalArgumentException instead. And you want to make sure that your production code does actually throw an exception for invalid cases.

  @Test
  public void testConvertToLowerCaseWithEmptyInput() {
    assertThat(underTest.convertToLowerCase(""), is(""));
  } 

I recommend using assertThat(), together with hamcrest matchers - as the resulting code is simply easier to read and understand.

And then you continue adding more testcases. You step back, and you think upfront about the things to test here. You want to make sure that "e" stays "e", that "E" turns into "e" ... and so on.

Other things here:

  • especially when doing such input/output-only testing --- do not use fields/constants in your test method. You want that your tests are self contained as much as possible. In order to understand my test - you just need to look into the method body. Your approach requires you to lookup what those constants actually contain!
  • be precise: there is absolutely no need to have throws Exception on your test method. It doesn't throw - so the signature shouldn't announce that!
  • bad naming in your production code. Your method doesn't only turn into lower case. It also replaces content. The method name should express that. As of now, that method name is misleading. That is the worst thing your code can do: mislead the reader into believing what code does, but doing more/something else instead!
Share:
15,643
Tee Jay
Author by

Tee Jay

Dreamer- YES Coder- Work in Progress

Updated on June 04, 2022

Comments

  • Tee Jay
    Tee Jay almost 2 years

    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?

  • Tee Jay
    Tee Jay almost 7 years
    If I want to test some other function : public String createSqlFromSchema(List<MapSchema> mapSchemas) { String template = "%s %s"; List<String> colList = mapSchemas.stream().map(m -> String.format(template, m.getColName(), m.getDataType())) .collect(Collectors.toList()); return String.join("\n", colList); } . What do I have to provide as a parameter in the test function? When I log the current parameter, it shows a reference and not the actual value.
  • GhostCat
    GhostCat almost 7 years
    @TeeJay When you have a new question, ask a new question. Dont get into "more questions in comments" ping pong. Besides: do you think anybody wants to read source code written up in comments?
  • TuyenNTA
    TuyenNTA almost 7 years
    @TeeJay please take noted @GhostCat's comment. Most importance thing is you need to learn about junit before working with it. For your question, you need to test for function createSqlFromSchema(List<MapSchema> mapSchemas) the input of this function must be a List<MapSchema>, let's initialize your list and pass to the test function. More things, you want to get value of a list, then go through one by one to get the value.