How do you write the simple UnitTests to the simplest functions?



  • I want to learn to write tests for my projects.

    Say some good resources to learn to test Android applications.

    How important is their use?

    I'm writing an annex without their use, and I can't assess their usefulness.

    The codes that I meet online don't remember seeing any tests in the code.

    Anyway, I want to know what that means. Tell me where to start.

    Let's say there's a method like this:

    private File getFile(File path) {
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        return new File(path.getPath() + File.separator + timeStamp + ".html");
    }
    

    How do you write a test for him and what do you need to do?

    RIGHTS

    By the way, there's a link with the video showing the example of the test.

    https://www.youtube.com/watch?v=ZJE0MDKJOow



  • To create a test, you first need to determine what you're going to test. Ideally, your method has to do something one thing and then your task will be simplified. If possible, the test should be tested as a black box, so you're doing something on the entrance and checking the value. Unfortunately, it's not always possible.

    In your particular case, the first thing that goes into the eye is that the method is marked. privatesuch a method cannot be tested by standard methods. There's a lot of theories about whether to test private methods or not. My opinion is they can't be tested. The correctness of private practice will not be verified when you test all open methods. Although if you really want to, you can tag the method like 'default' or you can use reflection.

    But your method is public, then we understand the code that the method is returning a new file with the name built from the road and the function of the current date. I mean, he's doing two things: ♪ Creates the file name. ♪ Create a file ♪ You can test it now, but you're not interested. I'd advise you to adjust your code a little so that it's more suitable for testing:

    public File getFileFullName(File path, Date date) {
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String fullPath = path.getPath() + File.separator + timeStamp + ".html";
        return fullPath;
    }
    

    // где-то в вашем коде
    String path = ...
    Date now = new Date();
    String fileFullName = getFileFullName(path, now);
    File file = new File(fileFullName);

    // тест для метода getFileFullName в классе ClassUnderTest
    @Test
    public void testGetFileFullName() {
    String path = "/abc";
    Date date = new Date(1465953124); //2016-06-15 13:12:06

    ClassUnderTest instance = new ClassUnderTest()
    String name = instance.getFileFullName(path, date);
    
    assertEquals("/abc/20160615_131206.html");
    

    }

    But can you ask, how do we test the code where we create a date or use a file? With regard to the dates, I would recommend that no new Date(s) be used in the code at all. Instead, use the type of provider or service to receive the current date/time. Then you can replace this class for testing. Simple implementation may be singlton:

    public class DateTimeUtils {

    private static DateTime fixedTime;
    
    public static DateTime getCurrentDateTime() {
        if (fixedTime == null) {
            return new DateTime(DateTimeZone.UTC);
        }
        return fixedTime;
    }
    public static void useFixedCurrentTime(DateTime timeToReturn) {
        fixedTime = timeToReturn;
    }
    

    }

    With regard to the File facility, you'll be able to substitution classes with mockery. Look at Mockito or PowerMock.

    A little supplement. If you had used test-driven development, there would be no such problem.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2