在MainWindow.xaml中有下面的xaml:
<Window x:Class="TestDependency.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Label Name="someLabel" Grid.Row="0" Content="{Binding Path=LabelText}"></Label>
<Button Grid.Row="2" Click="Button_Click">Change</Button>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
以下是MainWindow.xaml.cs中的代码:
public static readonly DependencyProperty LabelTextProperty = DependencyProperty.Register("LabelText", typeof(String), typeof(MainWindow));
public int counter = 0;
public String LabelText
{
get
{
return (String)GetValue(LabelTextProperty);
}
set
{
SetValue(LabelTextProperty, value);
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
LabelText = "Counter " + counter++;
}
Run Code Online (Sandbox Code Playgroud)
我原以为默认DataContext是背后的代码.但是我被迫指定了DataContext.哪个DataContext是默认值? Null …
我有下面的界面
public interface Interface1 {
Object Execute(String commandToExecute) throws Exception;
}
Run Code Online (Sandbox Code Playgroud)
然后,我尝试进行模拟,以便可以测试将其调用的类的行为:
Interface1 interfaceMocked = mock(Interface1.class);
when(interfaceMocked.Execute(anyString())).thenThrow(new Exception());
Interface2 objectToTest = new ClassOfInterface2(interfaceMocked);
retrievePrintersMetaData.Retrieve();
Run Code Online (Sandbox Code Playgroud)
但是编译器告诉我,有一个未处理的异常。Retrieve方法的定义是:
public List<SomeClass> Retrieve() {
try {
interface1Object.Execute("");
}
catch (Exception exception) {
return new ArrayList<SomeClass>();
}
}
Run Code Online (Sandbox Code Playgroud)
Mockito文档仅显示RuntimeException的使用,而我在StackOverflow上也没有看到类似的东西。我正在使用Java 1.7u25和Mockito 1.9.5