在我使用Mockito的单元测试中,我想验证NullPointerException
没有被抛出.
public void testNPENotThrown{
Calling calling= Mock(Calling.class);
testClass.setInner(calling);
testClass.setThrow(true);
testClass.testMethod();
verify(calling, never()).method();
}
Run Code Online (Sandbox Code Playgroud)
我的测试设置了testClass
,设置Calling
对象和属性,以便该方法将抛出一个NullPointerException
.
我验证从不调用Calling.method().
public void testMethod(){
if(throw) {
throw new NullPointerException();
}
calling.method();
}
Run Code Online (Sandbox Code Playgroud)
我想要一个失败的测试,因为它抛出一个NullPointerException
,然后我想写一些代码来解决这个问题.
我注意到的是测试总是通过,因为异常永远不会被测试方法抛出.
我正在创建我的第一个N-Tier MVC应用程序,并且我遇到了如何DbContexts
使用我的数据库第一种方法来管理多个的路障.
我有以下几层
Presentation
Service (WCF)
Business
Data Access
Run Code Online (Sandbox Code Playgroud)
我不想在我的服务层中使用实体框架引用,但是我没有看到如何创建接口或管理两个上下文的东西.我让它在IDatabaseFactory中使用单个上下文,但我似乎无法找到管理两个的方法.
下面是我UnitOfWork
在我的服务ctor中创建的,但我看待它的每一种方式我仍然依赖于它SiteModelContainer
,而实际上我有另一个上下文.
public class UnitOfWork : IUnitOfWork
{
private SiteModelContainer _context;
private readonly IDatabaseFactory _databaseFactory;
protected SiteModelContainer SiteContext
{
get { return _context ?? (_context = _databaseFactory.Get()); }
}
public UnitOfWork(IDatabaseFactory factory)
{
_databaseFactory = factory;
_context = _databaseFactory.Get();
}
//More code
}
public class DatabaseFactory : Disposable, IDatabaseFactory
{
private SiteModelContainer _dataContext;
public SiteModelContainer Get()
{
return _dataContext ?? (_dataContext = new SiteModelContainer());
} …
Run Code Online (Sandbox Code Playgroud) 我有2个类的类型列表对象,
class person
{
public string id { get; set; }
public string name { get; set; }
}
List<person> pr = new List<person>();
pr.Add(new person { id = "2", name = "rezoan" });
pr.Add(new person { id = "5", name = "marman" });
pr.Add(new person { id = "3", name = "prithibi" });
List<person> tem = new List<person>();
tem.Add(new person { id = "1", name = "rezoan" });
tem.Add(new person { id = "2", name = "marman" });
tem.Add(new …
Run Code Online (Sandbox Code Playgroud) 我一直在寻找一种将foreach
循环分成多个部分的方法,并且遇到了以下代码:
foreach(var item in items.Skip(currentPage * itemsPerPage).Take(itemsPerPage))
{
//Do stuff
}
Run Code Online (Sandbox Code Playgroud)
会items.Skip(currentPage * itemsPerPage).Take(itemsPerPage)
在每次迭代中处理,还是会被处理一次,并且编译器会自动使用foreach循环的临时结果?
我想从目录中获取除具有特定扩展名的文件之外的所有文件.
在我的目录中,我有以下文件:
file1.txt
file1.ok
file2.txt
file2.ok
file3.txt
file3.ok
file4.txt
file5.xml
file6.ok
Run Code Online (Sandbox Code Playgroud)
我可以通过使用Directory.GetFiles(sourceDirectory,"*.ok")获得"ok"文件但是如何获得所有其他文件的列表?没有extions".ok"的所有文件的列表.
c# ×4
linq ×2
dbcontext ×1
foreach ×1
java ×1
lambda ×1
list ×1
mocking ×1
mockito ×1
optimization ×1
powermock ×1
repository ×1
unit-of-work ×1