草庐IT

java - 使用 junit test 将命令行参数传递给 Spring Boot 应用程序

我有一个非常基本的SpringBoot应用程序,它需要来自命令行的参数,没有它就无法工作。这是代码。@SpringBootApplicationpublicclassApplicationimplementsCommandLineRunner{privatestaticfinalLoggerlog=LoggerFactory.getLogger(Application.class);@AutowiredprivateReaderreader;@AutowiredprivateWriterwriter;publicstaticvoidmain(String[]args){SpringAp

java - 创建 JUnit 测试时没有此类方法错误

过去2天我一直在尝试解决这个问题,但没有成功。我只是尝试使用spring框架和hibernate创建一个基于注释的JUnit测试。我的IDE是netbeans6.5,我使用的是hibernate3、spring2.5.5和JUnit4.4。这是我得到的错误:Testcase:testFindContacts(com.mycontacts.data.dao.MyContactHibernateDaoTransactionTest):CausedanERRORFailedtoloadApplicationContextjava.lang.IllegalStateException:Fail

java - 如何按顺序制作junit测试运行方法

这个问题在这里已经有了答案:HowtoruntestmethodsinspecificorderinJUnit4?(23个回答)关闭6年前.伙计们!我有一个新问题要问你。我正在使用不同的缓存管理器将一些数据添加到缓存中,但我正面临着它的问题。我正在使用junit和spring来做。当我运行测试时,测试方法是随机执行的,但我需要它们按顺序执行。怎么做??这是一些代码和验证控制台输出:服务类:@Service("HelloCache")publicclassCacheServiceImplimplementsCacheInterface{@Autowired@Qualifier("memc

java - 点的 JsonPath JUnit 转义字符

我有一个名为template.welcome.email的json字段,我正在编写一个单元测试,检查该字段是否存在于服务器的回复中,但我找不到名称中的点的转义领域的。我的测试代码是:@TestpublicvoidtestEmailTemplates()throwsException{mockMvc.perform(get("/emailTemplates").contentType(MediaType.APPLICATION_JSON).locale(Locale.UK).accept(MediaType.APPLICATION_JSON)).andDo(print()).andExp

java - 如何让 Spring MVC 在 JUnit 测试中调用验证?

我有一个名为Browser的POJO,我使用HibernateValidator注释进行了注释。importorg.hibernate.validator.constraints.NotEmpty;publicclassBrowser{@NotEmptyprivateStringuserAgent;@NotEmptyprivateStringbrowserName;...}我编写了以下单元测试,试图验证我的Controller方法捕获验证错误。@TestpublicvoidtestInvalidData()throwsException{Browserbrowser=newBrowse

spring - 用于 Spring Boot 应用程序的 JUnit @BeforeClass 非静态工作

如果您希望JUnit的@BeforeClass注释在所有@Test方法之前运行一次,则必须将其声明为静态。但是,这不能与依赖注入(inject)一起使用。我想在运行JUnit测试之前使用SpringBoot清理我@Autowire的数据库。我不能@Autowire静态字段,所以我需要想办法解决。有什么想法吗? 最佳答案 只需使用@Before(而不是@BeforeClass)(或Spring的@BeforeTransaction(取决于您如何初始化数据库))。此注释必须附加到非静态公共(public)方法。当然:@Before在每个

java - Spring jUnit 测试属性文件

我有一个jUnit测试,它有自己的属性文件(application-test.properties)和它的spring配置文件(application-core-test.xml)。其中一种方法使用由springconfig实例化的对象,即spring组件。类中的成员之一从我们的主要属性文件application.properties派生其值。通过jUnit访问此值时,它始终为空。我什至尝试将属性文件更改为指向实际的属性文件,但这似乎不起作用。这是我访问属性文件对象的方式@Component@PropertySource("classpath:application.propertie

spring - 在 JUnit 测试用例上获取 LazyInitializationException

在SpringMVC应用程序中运行JUnit测试时出现问题。测试1(insertTweet)似乎运行良好,但是在测试2中我得到一个“LazyInitializationException”异常(参见下面的完整stactrace)。我理解为什么会抛出它,但不确定session为何关闭以及如何在每个测试2开始时重新打开它(或保持现有session打开以完成剩余测试)?我已经粘贴了与测试类一起抛出的整个StackTrace。org.hibernate.LazyInitializationException:failedtolazilyinitializeacollectionofrole:c

java - 如何使用 Spring 为 JUnit 测试注入(inject) ServletContext?

我想对使用ApacheCXF编写的RESTful接口(interface)进行单元测试。我使用ServletContext来加载一些资源,所以我有:@ContextprivateServletContextservletContext;如果我将它部署在Glassfish上,则会注入(inject)ServletContext并且它会像预期的那样工作。但我不知道如何在我的服务类中注入(inject)ServletContext,以便我可以使用JUnit测试对其进行测试。我使用Spring3.0、JUnit4、CXF2.2.3和Maven。 最佳答案

hibernate - Spring/Hibernate/JUnit - 没有绑定(bind)到线程的 hibernate session

我正在尝试访问测试用例中的当前hibernatesession,并收到以下错误:org.hibernate.HibernateException:NoHibernateSessionboundtothread,andconfigurationdoesnotallowcreationofnon-transactionalonehereatorg.springframework.orm.hibernate3.SpringSessionContext.currentSession(SpringSessionContext.java:63)atorg.hibernate.impl.Sessio