JunitGenerator

######################################################################################## 
## 
## Available variables: 
##         $entryList.methodList - List of method composites 
##         $entryList.privateMethodList - List of private method composites 
##         $entryList.fieldList - ArrayList of class scope field names 
##         $entryList.className - class name 
##         $entryList.packageName - package name 
##         $today - Todays date in MM/dd/yyyy format 
## 
##            MethodComposite variables: 
##                $method.name - Method Name 
##                $method.signature - Full method signature in String form 
##                $method.reflectionCode - list of strings representing commented out reflection code to access method (Private Methods) 
##                $method.paramNames - List of Strings representing the method's parameters' names 
##                $method.paramClasses - List of Strings representing the method's parameters' classes 
## 
## You can configure the output class name using "testClass" variable below. 
## Here are some examples: 
## Test${entry.ClassName} - will produce TestSomeClass 
## ${entry.className}Test - will produce SomeClassTest 
## 
######################################################################################## 
## 
#macro (cap $strIn)$strIn.valueOf($strIn.charAt(0)).toUpperCase()$strIn.substring(1)#end 
#macro (lowerCap $strIn)$strIn.valueOf($strIn.charAt(0)).toLowerCase()$strIn.substring(1)#end 
## Iterate through the list and generate testcase for every entry. 
#foreach ($entry in $entryList) 
#set( $testClass="${entry.className}Test") 
## 
package $entry.packageName; 

import cn.vv.web.vdc.BaseTest;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.MockitoAnnotations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

/** 
* @see $entry.packageName.${entry.className} Tester. 
* @author zhongmaming
* @date $today
* @version 1.0 
*/ 
@AutoConfigureMockMvc
public class $testClass extends BaseTest { 

    @InjectMocks
    private ${entry.className} #lowerCap(${entry.className});
    
    @Autowired
    private MockMvc mvc;
    
    @Before
    public void setUp() throws Exception { 
        //build MockMvc instance
        mvc = MockMvcBuilders.standaloneSetup(#lowerCap(${entry.className})).build();
        MockitoAnnotations.initMocks(this);
    } 
    
    @After
    public void tearDown() throws Exception { 
    } 
    
    #foreach($method in $entry.methodList) 
    
    /** 
     * @see $entry.packageName.${entry.className}#$method.signature 
     */ 
    @Test
    public void test#cap(${method.name})() throws Exception {
        this.mvc.perform(post("/api/crawl2vdc")
                .characterEncoding("UTF-8")
                .contentType(MediaType.APPLICATION_JSON)
                .accept(MediaType.APPLICATION_JSON).content("{}"))
                .andExpect(status().isOk())
                .andDo(print()).andReturn().getResponse().getContentAsString(); 
    } 
    #end 

    #foreach($method in $entry.privateMethodList)
    
    /**
     * @see $entry.packageName.${entry.className}#$method.signature
     */
    @Test
    public void test#cap(${method.name})() throws Exception {
    #foreach($string in $method.reflectionCode)
    $string
    #end
    
    }
    #end

}
#end
######################################################################################## 
## 
## Available variables: 
##         $entryList.methodList - List of method composites 
##         $entryList.privateMethodList - List of private method composites 
##         $entryList.fieldList - ArrayList of class scope field names 
##         $entryList.className - class name 
##         $entryList.packageName - package name 
##         $today - Todays date in MM/dd/yyyy format 
## 
##            MethodComposite variables: 
##                $method.name - Method Name 
##                $method.signature - Full method signature in String form 
##                $method.reflectionCode - list of strings representing commented out reflection code to access method (Private Methods) 
##                $method.paramNames - List of Strings representing the method's parameters' names 
##                $method.paramClasses - List of Strings representing the method's parameters' classes 
## 
## You can configure the output class name using "testClass" variable below. 
## Here are some examples: 
## Test${entry.ClassName} - will produce TestSomeClass 
## ${entry.className}Test - will produce SomeClassTest 
## 
######################################################################################## 
## 
#macro (cap $strIn)$strIn.valueOf($strIn.charAt(0)).toUpperCase()$strIn.substring(1)#end 
#macro (lowerCap $strIn)$strIn.valueOf($strIn.charAt(0)).toLowerCase()$strIn.substring(1)#end 
## Iterate through the list and generate testcase for every entry. 
#foreach ($entry in $entryList) 
#set( $testClass="${entry.className}Test") 
## 
package $entry.packageName; 

import org.junit.Test; 
import org.junit.Before; 
import org.junit.After; 
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.powermock.modules.junit4.PowerMockRunner;

/** 
* @see $entry.packageName.${entry.className} Tester. 
* @author zhongmaming
* @date $today
* @version 1.0 
*/ 
@RunWith(PowerMockRunner.class)
public class $testClass{ 

    @InjectMocks
    private ${entry.className} #lowerCap(${entry.className});
    
    @Before
    public void setUp() throws Exception { 
    } 
    
    @After
    public void tearDown() throws Exception { 
    } 
    
    #foreach($method in $entry.methodList) 
    
    /** 
     * @see $entry.packageName.${entry.className}#$method.signature 
     */ 
    @Test
    public void test#cap(${method.name})() throws Exception { 
    } 
    #end 

    #foreach($method in $entry.privateMethodList)
    
    /**
     * @see $entry.packageName.${entry.className}#$method.signature
     */
    @Test
    public void test#cap(${method.name})() throws Exception {
    #foreach($string in $method.reflectionCode)
    $string
    #end
    
    }
    #end

}
#end
原文地址:https://www.cnblogs.com/exmyth/p/12458555.html