org.junit.Assert.assertThat()方法的使用及代码示例

x33g5p2x  于2022-01-15 转载在 其他  
字(8.5k)|赞(0)|评价(0)|浏览(207)

本文整理了Java中org.junit.Assert.assertThat()方法的一些代码示例,展示了Assert.assertThat()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Assert.assertThat()方法的具体详情如下:
包路径:org.junit.Assert
类名称:Assert
方法名:assertThat

Assert.assertThat介绍

[英]Asserts that actual satisfies the condition specified by matcher. If not, an AssertionError is thrown with information about the matcher and failing value. Example:

assertThat(0, is(1)); // fails: 
// failure message: 
// expected: is <1> 
// got value: <0> 
assertThat(0, is(not(1))) // passes

org.hamcrest.Matcher does not currently document the meaning of its type parameter T. This method assumes that a matcher typed as Matcher<T> can be meaningfully applied only to values that could be assigned to a variable of type T.
[中]断言actual满足matcher指定的条件。如果不是,则抛出一个AssertionError,其中包含有关匹配器和失败值的信息。示例:

assertThat(0, is(1)); // fails: 
// failure message: 
// expected: is <1> 
// got value: <0> 
assertThat(0, is(not(1))) // passes

org.hamcrest.Matcher当前未记录其类型参数T的含义。此方法假定类型为Matcher<T>的匹配器只能有意义地应用于可分配给T类型变量的值。

代码示例

代码示例来源:origin: Netflix/eureka

private static void verifyResponseOkWithEntity(InstanceInfo original, EurekaHttpResponse<InstanceInfo> httpResponse) {
  assertThat(httpResponse.getStatusCode(), is(equalTo(200)));
  assertThat(httpResponse.getEntity(), is(notNullValue()));
  assertThat(EurekaEntityComparators.equal(httpResponse.getEntity(), original), is(true));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void componentSingleConstructor() {
  this.context = new AnnotationConfigApplicationContext(BaseConfiguration.class,
      SingleConstructorComponent.class);
  assertThat(this.context.getBean(SingleConstructorComponent.class).autowiredName, is("foo"));
}

代码示例来源:origin: LMAX-Exchange/disruptor

private static void assertLastEvent(int[] event, long sequence, DummyEventHandler<int[]>... eh1)
{
  for (DummyEventHandler<int[]> eh : eh1)
  {
    assertThat(eh.lastEvent, is(event));
    assertThat(eh.lastSequence, is(sequence));
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testSpr12701() throws Exception {
  ResolvableType resolvableType = ResolvableType.forClassWithGenerics(Callable.class, String.class);
  Type type = resolvableType.getType();
  assertThat(type, is(instanceOf(ParameterizedType.class)));
  assertThat(((ParameterizedType) type).getRawType(), is(equalTo(Callable.class)));
  assertThat(((ParameterizedType) type).getActualTypeArguments().length, is(equalTo(1)));
  assertThat(((ParameterizedType) type).getActualTypeArguments()[0], is(equalTo(String.class)));
}

代码示例来源:origin: apache/storm

private Map<Object, Object> listToMap(List<Object> list) {
  assertThat(list.size() % 2, is(0));
  Map<Object, Object> res = new HashMap<>();
  for (int i = 0; i < list.size(); i += 2) {
    res.put(list.get(i), list.get(i + 1));
  }
  return res;
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void implicitTxManager() {
  AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
  ctx.register(ImplicitTxManagerConfig.class);
  ctx.refresh();
  FooRepository fooRepository = ctx.getBean(FooRepository.class);
  fooRepository.findAll();
  CallCountingTransactionManager txManager = ctx.getBean(CallCountingTransactionManager.class);
  assertThat(txManager.begun, equalTo(1));
  assertThat(txManager.commits, equalTo(1));
  assertThat(txManager.rollbacks, equalTo(0));
}

代码示例来源:origin: spring-projects/spring-framework

private void assertBeanPropertyValueOf(String propertyName, String expected, DefaultListableBeanFactory factory) {
  BeanDefinition bean = factory.getBeanDefinition(expected);
  PropertyValue value = bean.getPropertyValues().getPropertyValue(propertyName);
  assertThat(value, is(notNullValue()));
  assertThat(value.getValue().toString(), is(expected));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void findsBeansByTypeIfNotInstantiated() {
  assertThat(bf.isTypeMatch("&foo", FactoryBean.class), is(true));
  @SuppressWarnings("rawtypes")
  Map<String, FactoryBean> fbBeans = bf.getBeansOfType(FactoryBean.class);
  assertThat(1, equalTo(fbBeans.size()));
  assertThat("&foo", equalTo(fbBeans.keySet().iterator().next()));
  Map<String, AnInterface> aiBeans = bf.getBeansOfType(AnInterface.class);
  assertThat(aiBeans.size(), is(1));
  assertThat(aiBeans.keySet(), hasItem("&foo"));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void shouldSupportNullFieldType() throws Exception {
  String[] codes = resolver.resolveMessageCodes("errorCode", "objectName", "field",
      null);
  assertThat(codes, is(equalTo(new String[] {
      "errorCode.objectName.field",
      "errorCode.field",
      "errorCode" })));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void componentTwoConstructorsNoHint() {
  this.context = new AnnotationConfigApplicationContext(BaseConfiguration.class,
      TwoConstructorsComponent.class);
  assertThat(this.context.getBean(TwoConstructorsComponent.class).name, is("fallback"));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void send() throws Exception {
  this.session.handleFrame(SockJsFrame.openFrame().getContent());
  this.session.sendMessage(new TextMessage("foo"));
  assertThat(this.session.sentMessage, equalTo(new TextMessage("[\"foo\"]")));
}

代码示例来源:origin: Netflix/eureka

public void expectTransientErrors(int count) throws InterruptedException {
  for (int i = 0; i < count; i++) {
    ProcessingResult task = transientErrorTasks.poll(5, TimeUnit.SECONDS);
    assertThat(task, is(notNullValue()));
  }
}

代码示例来源:origin: LMAX-Exchange/disruptor

private static void assertShutoownCalls(int startCalls, DummyEventHandler<int[]>... handlers)
  {
    for (DummyEventHandler<int[]> handler : handlers)
    {
      assertThat(handler.shutdownCalls, is(startCalls));
    }
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void shouldInvokeAwareMethodsInImportBeanDefinitionRegistrar() {
  AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
  context.getBean(MessageSource.class);
  assertThat(SampleRegistrar.beanFactory, is(context.getBeanFactory()));
  assertThat(SampleRegistrar.classLoader, is(context.getBeanFactory().getBeanClassLoader()));
  assertThat(SampleRegistrar.resourceLoader, is(notNullValue()));
  assertThat(SampleRegistrar.environment, is(context.getEnvironment()));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
@SuppressWarnings("rawtypes")
public void getObjectType() {
  assertThat(factory.getObjectType(), is(equalTo((Class) DateTimeFormatter.class)));
}

代码示例来源:origin: LMAX-Exchange/disruptor

@Test
public void shouldNotifyOfBatchProcessorLifecycle() throws Exception
{
  new Thread(batchEventProcessor).start();
  startLatch.await();
  batchEventProcessor.halt();
  shutdownLatch.await();
  assertThat(Integer.valueOf(handler.startCounter), is(Integer.valueOf(1)));
  assertThat(Integer.valueOf(handler.shutdownCounter), is(Integer.valueOf(1)));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void handleFrameClose() throws Exception {
  this.session.handleFrame(SockJsFrame.openFrame().getContent());
  this.session.handleFrame(SockJsFrame.closeFrame(1007, "").getContent());
  assertThat(this.session.isOpen(), equalTo(false));
  assertThat(this.session.disconnectStatus, equalTo(new CloseStatus(1007, "")));
  verify(this.handler).afterConnectionEstablished(this.session);
  verifyNoMoreInteractions(this.handler);
}

代码示例来源:origin: Netflix/eureka

private static void assertResponseEntityExist(Response httpResponse) {
  ReplicationListResponse entity = (ReplicationListResponse) httpResponse.getEntity();
  assertThat(entity, is(notNullValue()));
  ReplicationInstanceResponse replicationResponse = entity.getResponseList().get(0);
  assertThat(replicationResponse.getResponseEntity(), is(notNullValue()));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void withOptionArgsOnly() {
  CommandLinePropertySource<?> ps =
    new SimpleCommandLinePropertySource("--o1=v1", "--o2");
  assertThat(ps.containsProperty("o1"), is(true));
  assertThat(ps.containsProperty("o2"), is(true));
  assertThat(ps.containsProperty("o3"), is(false));
  assertThat(ps.getProperty("o1"), equalTo("v1"));
  assertThat(ps.getProperty("o2"), equalTo(""));
  assertThat(ps.getProperty("o3"), nullValue());
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void shouldSupportMalformedIndexField() throws Exception {
  String[] codes = resolver.resolveMessageCodes("errorCode", "objectName", "field[",
      TestBean.class);
  assertThat(codes, is(equalTo(new String[] {
      "errorCode.objectName.field[",
      "errorCode.field[",
      "errorCode.org.springframework.tests.sample.beans.TestBean",
      "errorCode" })));
}

相关文章