@Test // SPR-14238 public void sendToUserWithSendToDefaultOverride() throws Exception { given(this.messageChannel.send(any(Message.class))).willReturn(true); Class<?> clazz = SendToUserWithSendToOverrideTestBean.class; Method method = clazz.getDeclaredMethod("handleAndSendToDefaultDestination"); MethodParameter parameter = new SynthesizingMethodParameter(method, -1); String sessionId = "sess1"; Message<?> inputMessage = createMessage(sessionId, "sub1", null, null, null); this.handler.handleReturnValue(PAYLOAD, parameter, inputMessage); verify(this.messageChannel, times(1)).send(this.messageCaptor.capture()); assertResponse(parameter, sessionId, 0, "/user/sess1/dest-default"); }
@Test public void sendToNoAnnotations() throws Exception { given(this.messageChannel.send(any(Message.class))).willReturn(true); String sessionId = "sess1"; Message<?> inputMessage = createMessage(sessionId, "sub1", "/app", "/dest", null); this.handler.handleReturnValue(PAYLOAD, this.noAnnotationsReturnType, inputMessage); verify(this.messageChannel, times(1)).send(this.messageCaptor.capture()); assertResponse(this.noAnnotationsReturnType, sessionId, 0, "/topic/dest"); }
@Test public void sendToUserClassDefaultNoAnnotation() throws Exception { given(this.messageChannel.send(any(Message.class))).willReturn(true); String sessionId = "sess1"; Message<?> inputMessage = createMessage(sessionId, "sub1", null, null, null); this.handler.handleReturnValue(PAYLOAD, this.userDefaultNoAnnotation, inputMessage); verify(this.messageChannel, times(1)).send(this.messageCaptor.capture()); assertResponse(this.userDefaultNoAnnotation, sessionId, 0, "/user/sess1/dest-default"); }
@Test public void subscribeTwice() { assertThat(this.channel.subscribe(this.handler), equalTo(true)); assertThat(this.channel.subscribe(this.handler), equalTo(false)); this.channel.send(this.message); verify(this.handler, times(1)).handleMessage(this.message); }
@Test public void sendToUserDefaultDestinationWhenUsingDotPathSeparator() throws Exception { given(this.messageChannel.send(any(Message.class))).willReturn(true); TestUser user = new TestUser(); Message<?> inputMessage = createMessage("sess1", "sub1", "/app/", "dest.foo.bar", user); this.handler.handleReturnValue(PAYLOAD, this.sendToUserDefaultDestReturnType, inputMessage); verify(this.messageChannel, times(1)).send(this.messageCaptor.capture()); SimpMessageHeaderAccessor accessor = getCapturedAccessor(0); assertEquals("/user/" + user.getName() + "/queue/dest.foo.bar", accessor.getDestination()); }
@Test public void resolveResourceExisting() throws Exception { String file = "bar.css"; Resource expected = new ClassPathResource("test/" + file, getClass()); given(this.chain.resolveResource(null, file, this.locations)).willReturn(expected); this.resolver.setStrategyMap(Collections.singletonMap("/**", this.versionStrategy)); Resource actual = this.resolver.resolveResourceInternal(null, file, this.locations, this.chain); assertEquals(expected, actual); verify(this.chain, times(1)).resolveResource(null, file, this.locations); verify(this.versionStrategy, never()).extractVersion(file); }
@Test public void resolveResourceNoVersionInPath() throws Exception { String file = "bar.css"; given(this.chain.resolveResource(null, file, this.locations)).willReturn(null); given(this.versionStrategy.extractVersion(file)).willReturn(""); this.resolver.setStrategyMap(Collections.singletonMap("/**", this.versionStrategy)); Resource actual = this.resolver.resolveResourceInternal(null, file, this.locations, this.chain); assertNull(actual); verify(this.chain, times(1)).resolveResource(null, file, this.locations); verify(this.versionStrategy, times(1)).extractVersion(file); }
@Test public void emptySubProtocol() throws Exception { this.session.setAcceptedProtocol(""); this.webSocketHandler.setDefaultProtocolHandler(this.defaultHandler); this.webSocketHandler.afterConnectionEstablished(session); verify(this.defaultHandler).afterSessionStarted( isA(ConcurrentWebSocketSessionDecorator.class), eq(this.inClientChannel)); verify(this.stompHandler, times(0)).afterSessionStarted(session, this.inClientChannel); verify(this.mqttHandler, times(0)).afterSessionStarted(session, this.inClientChannel); }
@Test public void beforeAndAfterTestClassForDirtiesContextDeclaredLocallyOnClassBeforeClass() throws Exception { Class<?> clazz = DirtiesContextDeclaredLocallyBeforeClass.class; BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz); beforeListener.beforeTestClass(testContext); afterListener.beforeTestClass(testContext); verify(testContext, times(1)).markApplicationContextDirty(EXHAUSTIVE); afterListener.afterTestClass(testContext); beforeListener.afterTestClass(testContext); verify(testContext, times(1)).markApplicationContextDirty(EXHAUSTIVE); }
@Test public void resolveResourceNotFound() { String file = "something/something.js"; given(this.chain.resolveResource(this.exchange, file, this.locations)).willReturn(Mono.empty()); Resource actual = this.resolver .resolveResource(this.exchange, file, this.locations, this.chain) .block(TIMEOUT); assertNull(actual); verify(this.chain, times(1)).resolveResource(this.exchange, file, this.locations); verify(this.chain, never()).resolveResource(this.exchange, null, this.locations); }
@Test public void beforeAndAfterTestMethodForDirtiesContextDeclaredLocallyOnClassBeforeEachTestMethod() throws Exception { Class<?> clazz = DirtiesContextDeclaredLocallyBeforeEachTestMethod.class; BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz); given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("clean")); beforeListener.beforeTestMethod(testContext); afterListener.beforeTestMethod(testContext); verify(testContext, times(1)).markApplicationContextDirty(EXHAUSTIVE); afterListener.afterTestMethod(testContext); beforeListener.afterTestMethod(testContext); verify(testContext, times(1)).markApplicationContextDirty(EXHAUSTIVE); }
@Test public void resolveResourceNoVersionStrategy() throws Exception { String file = "missing.css"; given(this.chain.resolveResource(null, file, this.locations)).willReturn(null); this.resolver.setStrategyMap(Collections.emptyMap()); Resource actual = this.resolver.resolveResourceInternal(null, file, this.locations, this.chain); assertNull(actual); verify(this.chain, times(1)).resolveResource(null, file, this.locations); }
@Test public void beforeAndAfterTestClassForDirtiesContextDeclaredLocallyOnClassAfterClass() throws Exception { Class<?> clazz = DirtiesContextDeclaredLocallyAfterClass.class; BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz); beforeListener.beforeTestClass(testContext); afterListener.beforeTestClass(testContext); verify(testContext, times(0)).markApplicationContextDirty(any(HierarchyMode.class)); afterListener.afterTestClass(testContext); beforeListener.afterTestClass(testContext); verify(testContext, times(1)).markApplicationContextDirty(EXHAUSTIVE); }
@Test public void beforeAndAfterTestClassForDirtiesContextDeclaredViaMetaAnnotationOnClassAfterClass() throws Exception { Class<?> clazz = DirtiesContextDeclaredViaMetaAnnotationAfterClass.class; BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz); beforeListener.beforeTestClass(testContext); afterListener.beforeTestClass(testContext); verify(testContext, times(0)).markApplicationContextDirty(any(HierarchyMode.class)); afterListener.afterTestClass(testContext); beforeListener.afterTestClass(testContext); verify(testContext, times(1)).markApplicationContextDirty(EXHAUSTIVE); }
@Test public void subProtocolMatch() throws Exception { this.webSocketHandler.setProtocolHandlers(Arrays.asList(stompHandler, mqttHandler)); this.session.setAcceptedProtocol("v12.sToMp"); this.webSocketHandler.afterConnectionEstablished(session); verify(this.stompHandler).afterSessionStarted( isA(ConcurrentWebSocketSessionDecorator.class), eq(this.inClientChannel)); verify(this.mqttHandler, times(0)).afterSessionStarted(session, this.inClientChannel); }
@Test public void connectSockJsInfoCached() throws Exception { setupInfoRequest(true); this.sockJsClient.doHandshake(handler, URL); this.sockJsClient.doHandshake(handler, URL); this.sockJsClient.doHandshake(handler, URL); verify(this.infoReceiver, times(1)).executeInfoRequest(any(), any()); }
@Test public void beforeAndAfterTestMethodForDirtiesContextDeclaredViaMetaAnnotationOnClassAfterClass() throws Exception { Class<?> clazz = DirtiesContextDeclaredViaMetaAnnotationAfterClass.class; BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz); given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("clean")); beforeListener.beforeTestMethod(testContext); afterListener.beforeTestMethod(testContext); afterListener.afterTestMethod(testContext); beforeListener.afterTestMethod(testContext); verify(testContext, times(0)).markApplicationContextDirty(any(HierarchyMode.class)); }
@Test public void handleReturnTypeResource() throws Exception { Resource returnValue = new ByteArrayResource("Content".getBytes(StandardCharsets.UTF_8)); given(resourceMessageConverter.canWrite(ByteArrayResource.class, null)).willReturn(true); given(resourceMessageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.ALL)); given(resourceMessageConverter.canWrite(ByteArrayResource.class, MediaType.APPLICATION_OCTET_STREAM)) .willReturn(true); processor.handleReturnValue(returnValue, returnTypeResource, mavContainer, webRequest); then(resourceMessageConverter).should(times(1)).write(any(ByteArrayResource.class), eq(MediaType.APPLICATION_OCTET_STREAM), any(HttpOutputMessage.class)); assertEquals(200, servletResponse.getStatus()); }
@Test public void sendAndReceive() { Destination destination = new Destination() {}; Message<String> request = createTextMessage(); javax.jms.Message replyJmsMessage = createJmsTextMessage(); given(this.jmsTemplate.sendAndReceive(eq(destination), any())).willReturn(replyJmsMessage); Message<?> actual = this.messagingTemplate.sendAndReceive(destination, request); verify(this.jmsTemplate, times(1)).sendAndReceive(eq(destination), any()); assertTextMessage(actual); }
@Test public void sendAndReceiveDefaultDestination() { Destination destination = new Destination() {}; this.messagingTemplate.setDefaultDestination(destination); Message<String> request = createTextMessage(); javax.jms.Message replyJmsMessage = createJmsTextMessage(); given(this.jmsTemplate.sendAndReceive(eq(destination), any())).willReturn(replyJmsMessage); Message<?> actual = this.messagingTemplate.sendAndReceive(request); verify(this.jmsTemplate, times(1)).sendAndReceive(eq(destination), any()); assertTextMessage(actual); }