Tag: unit testing

在Spring Boot测试类上使用@WebMvcTest批注时出错

我已经按照这里描述的“测试Spring MVC切片”​​部分为Spring MVC控制器编写了一个测试类。 这个类看起来像这样: @RunWith(SpringRunner.class) @WebMvcTest(controllers=OurController.class) public class OurControllerTest { @Autowired private MockMvc mvc; @MockBean private OurRepository ourRepository; @Test public void testGetStuff() { // code removed } } 当我运行它时,我收到以下错误: MockHttpServletResponse: Status = 401 Error message = null Headers = {Cache-Control=[no-store], Pragma=[no-cache], WWW-Authenticate=[Bearer realm=”oauth2-resource”, error=”unauthorized”, error_description=”Full authentication is required to access this resource”], Content-Type=[application/json;charset=UTF-8], X-Content-Type-Options=[nosniff], X-XSS-Protection=[1; […]

测试Web应用程序

我是一名学生,我开发了一个基于JSP的Web应用程序。 现在我的教授建议我应该为我的网络应用程序做unit testing等测试。 任何人都可以建议我可以用什么其他测试来certificate我的应用程序的性能。 还有我可以研究如何进行unit testing的任何好资源,因为我以前从未做过任何测试。 谢谢!

使用JUnit 4的预期exception机制导致意外行为的原因?

我试图测试一个特定的方法从方法抛出预期的exception。 根据JUnit4文档和这个答案,我把测试编写为: @Test(expected=CannotUndoException.class) public void testUndoThrowsCannotUndoException() { // code to initialise ‘command’ command.undo(); } 但是,此代码未通过JUnit测试,将抛出(和预期)exception报告为错误。 我正在测试的方法只有在体内: public void undo() { throw new CannotUndoException(); } 此外,以下测试通过: public void testUndoThrowsCannotUndoException() { // code to initialise ‘command’ try { command.undo(); fail(); } catch (CannotUndoException cue){ } } 意味着实际抛出了预期的exception。 我实际上计划将方法更改为实际执行某些操作,而不是仅仅抛出exception,但是让我对导致问题的原因感到好奇,以免将来再次发生。 已进行以下检查: 导入到测试用例中的CannotUndoException是正确的 JUnit的第4版是我的类路径中唯一的一个 清理和构建Eclipse工作区并没有改变结果 我正在使用JUnit 4.1,在同一个测试中我正在使用Mockito。 什么可能导致错误的失败?

任何基于YAML的Java夹具加载器?

我已经使用过DbUnit了,但是在最近玩Play Framework后我发现它的Fixtures.load(String yamlFilename)非常有用。 任何人都知道可以与任何Java项目一起使用的类似工具吗?

使用MockMvc在Spring MVC中进行unit testing/登录

我有一个使用Spring MVC创建的非常简单的REST应用程序。 (代码可以在GitHub上获得 。)它有一个简单的WebSecurityConfigurer ,如下所示: @Override protected void configure(HttpSecurity httpSecurity) throws Exception { httpSecurity .csrf().disable() .exceptionHandling() .authenticationEntryPoint(authenticationEntryPoint) .and() .authorizeRequests() .antMatchers(“/user/new”).permitAll() .anyRequest().authenticated() .and() .formLogin() .loginPage(“/login”).permitAll() .successHandler(authenticationSuccessHandler) .failureHandler(authenticationFailureHandler) .and() .logout() .permitAll() .logoutSuccessHandler(logoutSuccessHandler); } 当我运行应用程序时,自定义控制器和登录/注销页面都可以正常工作。 我甚至可以通过MockMvc unit testing /user/new MockMvc 。 但是,当我尝试使用以下function测试/login时 @Test public void testUserLogin() throws Exception { RequestBuilder requestBuilder = post(“/login”) .param(“username”, testUser.getUsername()) .param(“password”, testUser.getPassword()); mockMvc.perform(requestBuilder) […]

如何在每个测试方法中强制运行静态块?

我发现静态块只在我执行多个JUnit测试时运行一次。 如何强制它为每个测试方法运行? 我正在使用最新的JUnit 4.8.2 另外,根据xUnit设计原则,每种方法都应该完全独立于其他方法。 为什么静态块只能执行一次? @Test TestMethod1 () { Accounts ac = new Accounts(); ac.method1(); //kill the thread inside } @Test TestMethod2 () { Accounts ac = new Accounts(); ac.method2(); // the thread is no longer available!! } class Accounts { static { // initalize one thread to monitor something } } 当TestMethod1和TestMethod2在不同的测试类中时,甚至会发生这种情况。

无法运行arquillian测试

我正在尝试使用Arquillian进行一些unit testing,但是我无法找到使用Maven部署时失败的原因。 这是class级考试: package com.ndeveloper.spec.test; import javax.inject.Inject; import junit.framework.Assert; import org.jboss.arquillian.container.test.api.Deployment; import org.jboss.arquillian.junit.Arquillian; import org.jboss.shrinkwrap.api.ArchivePaths; import org.jboss.shrinkwrap.api.ShrinkWrap; import org.jboss.shrinkwrap.api.asset.ByteArrayAsset; import org.jboss.shrinkwrap.api.spec.JavaArchive; import org.junit.Test; import org.junit.runner.RunWith; import com.ndeveloper.spec.controller.GreetingBean; @RunWith(Arquillian.class) public class GreetingBeanTest { @Inject GreetingBean greetingBean; @Deployment public static JavaArchive createTestArchive() { return ShrinkWrap.create( JavaArchive.class,”test.jar”). addClass(GreetingBean.class).addAsManifestResource( new ByteArrayAsset(“”.getBytes()), ArchivePaths.create(“beans.xml”)); } @Test public void testInjection(){ Assert.assertEquals(“Hello World”, […]

如何让我的Spring-JUnit测试认为它在GenericApplicationContext中运行?

我使用的是Spring 3.2.6.RELEASE , JUnit 4.11和DWR 3.0.0-rc2 。 我的问题是,在运行Spring-JUnit集成测试时,如何模拟org.springframework.context.support.GenericApplicationContext中运行的内容? 我试过这个: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration({ “classpath:test-context.xml”, “classpath:dwr-context.xml” }) @WebAppConfiguration public class MyServiceIT {} 我的“dwr-context.xml”文件设置为 但是,我的所有JUnit测试最终都会因以下exception而死: (“java.lang.IllegalStateException:WebApplicationObjectSupport实例[org.directwebremoting.spring.DwrController@11297ba3]不在WebApplicationContext中运行,而是在:org.springframework.context.support.GenericApplicationContext@4e513d61”)中运行。 如果我能弄清楚如何欺骗系统在GenericApplicationContext运行,我就会遇到这个问题。 java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:157) at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109) at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75) at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:321) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:288) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:290) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) at […]

对java Mock文件的建议(模拟java.io.File)

有没有人有java模拟File对象的建议? 我使用的是需要获取java.io.File对象作为参数的第三方类。 我通过webservice(也是他们的产品之一)在流中接收此文件的数据。 一种解决方案是将所有这些数据写入文件并将其提供给类。 这是一个我不喜欢的解决方案:它消除了使用Web服务而不是仅下载文件的优势。 更快更有效的方法是将内存中的数据放入Mock文件中,并将此Mock文件提供给第三方类。 它可能必须是一个扩展java.io.File的MockFile,并覆盖与硬盘上的文件实际连接的所有函数。 我知道第三方应该使用流作为输入参数而不是文件。 但是,这超出了我的影响范围。

我想测试一个私有方法 – 我的设计有问题吗?

所以我对软件测试非常陌生,我正在考虑为我的一个应用程序添加几个测试。 我有一个公共方法addKeywords(),它沿途调用私有方法removeInvalidOperations()。 这种私有方法调用外部API并且是大约50行代码。 我认为这是一个有点复杂的方法,我想通过调用addKeyword()方法来测试它,而不必这样做。 但这似乎不可能(至少不是JUnit)。 我所看到的信息表明,测试私有方法的愿望可能是代码味道。 有些人认为这可能表明这应该被重构为一个单独的类并公之于众。 此外,还有一些建议是,如果您确实需要,则可以对生产代码进行编辑,例如更改私有方法的可见性。 我真的不明白为什么我当前的代码设计有问题,但也不喜欢编辑我的生产代码以满足我的测试需求的想法。 正如我所说 – 我对测试很新,所以任何帮助都非常感谢。 另外,如果有任何进一步的信息,我可以告诉我,以帮助解答。