我需要测试一个属于服务类的方法。此服务类在构造函数中有几个依赖项,其中一些由此方法使用,另一些则不是。如果我们不应该使用DI容器进行单元测试,那么实例化服务类的最佳方法是什么?
var service = new ServiceClass(new Repository1(), new Repository2(), new ServiceClass2(), etc.);
这很难阅读,而且似乎只为了测试一个方法就需要很多代码。当其中一些依赖项有自己的依赖项时,事情就会变得非常混乱。
使用测试数据对HTTP触发的Azure函数进行单元测试是很好且经常描述的。例如:How to write unit test for azure function v1 模拟数据在http请求中给出。 但是我有一个定时器触发器Azure函数,可以从FTP服务器读取数据。我想用测试数据对函数进行单元测试。 我的函数: [FunctionName("MyTimerTriggeredFunction")]
public static void Run([TimerTrigger("0 */2 * * * *")]TimerInfo myTimer, ILogger
当我试图在UT中使用Mockito时,Null指针有问题。
ServiceClass{
@Inject
private UtilsOne;
...
public void methodOne(object1, object2, List<> elements){
--- some logic ---
UtilsOne.method1();
--- some logic ---
UtilsOne.method2();
--- some logic ---
Element e =
我认为,对于何时的工作方式,或者更具体地说,Mockito是如何工作的,我有一个基本的误解。
我有一个服务类,它通过构造函数注入了一个实用程序类。该实用工具类还具有由构造函数自动处理的其他依赖项。
服务类方法调用实用程序类中的几个方法。测试在被调用的实用程序方法上使用when/thenReturn语句。当我对服务方法进行调用时,我得到一个带有null参数的实用程序方法的NPE。但是我期望在中设置子句中设置的参数。代码如下:
@Service
public class ServiceClass {
private Utility utility;
public ServiceCl
我有一组服务,我想将它们划分为ServiceClass,并将服务引用到它所属的服务类。我将引用存储在Dictionary<Service, ServiceClass>中。
在下面的代码中,我想分成五个服务类。我觉得下面的代码写得不好,比如变量的命名,但是我不知道如何改进它,有人能帮忙吗?
int group = 5;
List<Service> service = FileIO.ReadFeatureFile(@"D:\C Drive\Desktop\input.txt");
int numPerClass = service.Count / group
是否有一种方法可以在控制器之外使用等待()机制?
我不希望有在控制器中进行异步调用的代码,而是在服务类中有可以被多个控制器重用的代码,但是,由于该方法是受保护的,因此无法在控制器外部进行调用。
例如,在控制器中:
ServiceClass service = new My ServiceClass();
MyObject myObject= service.getMeAnObject();
render(myObject);
以及服务班:
public class ServiceClass
{
...
public MyObject get
我有一个Autofac模块,如下所示
public class ServiceInjector:Module
{
protected override void Load(ContainerBuilder builder)
{
// many registrations and type looking up here
...
// One of the registration, say t which is found
我刚刚开始研究Java应用程序的Spock测试框架。我之前没有使用Groovy的经验。如何使用Spock框架将mock注入到构造函数中?下面是我的代码和测试示例。
public class ResourceClass {
private final IDynamoDBMapper factory = new DefaultDynamoDBClientFactory(DynamoDBConfig.fromProperties()).mapperClient();
private ServiceClass service;
@Inject
public Reso
在使用JMockit测试服务类时,我希望模拟域依赖项。问题是,域对象在服务的一个方法中实例化,使用的域构造函数具有参数。
域类:
package com.example;
public class DomainClass {
String str;
public DomainClass(String s) {
str=s;
}
public String domainMethod() {
return str;
}
}
服务类
package com.example;
public class ServiceClass {
public String s
我在我的应用程序中运行前台服务,如果它们在我的活动中运行,我需要删除它们。从我的搜索中我发现这是一种选择:
private fun isMyServiceRunning(serviceClass: Class<*>): Boolean {
val manager = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
for (service in manager.getRunningServices(Int.MAX_VALUE)) {
if (serviceClass.name
我有一个从application.properties访问属性的方法&我正在为它编写junit。 class ServiceClass{
@Value("${urlfromapplicationproperties}")
public String myUrl ;
public String getUrl(){
return myUrl;
}
} 下面是测试用例: @RunWith(SpringJunit4ClassRunner.class)
@PropertySource("classpath:test:properties")
public
我已经实现了服务。它将在后台运行3分钟。在服务中,我从web服务获取值并将其存储在数据库中。
用户在同一个UI中,数据也要更新。我正在将数据库中的数据显示到列表视图中。
下面是我的服务类:
public class ServiceClass extends Service {
private DbHelper mHelper;
private SQLiteDatabase dataBase;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStart
我看到serviceClass类加载器类型是MockClassLoader。有什么办法防止这种情况发生吗?@PowerMockIgnore是唯一的选择吗?
类A使用powermock运行,并通过反射调用B类的方法
@RunWith(PowerMockRunner.class)
public class A {
@Test
public void testA() throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityExcepti
在我现在正在从事的.net 3.5项目中,我正在为一个服务类编写一些测试。
public class ServiceClass : IServiceClass
{
private readonly IRepository _repository;
public ServiceClass(IRepository repository)
{
_repository = repository;
}
#region IServiceClass Members
public IEnumerable<ActionT
我有一个要上传的活动,我正在从中调用意图服务。在那里,我正在处理API请求调用。
我想让一个activity知道服务是否正在运行,并显示一个上传标签。
我尝试执行以下操作以确定该服务是否正在运行:
public void startUploadServiceTask() {
if (Util.isNetworkAvailable(mContext)) {
if (!isMyServiceRunning(UploadDriveService.class)) {
startService(new Intent(mContext,
我正在尝试使用AuthenticationStateProvider的作用域服务来获取解决方案不同类中的用户详细信息。用作作用域服务的服务类如下所示: public class ServiceClass
{
private readonly AuthenticationStateProvider _authenticationStateProvider;
public ServiceClass(AuthenticationStateProvider authenticationStateProvider)
{
_authenticationState