尝试使用JavaConfig在Spring中编写junit测试

2024-05-18

我正在尝试为我的示例项目编写一个 junit 测试,但不知道如何访问 jUnit 测试中的 ApplicationContext:

这是该项目的主要类:

public static void main(String[] args)
    {
        // in this setup, both the main(String[]) method and the JUnit method both specify that
        ApplicationContext context = new AnnotationConfigApplicationContext( HelloWorldConfiguration.class );
        MessageService mService = context.getBean(MessageService.class);
        HelloWorld helloWorld = context.getBean(HelloWorld.class);

        /**
         * Displaying default messgae
         */
        LOGGER.debug("Message from HelloWorld Bean: " + helloWorld.getMessage());

        /**
         *   Saving Message to database
         */
        Message message = new Message();
        message.setMessage(helloWorld.getMessage());
        mService.SaveMessage(message);

        /**
         * Settting new message in bean
         */
        helloWorld.setMessage("I am in Staten Island, New York");
        LOGGER.debug("Message from HelloWorld Bean: " + helloWorld.getMessage());

        /**
         * Saving Message in database.
         */
        message.setMessage(helloWorld.getMessage());
        mService.SaveMessage(message);

        /**
         * Getting messages from database
         *    - display number of message(s)
         *    - display each message in database
         */
        List<Message> myList = mService.listMessages();
        LOGGER.debug("You Have " + myList.size() + " Message(s) In The Database");

        for (Message i : myList)
        {
            LOGGER.debug("Message: ID: " + i.getId() + ", Message: " + i.getMessage() + ".");
        }
    }

现在这是 junit 测试:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = HelloWorldConfiguration.class)
public class TestApp {


    @Test
    public void testBean() {
        HelloWorld helloWorld = context.getBean(HelloWorld.class);

        helloWorld.setMessage("I Love Dogs");
        Assert.assertEquals(helloWorld.getMessage(), "I Love Dogs");
    }
}

您可以使用自动装配。请注意,大多数时候您对应用程序上下文本身并不感兴趣,而是对与其关联的一个或多个 bean 感兴趣。下面是两个本质上做同样事情的例子:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = HelloWorldConfiguration.class)
public class TestApp {

    @Autowired
    HelloWorld helloWorld;

    @Test
    public void testBean() {
        helloWorld.setMessage(...);
        // asserts, etc.
    }
}

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = HelloWorldConfiguration.class)
public class TestApp {

    @Autowired
    ApplicationContext applicationContext;

    HelloWorld helloWorld;

    @Before
    public void setUp() {
        helloWorld = context.getBean(HelloWorld.class);
    }

    @Test
    public void testBean() {
        helloWorld.setMessage(...);
        // asserts, etc.
    }
}

See the 参考文档 http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/testing.html#testcontext-ctx-management了解详情。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

尝试使用JavaConfig在Spring中编写junit测试 的相关文章

随机推荐

  • 在上下文中提取搜索字符串

    我正在尝试执行 MySQL 查询 在上下文中提取搜索字符串 因此 如果搜索是 mysql 我想从 body 列返回类似的内容 下载后只需几分钟MySQL安装程序即可使用 这就是我现在得到的 但它不起作用 因为它只是从正文字段中获取前 20
  • C程序调用shell脚本

    我有一个小型 C 程序 调用 shell 脚本 myScript sh 我得到的 ret 值为 256 请帮助我了解系统调用出了什么问题 int main int ret ret system myScript sh ret gt gt r
  • 错误关系不存在

    我得到了 error relation causes does not exist 我的节点应用程序出现错误 这种关系确实存在 我不确定问题出在哪里 我创建了该表 CREATE TABLE causes cause id bigint NO
  • 使用 NLog .NET Core 将日志记录到 PostgreSQL DB

    我尝试将日志记录集成到 NET Core 中的数据库 我能够设置 NLog 并将消息记录到 SQL Server 这很容易 但是当我尝试将 DB 切换到 PostgreSQL 时 似乎没有记录任何内容 以下是startup cs中的代码 p
  • 使用 boost asio 捕获 Ctrl-C

    I m trying to catch the Ctrl C in application as demonstrated in the following MWE include
  • 在头文件中声明和定义并仅在其 cpp 文件中使用的变量存在多重定义错误

    我正在将为一个芯片编译而编写的代码移动到另一芯片上 出现的一个问题是存在大量的多重定义错误 其中一些似乎是由于第一个芯片的链接器让我在跨多个源文件使用变量时懒于声明变量 extern 我以前根本没有使用 extern 在 Something
  • 带有 LDAP 身份验证的 SVN 不起作用

    我首先从我的设置开始 为此我们有 2 台服务器 Server1 将用于所有源代码 文件等 在 Server2 上 我们拥有所有用户信息和登录信息 两台服务器都运行在 Centos 6 4 上 现在我们要在 server1 上设置一个 SVN
  • 如何使用深度等于 (Angular 7)

    通过 VS Code 自动导入 import deepEqual require deep equal 不起作用 错误 TS1202 定位时无法使用导入分配 ECMAScript 模块 考虑使用 import as ns from mod
  • WCF 缓慢的 ServiceHost.Open() 调用

    这是一个与此类似的问题 WCF 服务的 Win32Exception ServiceHost Open https stackoverflow com questions 1542242 win32exception servicehost
  • 编译器值类型解析和硬编码“0”整数值

    首先 介绍一些背景知识 阅读问题并接受答案发布在这里 https stackoverflow com questions 8352260 why does the sqlparameter name value constructor tr
  • 如何使用wireshark清晰捕获mysql查询sql

    因为我们使用远程开发Mysql服务器 所以不能轻易检查查询sql 如果使用本地服务器可以tail f general log file查看调用某个http接口时执行了哪些sql 所以我安装了一个wireshark捕获这些从本地发送的查询sq
  • TWA 问题 ~Android 11 版本“请安装 Chrome Stable 72 或更高版本”

    对于 Android 版本 小于 11 我的应用程序运行良好 但对于Android 11的版本 它会显示一条消息 请安装 Chrome 稳定版 72 或更高版本 当我尝试打开应用程序时 更改依赖关系 implementation com g
  • 如何获取多行值

    这是我旧帖子的延续问题 如果您想查看下图的代码 请点击下面的链接 并解决我的问题 如何将两个列值相乘并在每行末尾显示其结果 https stackoverflow com questions 11202617 how to multiply
  • 我应该如何缓冲绘制的矩形以提高性能(C#/.NET/WinForms/GDI+)

    我在做什么 我正在开发一个 C NET 4 7 2 WinForms 应用程序 它使用以下命令在表单上绘制大量填充矩形Graphics FillRectangle https learn microsoft com en us dotnet
  • 使用Azure Service Fabric的默认客户端时如何向请求添加消息头?

    我想知道是否可以将自定义消息头注入到传出请求中以携带附加信息 而无需反序列化有效负载来完成身份验证 验证或请求关联等功能 例如通过消息检查器提供的 wcf Update 使用 SDK v2 您现在可以 相对 轻松地修改 Reliable S
  • 如何将 AWS Cognito 本机用户链接到联合用户

    现在Cognito 允许 https docs aws amazon com cognito user identity pools latest APIReference API AdminLinkProviderForUser html
  • 在 django 1.7 上的数据迁移中,直到原子块结束后才能执行查询

    我有一个相当长的数据迁移 我正在执行此操作来纠正早期的错误迁移 其中某些行创建不正确 我试图根据旧列将值分配给新列 但是 有时这会导致完整性错误 发生这种情况时 我想扔掉导致完整性错误的那个 这是一个代码片段 def load data a
  • 如何在 Kubernetes 中按特定顺序配置 Pod 初始化?

    我想知道如何按特定顺序开始部署 我知道initContainers但这对我不起作用 我有一个巨大的平台 有大约 20 个部署和 5 个有状态集 每个都有自己的服务 环境变量 卷 水平自动缩放器等 所以不可能 或者我不知道如何 在另一个中定义
  • 在 terraform 存储库中设置多个逻辑组织的子文件夹的正确方法是什么?

    目前我正在 azure 中开发一个基础设施 其中包括以下内容 资源组 应用网关 应用服务 etc 我所拥有的一切都在一张单曲里main tf我知道这是一个错误的文件 但我想从那里开始 我目前正在尝试将每个部分移到我的存储库中自己的子文件夹中
  • 尝试使用JavaConfig在Spring中编写junit测试

    我正在尝试为我的示例项目编写一个 junit 测试 但不知道如何访问 jUnit 测试中的 ApplicationContext 这是该项目的主要类 public static void main String args in this s