我已经为 JUnit 下载了 2 个 NBM。在 Netbeans 8 中,我使用工具->插件->下载窗口并添加了相关的 NMB。它们都被选中,当我按下“安装”时,Netbeans 会弹出一个对话框,上面写着(大约)“安装程序将下载,验证,然后安装选定的模块”。然后它确实尝试使用 Internet 进行下载 - 但失败了。这台机器无法访问互联网(因此采用离线方法)。
我是否没有下载正确的 NBM(它们是 ZIP 文件,我将它们重命名为 NBM - 阅读这是要做的事情)?有没有我遗漏的其他部分?
被测试的类如下所示:
public class SomeAdapter {
@Inject
HttpService httpService;
@Inject
Configuration configuration;
public SomeAdapter()
{
GuiceInjector.getInjector().injectMembers(this);
}
public String getBaseUrl()
{
return configuration.getProtocol()+ "://" + some.getServer() + ":" + configuration.getPort();
}
}
Run Code Online (Sandbox Code Playgroud)
我已经尝试过来自mockito框架的InjectMocks,但它似乎并不可靠。是否有必要创建一个扩展AbstractModule的单独的测试模块?
我在 junit-dep jar 上有 hamcrest-all-1.3,当我使用 ant 运行时仍然遇到此异常。然而,Eclipse 足够智能,可以解决此冲突并且测试有效。我的类路径上没有mockito,但是,有jmockit 和easymock,我相信这不是问题。这是断言:
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.lessThanOrEqualTo;
/* ...
...
... */
assertThat(10, lessThanOrEqualTo(20));
Run Code Online (Sandbox Code Playgroud)
例外的是
[junit] org/hamcrest/Matchers
[junit] java.lang.NoClassDefFoundError: org/hamcrest/Matchers
我有 Spring 课说
@Component
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
class MainServiceImpl implements MainService {
private final InternalService internalService;
public Set<String> do(String anything) {
Set<String> relevent = internalService.finaIntern(anything);
return relevent;
}
}
Run Code Online (Sandbox Code Playgroud)
我正在编写单元测试用例如下
@RunWith(MockitoJUnitRunner.class)
class TestMainServiceImpl {
@InjectMocks
private MainServiceImpl service;
@Mock
InternalService internalService;
@Before
public void init(){
MockitoAnnotations.initMocks(this);
}
@Test
public void testDo() {
Set<String> setData = new HashSet<>();
setData.add("ABC");
String a ="a";
when(internalService.finaIntern(any(String.class))
.thenReturn(setData);
Set<String> result = service.do(a);
assertTrue(!result.isEmpty());
}
}
Run Code Online (Sandbox Code Playgroud)
在这里我的测试用例失败,但如果我删除的最终形式MainServiceImpl,做一个明确的@Autowired像下面
@Component
class MainServiceImpl implements MainService …Run Code Online (Sandbox Code Playgroud) 我在使用测试(JUnit/Mockito)覆盖以下函数“ postJson ”时遇到问题,并且无法找到模拟该行的方法response = getTarget(path).request().post(entity, Map.class);
//Constructor\npublic HttpService() {\n this.client = ClientBuilder.newClient();\n}\n\nClient client;\n\npublic Map<String, ?> postJson(String path, Map<String, ?> data){\n Map<String, ?> response = null;\n\n try {\n Entity<Map<String, ?>> entity = Entity.entity(data, MediaType.APPLICATION_JSON);\n response = getTarget(path).request().post(entity, Map.class); \n } catch (Exception e) {\n LOG.error(e.toString());\n }\n\n return response;\n}\n\npublic WebTarget getTarget(String path){\n return client.target(BASE_URI + path);\n}\nRun Code Online (Sandbox Code Playgroud)\n\n我目前的测试
\n\n@Test\npublic void postJsonTest(){\n assertEquals(null,new HttpService().postJson("", new HashMap<String,Integer>()));\n\n //Verifica se a fun\xc3\xa7\xc3\xa3o de comunica\xc3\xa7\xc3\xa3o com servidor \xc3\xa9 chamda\n Map<String,String> resposta = new …Run Code Online (Sandbox Code Playgroud) 我有一个计划任务,每天晚上汇总数据。每当我启动应用程序时,任务就会运行,当我在应用程序上运行 jUnit 测试时,我想阻止它运行。
@Scheduled(cron = "0 0 0 1 * ?")
public void SalesDataAggregation() {
//aggregation
}
Run Code Online (Sandbox Code Playgroud)
编辑
上面的方法也在这里被调用
@PostConstruct
public void init(){
SalesDataAggregation();
}
Run Code Online (Sandbox Code Playgroud) 我正在为 springboot 应用程序编写 Junits。当我运行junit时,我发现没有方法可以运行。下面是代码junit:
@RunWith(Suite.class)
@SuiteClasses({ EmployeeServiceApplication.class })
public class TestEmployeeService {
@Mock
EmployeeRepository empRepo;
@Autowired
EmployeeService service;
@BeforeEach
void setMockOutput() {
HashSet<EmployeeView> empSet = new HashSet<>();
empSet.add(new EmployeeView(1, "firstName", "lastName", "05/30/1986", "EE"));
when(empRepo.getEmployeeList()).thenReturn(empSet);
}
@Test
public void testGetEmployeeList() {
Assert.notEmpty(service.getEmployeeList());
}
Run Code Online (Sandbox Code Playgroud)
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.employeeService</groupId>
<artifactId>employeeService</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>employeeService</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> …Run Code Online (Sandbox Code Playgroud) 我的代码库很古老并且被锁定在 JUnit4 中。我想将 testcontainers 与项目集成,以将 docker 容器合并到自动化测试中。
我的开发箱(我控制)运行 docker,但是,我的 CI 系统(我不控制)没有。
如果我可以使用 JUnit5,我只需添加@Testcontainers(disabledWithoutDocker = true)注释,基于docker的测试将在我的开发箱上愉快地运行,而在 CI 机器上被禁用。
JUnit4 相当于@Testcontainers(disabledWithoutDocker = true)什么?
我已经将 Android Studio 更新到 4.1,当我运行 JUnit 时,我得到了错误
!!! JUnit version 3.8 or later expected:
java.lang.RuntimeException: Stub!
at junit.runner.BaseTestRunner.<init>(BaseTestRunner.java:14)
at junit.textui.TestRunner.<init>(TestRunner.java:54)
at junit.textui.TestRunner.<init>(TestRunner.java:48)
at junit.textui.TestRunner.<init>(TestRunner.java:41)
at com.intellij.rt.junit.JUnitStarter.junitVersionChecks(JUnitStarter.java:212)
at com.intellij.rt.junit.JUnitStarter.canWorkWithJUnitVersion(JUnitStarter.java:195)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:49)
Run Code Online (Sandbox Code Playgroud)
下面是我的配置
JUnit Version: 4.12
Mockito Version: 3.5.11
PowerMockito Version: 2.0.7
Espresso Version: 3.3.0
Gradle Plugin version: 4.0.2 and 4.1.0
Gradle Version: 6.1.1 and 6.5
Run Code Online (Sandbox Code Playgroud)
当我下载到 4.0.2 时它工作正常
请告诉我我缺少什么..
从历史上看,JUnit 中有一个流行的约定,即:
@Before/@BeforeEach方法为setUp()@After/@AfterEach方法为tearDown()@BeforeClass/@BeforeAll和@AfterClass/方法名称怎么样@AfterAll?有没有采用的命名约定?
我找不到任何可靠的资源。