小编swa*_*ter的帖子

使用模拟服务/组件进行Spring Boot集成测试

情况和问题:在Spring Boot中,如何将一个或多个模拟类/ bean注入应用程序以进行集成测试?StackOverflow上有一些答案,但它们专注于Spring Boot 1.4之前的情况,或者只是不适合我.

背景是,在下面的代码中,Settings的实现依赖于第三方服务器和其他外部系统.Settings的功能已经在单元测试中进行了测试,因此对于完整的集成测试,我想模拟对这些服务器或系统的依赖性,并提供虚拟值.

MockBean将忽略所有现有bean定义并提供虚拟对象,但此对象不会在注入此类的其他类中提供方法行为.使用@Before方法在测试之前设置行为不会影响注入的对象,也不会在其他应用程序服务(如AuthenticationService)中注入.

我的问题:如何将我的bean注入应用程序上下文?我的测试:

package ch.swaechter.testapp;

import ch.swaechter.testapp.utils.settings.Settings;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.test.context.junit4.SpringRunner;

@TestConfiguration
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {MyApplication.class})
public class MyApplicationTests {

    @MockBean
    private Settings settings;

    @Before
    public void before() {
        Mockito.when(settings.getApplicationSecret()).thenReturn("Application Secret");
    }

    @Test
    public void contextLoads() {
        String applicationsecret = settings.getApplicationSecret();
        System.out.println("Application secret: " + applicationsecret);
    }
}
Run Code Online (Sandbox Code Playgroud)

并且下面应该使用模拟类的服务,但是没有收到这个模拟类:

package ch.swaechter.testapp;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AuthenticationServiceImpl implements AuthenticationService …
Run Code Online (Sandbox Code Playgroud)

spring-test junit4 spring-boot

9
推荐指数
2
解决办法
7582
查看次数

使用JavaFX Platform.runLater并从不同的线程访问UI

我有几个问题Platform.runLater.我有一个JavaFX Application类.在这个类中,我运行一个线程(该线程从网络套接字读取数据).

现在当我Stage在线程内部创建一个新的时,系统会抛出一个execption(JavaFX事件调度程序线程和我的netork-read线程不一样) - 我理解这种行为.

但另一方面是,我将文本从网络阅读器附加到现有的TextArea或添加/删除一些项目ListView<String>- 这不会引发异常 - 为什么?我认为JavaFX是单线程的(ui库部分).这与Swing中的相同:有时它可以工作,有时你只有垃圾(因为EDT)?

我的问题:

  • JavaFX事件调度程序线程何时抛出异常,何时抛出异常?
  • 有关于此的任何好文件吗?
  • 是否有更简单(短和清洁剂)的方式来使用Platform.runLater同一个run()方法?结合try catch(或多次捕获),它看起来很奇怪

我知道Platform.runLater在一个线程中的用法并不是那么好(设计解决方案)

java multithreading event-dispatch-thread javafx-2

8
推荐指数
2
解决办法
2万
查看次数

将错误消息下载或重定向到Spring Web MVC中的另一个控制器操作

想法:我有一个Spring Web MVC动作应该完成其中一个或两个:

  • 从远程服务器下载文件,并将输入流写入响应输出流
  • 或者捕获下载的异常,设置多个错误消息之一并重定向到/ addresses页面.地址页面将显示错误

问题:Spring无法下载文件并在出现问题时重定向 - 某种方式flash属性不起作用,因为重定向中丢失了:

@ResponseBody
@RequestMapping(value = "/download/{fileaddress}", method = RequestMethod.GET)
public void download(HttpServletRequest request, HttpServletResponse response, @PathVariable(value = "fileaddress") String fileaddress) throws Exception
{
    if(fileaddress != null && fileaddress.length() > 0)
    {
        try
        {
            // Get the remove file based on the fileaddress
            RemoteFile remotefile = new RemoteFile(fileaddress);

            // Set the input stream
            InputStream inputstream = remotefile.getInputStream();

            // Write the input stream to the output stream or throw an exception
            Utils.writeTo(inputstream, response.getOutputStream()); …
Run Code Online (Sandbox Code Playgroud)

java redirect spring-mvc download spring-web

8
推荐指数
1
解决办法
4987
查看次数

C标准库的内部工作

我对标准C库的内部工作感兴趣.我找到了一本关于可能的实现的好书 - 但我正在寻找对整个标准库和标准(如POSIX)的更深层次的解释 - 标准库中这些标准的定义.

C草稿非常有用,但阅读起来不是很好.还有关于这个主题的其他文献吗?

  • 标准图书馆-PJ-Plauger 1991
  • FreeBSD的
  • GNU男人
  • C草案

阿尔伯特

c standards

7
推荐指数
2
解决办法
477
查看次数

如何正确监视输入流

我的理解是,它将Mockito.spy(object)代理包装在现有对象周围。该代理将方法调用委托给监视对象并允许进一步验证(因此它与不提供实现的模拟不同)。

我想监视输入流以确保正确调用关闭/读取方法。但以下(简单)间谍代码不起作用:

// Create a spy input stream object
String testData = "Hello";
InputStream inputStream = new ByteArrayInputStream(testData.getBytes(StandardCharsets.UTF_8));
InputStream spiedInputStream = spy(inputStream);
assertEquals(testData.getBytes(StandardCharsets.UTF_8).length, spiedInputStream.available()); // Fails: Expected 5, Actual 0

// Read the input stream
byte [] readData = new byte[testData.length()];
assertEquals(testData.getBytes(StandardCharsets.UTF_8).length, spiedInputStream.read(readData)); // Fails: Expected 5, Actual -1
assertEquals(testData, new String(readData, StandardCharsets.UTF_8)); // Fails, readData is fully zeroed
Run Code Online (Sandbox Code Playgroud)

那么我做错了什么(Ubuntu 22.04,Java 17,Mockito 4.7.0)

java mockito spy junit5

6
推荐指数
1
解决办法
377
查看次数

如何使用QueryParser进行Lucene范围查询(IntPoint/LongPoint)

我真正喜欢 Lucene 的一件事是查询语言,我/应用程序用户可以在其中编写动态查询。我通过解析这些查询

QueryParser parser = new QueryParser("", indexWriter.getAnalyzer());
Query query = parser.parse("id:1 OR id:3");
Run Code Online (Sandbox Code Playgroud)

但这对于像这样的范围查询不起作用:

Query query = parser.parse("value:[100 TO 202]"); // Returns nothing
Query query = parser.parse("id:1 OR value:167"); // Returns only document with ID 1 and not 1 
Run Code Online (Sandbox Code Playgroud)

另一方面,通过 API 它可以工作(但我放弃了仅使用查询作为输入的便捷方法):

Query query = LongPoint.newRangeQuery("value", 100L, 202L); // Returns 1, 2 and 3
Run Code Online (Sandbox Code Playgroud)

这是查询解析器中的错误还是我错过了重要的一点,例如 QueryParser 采用词法值而不是数值?在不使用查询 API 而是解析字符串的情况下如何才能实现这一点?

这个问题是这个问题的后续问题,指出了问题,但没有指出原因:Lucene LongPoint Range search does not work

完整代码:

package acme.prod;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.*;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader; …
Run Code Online (Sandbox Code Playgroud)

java lucene

2
推荐指数
1
解决办法
1994
查看次数