Tôi đang cố gắng để viết lớp thử nghiệm cho các phương pháp sau đâymùa xuân trong Mockito
public class CustomServiceImpl implements CustomService {
@Value("#{myProp['custom.url']}")
private String url;
@Autowire
private DataService dataService;
Tôi đang sử dụng giá trị url tiêm tại một trong những phương thức trong lớp. Để kiểm tra điều này tôi đã viết một lớp junit
@RunWith(MockitoJUnitRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext-test.xml" })
public CustomServiceTest{
private CustomService customService;
@Mock
private DataService dataService;
@Before
public void setup() {
customService = new CustomServiceImpl();
Setter.set(customService, "dataService", dataService);
}
...
}
public class Setter {
public static void set(Object obj, String fieldName, Object value) throws Exception {
Field field = obj.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
field.set(obj, value);
}
}
Trong applicationContext-test.xml Tôi đang tải các tập tin bất động sản sử dụng
<util:properties id="myProp" location="myProp.properties"/>
Nhưng giá trị url là không nhận được nạp trong CustomService khi chạy thử nghiệm. Tôi đã tự hỏi nếu có anyway để có được điều này thực hiện.
Cảm ơn
Nếu bạn đang chế nhạo 'CustomService', thì cách sử dụng' CustomServiceImpl' như thế nào? Điều đó không có ý nghĩa. – skaffman
Tôi đang sử dụng CustomeServiceImpl. Đã cập nhật mã. Làm thế nào tôi có thể giả định giá trị của url mà cần phải được đọc từ các tập tin thuộc tính? – rohit
Vì nó bây giờ là viết tắt, bạn không sử dụng Spring để đặt giá trị customService, bạn đặt giá trị theo cách thủ công trong phương thức setup() với mã này: 'customService = new CustomServiceImpl();' – DwB