junit

【Spring Boot 单元测试】3. 使用Mock单元测试Controller

眉间皱痕 提交于 2020-01-07 01:23:41
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 如下简单Controller: @RestController public class UserController { @Autowired private UserService userService; @GetMapping("user/{userName}") public Map<String,String> getUserByName(@PathVariable(value = "userName") String userName) { return this.userService.findByName(userName); } @PostMapping("user/save") public void saveUser(@RequestBody User user) { //添加成功 System.out.println("添加成功:"+user.getName()); } } 现在编写一个针对于该Controller getUserByName(@PathVariable(value = "userName") String userName) 方法的测试类: @RunWith(SpringRunner.class) @SpringBootTest public class

【Spring Boot 单元测试】2. 使用Mock单元测试Service

耗尽温柔 提交于 2020-01-07 00:23:45
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 现有如下简单Service: @Service public class UserServiceImpl implements UserService { @Override public Map<String, String> findByName(String name) { if (name.equals("admin")) { Map<String, String> m = new HashMap<>(2); m.put("id", "1"); m.put("name", name); return m; } else { return null; } } } 编写一个该Service的单元测试,测试 findByName 方法是否有效: @Autowired private UserService userService; @Test public void findByName() { Map<String,String> m = this.userService.findByName("admin"); Assert.assertEquals("用户名为admin", "admin", m.get("name")); } 运行后,JUnit没有报错说明测试通过,即 UserService 的

【Spring Boot 单元测试】1. 编写单元测试

≡放荡痞女 提交于 2020-01-06 23:26:35
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 编写单元测试可以帮助开发人员编写高质量的代码,提升代码质量,减少Bug,便于重构。Spring Boot提供了一些实用程序和注解,用来帮助我们测试应用程序,在Spring Boot中开启单元测试只需引入 spring-boot-starter-test 即可,其包含了一些主流的测试库。本文主要介绍基于 Service和Controller的单元测试。 引入 spring-boot-starter-test : <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> JUnit,标准的单元测试Java应用程序; Spring Test & Spring Boot Test,对Spring Boot应用程序的单元测试提供支持; Mockito, Java mocking框架,用于模拟任何Spring管理的Bean,比如在单元测试中模拟一个第三方系统Service接口返回的数据,而不会去真正调用第三方系统; AssertJ,一个流畅的assertion库

Appium/Selenium - assert that element declared as field is NOT displayed

ε祈祈猫儿з 提交于 2020-01-06 20:25:22
问题 I declare a button as field in following fashion: @AndroidFindBy(name = "Schedule") private WebElement calendarButton; ... and later I make sure it's NOT displayed because the app is in some special mode. Assert.assertFalse(this.calendarButton.isDisplayed()); It gives me org.openqa.selenium.NoSuchElementException , but the test is failed. Any ideas how can I make such assertion? The thing I don't want to define By condition a few times in the code, so using property is handy. 回答1: After some

Appium/Selenium - assert that element declared as field is NOT displayed

大兔子大兔子 提交于 2020-01-06 20:24:07
问题 I declare a button as field in following fashion: @AndroidFindBy(name = "Schedule") private WebElement calendarButton; ... and later I make sure it's NOT displayed because the app is in some special mode. Assert.assertFalse(this.calendarButton.isDisplayed()); It gives me org.openqa.selenium.NoSuchElementException , but the test is failed. Any ideas how can I make such assertion? The thing I don't want to define By condition a few times in the code, so using property is handy. 回答1: After some

JUnit to test for StringBuffers

北城以北 提交于 2020-01-06 19:31:09
问题 Assignment: Write a JUnit test assuming you have two StringBuffer references named sbOne and sbTwo and you only want it to pass if the two references point to the same StringBuffer object. I want to make sure this actually a good way to approach this assignment. I wrote the assumed method for practice: package StringBufferJUni; public class StringBufferUni { public static void main(String[] args){ } public boolean StringBuff(StringBuffer sbOne, StringBuffer sbTwo){ sbTwo = sbOne; if(sbTwo=

Getting “muleContext cannot be resolved” error in mule Junit test code

谁说胖子不能爱 提交于 2020-01-06 19:29:31
问题 I am getting error "Unresolved compilation problem: muleContext cannot be resolved" at the line "MuleClient client = muleContext.getClient();" in JUnit test code in Mule. As per my understanding this code is correct. Since i am new to JUnit, i am not getting why this error is coming. Can anyone help me to getout from this problem please. For your information below are the flow and Junit test code. Junit Test: import java.io.IOException; import org.mule.api.client.MuleClient; import org.junit

JUnit测试

不羁的心 提交于 2020-01-06 14:51:08
JUnit测试 使用JUnit,就必须导入jar包: 在项目下 新建lib文件夹。将jar包放入文件夹中 写一个测试类: 1 package com.monkey1024.test; 2 3 import org.junit.After; 4 import org.junit.Before; 5 import org.junit.Test; 6 7 public class MyTest { 8 9 10 @Before 11 public void before() { 12 System.out.println("执行before()方法"); 13 } 14 15 @After 16 public void after() { 17 System.out.println("执行after()方法"); 18 } 19 20 21 22 @Test 23 public void test01() { 24 System.out.println("执行test01()"); 25 26 } 27 28 @Test 29 public void test02() { 30 System.out.println("执行test02()"); 31 32 } 33 34 35 36 37 } 若直接点击运行: 执行before()方法 执行test01() 执行after()方法

My Junit test doesn't run

喜夏-厌秋 提交于 2020-01-06 08:52:34
问题 I'm new to Java, Android and JUnit. I want to learn how to write JUnit tests for an Android application. To that end, I have a very simple Android app (2 activities, 2 buttons, each button goes to the other activity). I want to test the button. This app runs fine on my phone when it's plugged in. I've been looking at the samples provided in the SDK, and I am trying to emulate them. My problem is that when I right-click on my test project, and choose Run As -> Android JUnit test, nothing

JUnit - How to mock MapStruct nested mapper

白昼怎懂夜的黑 提交于 2020-01-06 07:36:12
问题 I'm receiving an annoying NPE when I run a Unit Test for a service layer class. This class use an autogenerated mapper from MapStruct, which inside use another mapper (see in the Mapper annotation the "uses" attribute): Mapper(componentModel = "spring", uses = {UserMapper.class}) public interface CandidateMapper extends EntityMapper<CandidateDTO, Candidate> { @Mapping(target = "createdBy", ignore = true) @Mapping(target = "createdDate", ignore = true) @Mapping(target = "lastModifiedBy",