junit

How can we mock session values (request.getSession() ) in JUnit?

泄露秘密 提交于 2021-01-28 18:51:12
问题 private BankApp processSelection(HttpServletRequest request, String[] facets, String selectedText) { DebugUtility.debug(LOG, "Enter into JiraController :: processDashBoardPortFolioSelection()"); BankApp project = new BankApp(); List<BankApp> dataAgg = (List<BankApp>) request.getSession() .getAttribute(PROJECT_WISE_SESSION); if (CollectionUtils.isNotEmpty(dataAgg)) { List<BankApp> projects = new ArrayList<>(); for (String currentProjectId : facets) { Project currProject = vendorUtils

JUnit5 Jupiter test terminated in IntelliJ

情到浓时终转凉″ 提交于 2021-01-28 14:24:44
问题 When I run the my unit tests, they immediatly are being terminated. However no logging is presented. (only 'Failed to start' and 'Process finished with exit code 255'). Tests worked before... JUnit 4 does not give me this problem. Test do run succesfully in Maven. I use JUnit5 Jupiter and IntelliJ IDEA 2020.1 (Ultimate Edition). Anyone any thoughts? 回答1: I just had the same issue. The single thing that fixed this - and which I could reproduce - is this: don't .close() System.out (or System

Surefire fails because of checking if System.exit() was called in one of junit tests

孤人 提交于 2021-01-28 14:16:27
问题 To depict the problem I encountered, let's assume there is a dummy class: import static java.lang.System.exit class Example { void methodGeneratingSystemExit1() { exit 1 } void methodGeneratingSystemExit2() { exit 2 } } And a test against it: import org.junit.Test import org.junit.Rule import org.junit.contrib.java.lang.system.ExpectedSystemExit class ExampleTest { @Rule public final ExpectedSystemExit expectedSystemExit = ExpectedSystemExit.none() @Test void "System exits with code 1 when

How to mock external dependencies for final objects?

走远了吗. 提交于 2021-01-28 12:31:43
问题 public class A{ private final B b; public void meth() { //Some code Integer a = b.some_method(a,fun(b)); //Some code } private fun(int b) { return b; } } when(b.some_method(anyInt(),anyInt())).thenReturn(100) How to mock the externally dependency when writing unit tests for class A. When i mock dependency in the above way, the value of "a" is not getting assigned to 100 as expected. 回答1: Actually the answer of Jakub is correct. Maybe you need an example to understand how to do it. Check the

JAVA - LocalDate.plusDay() doesn't work properly [duplicate]

社会主义新天地 提交于 2021-01-28 09:08:25
问题 This question already has answers here : Why LocalDate.plusDays not working here? (2 answers) Closed 2 years ago . I want to create method thatd add trainings which belongs to some trainingCycle to calendar. Problably I made some mistake becouse it's adding all trainings to only one day. TrainingCycle model class: @DynamoDBTable(tableName = "trainingCycle") public class TrainingCycle extends Request { private String id; private String cycleName; private Long cycleTime; private LocalDateTime

Testing non-blocking REST services with Spring MVC

梦想与她 提交于 2021-01-28 07:42:09
问题 I have a Spring MVC application . I want to test this controller: @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @ContextConfiguration("classpath:backoffice-servlet.xml") public class TimeControllerTests { @Autowired private WebApplicationContext wac; private MockMvc mockMvc; @Before public void setup() { this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); } @Test public void should_OK() throws Exception { mockMvc.perform(get("/time/2") .contentType(APPLICATION

How do i write unit test for function with void return type

回眸只為那壹抹淺笑 提交于 2021-01-28 05:41:54
问题 I am new to writing Unit tests. I saw few examples of jUnit , but they all were for functions returning a value. I have to write unit test for following method. Also we are using objects and methods in this method not belonging to this class like ConfigParser and ParseConfig. How do i go about writing Unit tests for such menthods? @RequestMapping(value = "/generate-json" , consumes = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.POST) @PageType(pageType = "PlaceHolderPageType")

Junit @Before not working properly

只愿长相守 提交于 2021-01-28 04:30:52
问题 @Before public void setup(){ Ground ground = new Ground(100, 100); } @Test public void getDimX(){ String msg = "For a newly created Ground(100, 100), ground.getDimensionX() should return 100"; assertEquals(100, ground.getDimensionX()); } The above code returns a NullPointerException. If I move the Ground ground = new Ground(4, 4); into the getDimX() method, the test runs just fine. I have a number of tests that will use the same ground, so I would prefer not to make a new one with each test

Mocking a file, filewriter and csvwriter within a method for unit test throwing NullPointerException

限于喜欢 提交于 2021-01-28 00:05:30
问题 I'm trying to mock a file creation , FileWriter operation and CsvWriter operation. I'm using EasyMock to mock file creation , PowerMockito to mock FileWriter and CSVWriter operations. Even after mocking the file creation, junit showing NullPointerException.. My code is, package example.mypackage.rms.utility; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import au.com.bytecode.opencsv.CSVWriter; public class CSVBeanUtil { public static boolean

How to register Spring Context Events for current test ApplicationContext at runtime

别说谁变了你拦得住时间么 提交于 2021-01-27 17:53:36
问题 Background : My goal is to collect various timestamps while executing JUnit 5 tests in a Spring environment to create a statistic of the durations of the different tasks. (Repository for reference) Because I can't touch the actual test code, I have to register callbacks like the ContextRefreshedEvent from within my JUnit Jupiter extension. Currently I am registering these callbacks in a Spring configuration object and hope that this is found by some component scan in the test application. As