Tag: unit testing

在使用Spring Boot @WebMvcTest进行测试时,如何从我的上下文中排除其他@Controller

我有多个控制器,我的理解是通过在@WebMvcTest中指定一个控制器,其他控制器不会被加载到上下文中。 来自文档 controllers – 指定要测试的控制器。 如果应将所有@Controller bean添加到应用程序上下文中,则可以留空。 我的第一个控制器 @Controller public class MyController { @Autowired private MyService myService; private final Logger logger = Logger.getLogger(this.getClass()); @RequestMapping(value = “/”, method = RequestMethod.GET) public @ResponseBody ResponseEntity index() { try { myService.get(); return new ResponseEntity(HttpStatus.OK); } catch (Exception e) { logger.error(e); e.printStackTrace(); } return new ResponseEntity(“REQUEST FAILED”, HttpStatus.INTERNAL_SERVER_ERROR); } } […]

无法使用Maven执行Junit5测试

Maven执行 mvn clean test 我正在尝试将junit5用于我的一个maven项目,但是在test阶段无法使用 – 执行unit testing – org.junit.jupiter junit-jupiter-api 5.0.0-M3 我得到的输出是 – [INFO] — maven-surefire-plugin:2.19.1:test (default-test) @ utils — ——————————————————- TESTS ——————————————————- Results : Tests run: 0, Failures: 0, Errors: 0, Skipped: 0 尝试实施提到的解决方案@ Surefire没有采用Junit 5测试来更新依赖关系 – org.junit.jupiter junit-jupiter-api 5.0.0-M3 org.junit junit5-api 5.0.0-ALPHA test 并将插件更新为 – org.apache.maven.plugins maven-surefire-plugin 2.19.1 org.junit surefire-junit5 5.0.0-ALPHA 但输出仍然相同。 […]

RestEasy:org.codehaus.jackson.map.JsonMappingException:无法从START_OBJECT标记(..)中反序列化java.util.ArrayList的实例

我有一个rest端点,它返回List 。 我试图测试这个rest端点为 @Test public void testGetAllVariablesWithoutQueryParamPass() throws Exception { final ClientRequest clientCreateRequest = new ClientRequest(“http://localhost:9090/variables”); final MultivaluedMap formParameters = clientCreateRequest.getFormParameters(); final String name = “testGetAllVariablesWithoutQueryParamPass”; formParameters.putSingle(“name”, name); formParameters.putSingle(“type”, “String”); formParameters.putSingle(“units”, “units”); formParameters.putSingle(“description”, “description”); formParameters.putSingle(“core”, “true”); final GenericType<List> typeToken = new GenericType<List>() { }; final ClientResponse<List> clientCreateResponse = clientCreateRequest.post(typeToken); assertEquals(201, clientCreateResponse.getStatus()); final List variables = […]

从unit testing中启动和停止hsqldb

我正在尝试在内存模式下使用hsqldb创建集成测试。 目前,我必须在运行unit testing之前从命令行启动hsqldb服务器。 我希望能够从我的集成测试中控制hsqldb服务器。 我似乎无法从代码中得到这一切。 更新: 这似乎与在类路径中有一个hibernate.cfg.xml文件一起工作: org.hsqldb.Server.main(new String[]{}); 在我的hibernate.cfg.xml文件中: org.hsqldb.jdbcDriver jdbc:hsqldb:mem:ww sa 1 org.hibernate.dialect.HSQLDialect thread org.hibernate.cache.NoCacheProvider update 更新看起来这只是在使用jUnit和内置测试运行器从Eclipse中运行unit testing时的一个问题。 如果我跑 mvn test 他们被正确执行,没有例外。 就依赖性而言,我是否遗漏了一些东西? 我使用了生成eclipse项目 mvn eclipse:eclipse 我的pom是: 4.0.0 com.myproject myproject 1.0-SNAPSHOT war myproject jstl jstl 1.1.2 taglibs standard 1.1.2 org.hibernate hibernate-core 3.3.2.GA com.oracle ojdbc14 10.1.0.4.0 org.slf4j slf4j-api 1.6.0 org.slf4j slf4j-simple 1.6.0 log4j log4j […]

如何将mock注入到具有@Transactional的@Service中

我在我的unit testing中有任何问题,我有类似的东西。 如果使用Transactional注释blargh函数,则会在blargh上覆盖模拟注入。 如果我删除了Transactional,那么模拟就会停留在那里。 通过观察代码,当服务中的函数使用transactinal进行注释时,Spring似乎懒洋洋地加载服务,但是当服务中的函数没有进行注释时,它会急切地加载服务。 这会覆盖我注入的模拟。 有一个更好的方法吗? @Component public class SomeTests { @Autowired private SomeService someService; @Test @Transactional public void test(){ FooBar fooBarMock = mock(FooBar.class); ReflectionTestUtils.setField(someService, “fooBar”, fooBarMock); } } @Service public class someService { @Autowired FooBar foobar; @Transactional // <– this causes the mocked item to be overridden public void blargh() { fooBar.doStuff(); } […]

Android PointF构造函数不能在JUnit测试中工作

我在尝试编写JUnit测试时偶然发现了这一点。 不可否认,这是我在JUnit中的第一次unit testing,但我发现这种行为非常令人费解。 package com.example.dom.pointfbugrepro; import android.graphics.PointF; import org.junit.Test; import static org.junit.Assert.*; public class ExampleUnitTest { @Test public void pointf_isCorrect() throws Exception { PointF foo = new PointF(5, 0); assertEquals(5, foo.x, 0.0001f); } } 在全新的Android项目中运行此测试会导致断言失败: java.lang.AssertionError: Expected :5.0 Actual :0.0 我在研究这个问题时发现的一件事是,直接分配给PointF实例的x字段确实有效。 那么这里的问题是什么? 为什么构造函数没有正确设置字段? 我应该如何测试使用PointF Android类的类?

Mockito / Powermockito模拟私有空方法

我需要使用mockito和powermock来模拟一个不使用参数的私有void方法。 该方法属于间谍的实例。 我知道我需要这样做的事实表明代码不好但我正在使用一个旧项目将unit testing从一个测试框架转换到另一个测试框架。 如果有人有任何建议,将不胜感激。 谢谢! 到目前为止,我试过这个: PowerMockito.doNothing().when(Whitebox.invokeMethod(spy,”method”,null)); 但我得到这个错误: No method found with name ‘method’ with parameter types: [ ]

如何在Android Unit-Test中模拟Bundle方法?

我有一个处理片段创建的控制器类。 让我们说如下: public class FragmentController { public static Fragment newInstance(String title, int total) { return total > 0? MultipleDataFragment.newInstance(title, total) : SingleDataFragment.newInstance(title); } } public class MultipleDataFragment extends Fragment { public static MultipleDataFragment newInstance( String title, int total) { Bundle b = new Bundle(); b.putString(“title”, title); b.putInt(“total”, total); } } public class SingleDataFragment extends Fragment […]

为什么Mockito @InjectMocks可能是一个避免的事情?

为什么@InjectMocks可能是这种测试要避免的事情。 @RunWith(MockitoJUnitRunner.class) public class MyClassTest { @Mock private Bar bar; @InjectMocks private Foo foo; // created by Mockito @Test public void shouldCallMethod() { // when foo.myMethod(); // then … } } Foo.java public class Foo { private final Bar bar; public Foo(Bar bar) { this.bar = bar; } … 我在对这个答案的评论中读到了这个: https : //stackoverflow.com/a/21172873/516167 关于@InjectMocks 标记应在其上执行注射的区域。 […]

junit assertEquals忽略大小写

我只是从c# – > java移动。 我需要使用junit编写一些测试。 在我的测试中,我需要比较两个字符串,看看它们是否匹配。 所以我们也有Assert.assertEquals,但这是区分大小写的。 我怎样才能使它不区分大小写? 我需要的是: “blabla”.equals(“BlabLA”) 返回真实。 所以在C#中,我们曾经有: public static void AreEqual ( string expected, string actual, bool ignoreCase, string message ) 我很快就通过Junit文档,但我似乎无法找到这样的东西。