简体   繁体   中英

can anybody give the unit test for the method in java

public class This {
    int a;

    void data() {
        int a = 10;
        this.a = 200;      // this keyword current object variable access
        System.out.println("a=" + a);
    }

    public static void main(String args[]) {

        This t = new This();

        t.data();
        System.out.println("a=" + t.a);

    }

}

//junit test for data() method

Here an simple test using junit and assertJ

import org.assertj.core.api.Assertions;

@Test
public void testIt(){
   This t = new This();
   t.data();
   Assertions.assertThat(t.a)
      .isEqualTo(200);
}

Please note, that your a field should have either public visibility or method accessor (like getA() method).

A few unit-testing best practices articles:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM