Tag: unit testing

访问另一个类中的内部枚举以进行测试

如何访问另一个类中的内部枚举类? 例如: public class Foo { enum Bar { ONE, TWO, FOUR, EIGHT, SIXTEEN } // …methods here } 我正在尝试访问Foo.Bar的类: public class FooTest { private Foo f; @Before public void setUp() throws Exception { f = new Foo(); } public void testCase1() { assertEquals(Bar.ONE, f.climbUp()); } } 我尝试过Foo.Bar.ONE , Bar.ONE以及使用Foo.class.getDeclaredField(“Bar”)创建一个新变量,但这些似乎都没有用。 getDeclaredClasses()似乎得到了$Bar但我无法从那里访问任何东西。 更新:我不允许修改类Foo

为什么组件扫描不适用于Spring Bootunit testing?

服务类FooServiceImpl使用@Service aka @Component进行注释,这使其有资格进行自动assembly。 为什么在unit testing期间没有拾取和自动assembly此类? @Service public class FooServiceImpl implements FooService { @Override public String reverse(String bar) { return new StringBuilder(bar).reverse().toString(); } } @RunWith(SpringRunner.class) //@SpringBootTest public class FooServiceTest { @Autowired private FooService fooService; @Test public void reverseStringShouldReverseAnyString() { String reverse = fooService.reverse(“hello”); assertThat(reverse).isEqualTo(“olleh”); } } 测试无法加载应用程序上下文, 2018-02-08T10:58:42,385 INFO Neither @ContextConfiguration nor @ContextHierarchy found for test […]

测试返回的字符串是否具有java类

我有一个生成java类并写入.java文件的方法。 如何在这个方法上编写unit testing,以确保写入文件的字符串格式是标准的java类格式。 例如:我应检查它是否有包装声明应检查包装是否在课前声明打开和关闭括号等…

从Eclipse内部运行JUnit测试套件

从历史上看,我总是写这样的unit testing: public void WidgetTest { @Test public void test_whatever() { // Given… // When… // Then… } } 这允许我在Eclipse和Run As >> JUnit右键单击我的测试文件,并直接从Eclipse内部测试该特定测试。 然后,在进行本地(基于Ant)构建时,我配置一个 Ant任务来立即运行我的所有src/test/java测试。 我现在正在寻找一种中间解决方案。 也就是说,只需单击一个按钮,就可以同时从Eclipse内部运行所有测试类。 一位同事建议Junit有一个“测试套件”的概念,我可以附加我的所有测试类,但看起来这个测试套件是某种我不希望包含在内的JAR /工具我的项目。 所以我问: 如何定义这样一个“测试套件”,包括我所有的测试类,并从Eclipse内部一次性地运行所有这些测试套件? 提前致谢。

使用powermock对静态方法进行unit testing

我想为我的项目中的一些静态方法编写unit testing用例, 我的class级代码片段, Class Util{ public static String getVariableValue(String name) { if(isWindows()){ return some string… } else{ return some other string… } } public static boolean isWindows(){ if(os is windows) return true; else return false; } } 基本上,当isWindows()返回’false’时,我想为getVariableValue()编写unit testing用例。 我如何使用powermock写这个?

unit testing使用资源包的静态方法

我已经阅读了很多关于使用Powermock和Mockito的文章,并尝试了很多不同的方法,但我仍然无法找到unit testing以下静态方法的方法。 public static Map getEntries() { Map myEntriesMap = new TreeMap(); ResourceBundle myEntries = ResourceBundle.getBundle(ENTRIES_BUNDLE); Enumeration enumList = myEntries.getKeys(); String key = null; String value = null; while (enumList.hasMoreElements()) { key = enumList.nextElement().toString(); value = myEntries.getString(key); myEntriesMap.put(key, value); } return myEntriesMap; } 代码是包含大约30个这样的静态方法的(遗留)类的一部分,并且重构实际上不是一个选项。 类似地,在一些其他静态方法中,正在检索DB连接。 例如:如何模拟资源包ENTRIES_BUNDLE并对此方法进行unit testing? 我正在寻找一种可以普遍适用于所有静态方法的模式。

@RequestMapping的产生对值的顺序敏感吗?

这个问题是基于这个问题 。 通过提供的评论,我编写了三种不同的测试来validation正确设置的内容类型。 @Test public void testGetImageJpg_ShouldSucceed() throws Exception { File testImage = new File(TestConstants.TEST_IMAGE_JPG); byte[] expectedBytes = IOUtils.toByteArray(new FileInputStream(testImage)); when(service.getImage(anyString(), anyString())).thenReturn(testImage); mockMvc.perform(get(“/getImage/id/bla.jpg”).sessionAttrs(session)) .andExpect(status().isOk()).andExpect(content().contentType(MediaType.IMAGE_JPEG)) .andExpect(content().bytes(expectedBytes)); } @Test public void testGetImagePng_ShouldSucceed() throws Exception { File testImage = new File(TestConstants.TEST_IMAGE_PNG); byte[] expectedBytes = IOUtils.toByteArray(new FileInputStream(testImage)); when(service.getImage(anyString(), anyString())).thenReturn(testImage); mockMvc.perform(get(“/getImage/id/bla.png”).sessionAttrs(session)) .andExpect(status().isOk()).andExpect(content().contentType(MediaType.IMAGE_PNG)) .andExpect(content().bytes(expectedBytes)); } @Test public void testGetImageGif_ShouldSucceed() throws Exception […]

unit testing框架 – 使用可配置值定义threadPoolSize的TestNG

我正在挖掘一下TestNG框架。 我在我的测试用例中使用注释来配置线程值,例如: @Test(threadPoolSize = 2, invocationCount = 10) public void testOne() { //some code } 这个想法是在配置文件中配置这些值,这些值应该传递给所有测试。 所以我需要从配置项中更改这些值或通过unitTest构造函数传递此值,但TestNG只接受CONSTANT值。 任何提示/想法? 提前致谢!

如何测试ListActivity?

我是Android开发以及测试驱动开发的新手。 我想为以下ListActivity编写unit testing: public class TrendsMainActivity extends ListActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); String[] list_items = getResources().getStringArray(R.array.trend_menu_names); setListAdapter(new ArrayAdapter(this, R.layout.main, list_items)); } @Override protected void onListItemClick(ListView listView, View view, int position, long id) { Intent intent = null; switch(position) { case 0: intent = new Intent(this, TrendingActivity.class); break; case 1: intent = […]

找到正确的忽略unit testing

我有大项目,许多人忽略了unit testing。 有没有什么方法可以使用surefire maven插件运行所有被忽略的测试并查看通过的测试列表? 忽略的测试总数> 1000,因此,手动不可能。