junit

Guava EventBus unit tests

≡放荡痞女 提交于 2019-12-23 09:58:42
问题 I have a simple Guava EventBus with one simple event and one simple listener. My question is what is the test pattern to check if the listener method is invoked once the event is posted. 回答1: I would suggest that testing that the EventBus works properly is not a UNIT test you should be writing. One of the advantages of using a library (at least of using one you trust) is that the unit tests have been written by the library provider. So please don't waste your time verifying that the Google

JUnit assertions within in Java 8 stream

喜夏-厌秋 提交于 2019-12-23 09:57:13
问题 Say I have three objects that I save to the database and set the db generated ID into. I don't know the order of the objects returned from the method saveToDb . But I want to junit test that those generated IDs are there. How do I do that within in stream? I want to do something like this: List<MyObject> myObjects = getObjects(); numRecords = saveToDb(myObjects); // numRecords=3 List<Integer> intArray = Arrays.asList(1, 2, 3); intArray.stream() .forEach(it -> myObjects.stream() .filter(it2 ->

What exactly does assertEquals check for when asserting lists?

吃可爱长大的小学妹 提交于 2019-12-23 09:48:58
问题 In my test I'm asserting that the list I return is an alphabetically ordered list of the one I just created. What exactly does the assertEquals do check for? Does it check the ordering of the list or just its contents? So if I have a list of { "Fred", "Bob", "Anna" } would list 2 of { "Anna", "Bob", "Fred" } return true as they contain the same object, regardless of order? 回答1: If you follow the source code of jUnit. You will see that assertEquals eventually calls the equals method on the

Junit Mockito test case for ResponseEntity<?> in spring integration framework

帅比萌擦擦* 提交于 2019-12-23 09:39:05
问题 I am trying to mock the external call. ResponseEntity<?> httpResponse = requestGateway.pushNotification(xtifyRequest); requestGateway is an interface. public interface RequestGateway { ResponseEntity<?> pushNotification(XtifyRequest xtifyRequest); } Below is the test method i am trying to do. @Test public void test() { ResponseEntity<?> r=new ResponseEntity<>(HttpStatus.ACCEPTED); when(requestGateway.pushNotification(any(XtifyRequest.class))).thenReturn(r); } A compilation error is there in

How can I mock the presence of a properties file on the classpath?

为君一笑 提交于 2019-12-23 09:37:37
问题 This surely is a common problem. I have a properties file like my-settings.properties which is read by an application class. When I write a test class, it needs to test different scenarios of things that could be present in my-settings.properties in order to ensure maximum code coverage (e.g. empty properties file, basic properties file etc). But I can only have one my-settings.properties in my src/test/resources . What would be really great is if there was just some annotation

Why should you avoid conditional logic in unit tests and how? [closed]

老子叫甜甜 提交于 2019-12-23 09:30:12
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 4 years ago . Imagine having the following classes: public class Product { private String name; private double price; // Constructors, getters and setters } public class Products { private List<Product> products; // CRUD methods public double getTotalPrice() { // calculates the price of all products } } I have

Hamcrest generics hell #2 : iterableWithSize gives errror “is not applicable for the arguments”

和自甴很熟 提交于 2019-12-23 09:27:07
问题 In hamcrest (1.3.RC2, with no JUnit dependencies) I am failing using iterableWithSize(). I have an (extension of) an Iterator parametrized with Content like this EndResult<Content> contents = contentRepository.findAllByPropertyValue("title", "*content*"); where EndResult is package org.springframework.data.neo4j.conversion; public interface EndResult<R> extends Iterable<R> {...} and Content is a my Pojo. Now, I would think that this would work assertThat(contents, iterableWithSize(1)); but it

Spring AOP Aspect not working using Mockito

依然范特西╮ 提交于 2019-12-23 09:18:17
问题 I have an @Aspect that weaves the execution of all my controller action methods. It works fine when I run the system, but not in unit testing. I'm using Mockito and junit in the following way: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("file:**/spring-context.xml") @WebAppConfiguration public class UserControllerTest { private MockMvc mockMvc; @Mock private RoleService roleService; @InjectMocks private UserController userController; @Before public void setUp() {

How can I reference unit test classes of a maven dependency in my java project? [duplicate]

☆樱花仙子☆ 提交于 2019-12-23 09:16:33
问题 This question already has answers here : Sharing Test code in Maven (4 answers) Closed 10 months ago . I need to reference some JUnit Tests (src/test/java) from project B in the test package src/test/java of project A whereas B is a maven dependecy of A. Is this even possible? <dependency> <groupId>XYZ</groupId> <artifactId>B</artifactId> <version>${project.version}</version> <type>jar</type> <scope>test</scope> </dependency> Both projects are under my controll. Thanks for your advice 回答1:

How to expect void method call with any argument using EasyMock

北城余情 提交于 2019-12-23 09:03:27
问题 As a part of unit test I need to mock a void function(Which accept any non-primitive paramter. e.g. MAP) call with any argument. mockObj.myMethod(<anyObject>) Is it possible to do this with EasyMock? 回答1: Use either of the anyObject methods: anyObject() or anyObject(T) So expect(mockObj.myMethod(anyObject())); See the Flexible Expectations with Argument Matchers section of the documentation 来源: https://stackoverflow.com/questions/17123993/how-to-expect-void-method-call-with-any-argument-using