2016-08-11 5 views
0

Employee Service 테스트를위한 테스트 클래스를 만들고, 모든 것이 잘 작동합니다. 다른 함수에서 Field를 사용할 때를 제외하고는 null을 얻습니다.Groovy 클래스의 필드 선언 Spock 프레임 워크 유닛 테스트 케이스

// field 
Employee employee; 
// this will be assigned to return of below 
employee= employeeService.create(emp); 
// this(employee) gets null 

지금 무엇을 원하는되어

아래는 내 코드, 삭제 테스트 기능에 대한

  employeeService.remove(employee.getId); 

을 사용하는 것입니다. 친절하게 제안하기

나는 groovy를 처음 사용합니다.

package services.employee 
import spock.lang.Specification 
@ContextConfiguration(loader =   SpringApplicationContextLoader.class,classes = Application.class) 
class EmployeeSpec extends Specification{ 

    @Autowired 
    EmployeeService employeeService; 

Employee response = null; 
def "Check if Employee exists"(){ 
    setup: 
    long empid = 43; 

    when: 
    empid > 0 

    then: 
    employeeService.getEmployee(empid); 
} 

def "Find all Employee"(){ 
    setup: 

    when: 
    def res = employeeService.getAllEmployees(); 

    then: 
    res.size()>0; 
} 

def "Insert a New Employee"(){ 

    setup: 
    Employee employee = new Employee(); 
    employee.setName("Ajit Singh"); 
    employee.setCity("Delhi"); 
    employee.setAge(34); 

    when: 
    response = employeeService.createEmployee(employee); 

    then: 
    response.getName().equals("Ajit Singh"); 

} 

def "Updating an Employee"(){ 


} 

def "delete an Employee"(){ 
    setup: 
    if (response.equals(null)) 
     println("Object is null"); 

     when: 
     employeeService.removeEmployee(response.empID) 

    then: 
    def res = employeeService.find(response.empID); 
    res == null; 
} 

}

답변

1

:

@Shared Employee response = null

이 문서 이유를 인용하기 위해 (https://spockframework.github.io/spock/docs/1.1-rc-1/all_in_one.html#_fields 참조) 이것이 올바른 방법입니다.

정적 fie lds는 상수에만 사용해야합니다. 그렇지 않으면 공유에 대한 의미가보다 잘 정의되어 있기 때문에 공유 필드가 바람직합니다.

+0

글쎄, 당신의 대답은 매우 매력적입니다 –

0

그루비는 전역 범위의 일종이 없습니다. '정적'키워드를 추가 할 수 있습니다. 작품 위의 Ivans의 대답은, 스팍에서이 작업을 수행 할 수있는 적절한 방법이 @Shared 주석을 사용하는 동안

static Employee response = null; 
0

예, 그 태그로 선언 된 변수는 내가 당신의 'createEmployee'함수가 반환 모르는 자바 또는 C 의 가능성 전역 변수입니다 @Shared 태그를 사용할 수 있지만 그 문제에 아니에요 groovy로 모든 유형 대신 'def'유형을 사용할 수 있습니다.
그래서 그냥 추가하십시오 @Shared def 응답 = null

관련 문제