void keyword
This section describes how to declare and invoke a void method.
The following example declares a method named Printgrade and calls it to print a given score.
Example
public class Testvoidmethod {public
static void Main (string[] args) {
printgrade (78.5);
}
public static void Printgrade (double score) {
if (score >= 90.0) {
System.out.println (' A ');
}
else if (score >= 80.0) {
System.out.println (' B ');
}
else if (score >= 70.0) {
System.out.println (' C ');
}
else if (score >= 60.0) {
System.out.println (' D ');
}
else {
System.out.println (' F ');}}}
The results of the above instance compilation run are as follows:
Here the Printgrade method is a void type method, and it does not return a value.
A call to a void method must be a statement. So, it is called in the third line of the main method, in the form of a statement. Just like any statement that ends with a semicolon.
A method of single measuring void type
Java sevice layer will have many types of void methods, such as save*, update*, this method is only to do some updates, there is no return value, the single test can not be based on the return value of the method to write, can only use a special method;
This method environment: Mockito, testng
Methods tested:
void method to be tested
@Override public
void Updaterulename (Long RuleId, String newrulename, long ucid) {
assert.notnull (RuleId, " Rule ID cannot be null ");
Assert.notnull (newrulename, "rule name cannot be null");
Assert.notnull (Ucid, "ucid of operator cannot be null");
String cleannewrulename = Stringutils.trim (newrulename);
if (Stringutils.isblank (Cleannewrulename)) {
throw new IllegalArgumentException ("cannot be empty for the newly rule name");
}
Query Rules object Rule
= Queryrulebyid (RuleId);
if (null = rule) {
throw new illegaldataexception ("not Found");
}
Rule.setruleid (RuleId);
Rule.setrulename (cleannewrulename);
Rule.setupdateucid (ucid);
Rule.setupdatetime (New Date ());
ruledao.updateselective (rule);
}
Methods of testing:
Method test for void return
@Test public void Testupdaterulename () {Long RuleId = 1L;
String newrulename = "Newrulename";
Long ucid = 123L;
list<rule> rules = new arraylist<rule> ();
Rule rule = new rule ();
Rule.setrulestatus ((byte) dbvaluesetting.rule_status_take_effect);
Rules.add (rule);
Query Rule Object map<string, object> params = new hashmap<string, object> ();
Params.put ("RuleId", RuleId);
Mockito.when (Ruledao.queryrulesbycondition (params)). Thenreturn (rules); Mockito.doanswer (New answer<object> () {public Object Answer (Invocationonmock invocation) {//Breakpoint 2: here follow
After executing rule rule = (rule) invocation.getarguments () [0];
Assert.asserttrue (Rule.getrulename (). Equals ("Newrulename"));
return null;
. When (Ruledao). Updateselective (Mockito.any (Rule.class));
Breakpoint 1: First execute to here Ruleservice.updaterulename (RuleId, Newrulename, ucid); }
As the note shows, if you add two breakpoints, during execution, the last call line is executed first, and the stub of Endpoint 2 is executed at the end of 1 execution, at which point the entry parameter of the method execution can be obtained at Breakpoint 2, and the argument is verified to achieve the purpose;
The new Anwer is an interface in which only one method is used to set the proxy execution portal for the method invocation
The realization of Doanswer
Public interface Answer<t> {
/**
* @param invocation the ' invocation on the mock.
* * @return The value to being
returned
*
* @throws throwable the Throwable to be thrown
/T Answer (INVO Cationonmock invocation) throws Throwable;
}
When the code executes to "ruledao.updateselective" (rule); , the interceptor that is invoked against the mock object is triggered, and a dynamic proxy is created in the interceptor, and the invocation of the dynamic proxy is the method of the new answer overlay;
By means of interception and proxy, the method of mock object can be set up and acquired, and the execution class invocation inside void method could be validated by using this method.